volunteers_controller.rb 1.92 KB
Newer Older
1
class VolunteersController < ApplicationController
2
  include NestedAttributes
3
  include ContactAttributes
4
  include VolunteerAttributes
5
6
  include MakeNotice

7
8
9
  before_action :set_volunteer, only: [:show, :edit, :update, :destroy]

  def index
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
10
    @q = Volunteer.ransack(params[:q])
11
    @volunteers = @q.result
12
13
  end

14
  def show; end
15
16
17

  def new
    @volunteer = Volunteer.new
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
18
    authorize @volunteer
19
20
  end

21
  def edit; end
22
23
24

  def create
    @volunteer = Volunteer.new(volunteer_params)
25
    @volunteer.registrar = current_user
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
26
    authorize @volunteer
27
    if @volunteer.save
28
      redirect_to @volunteer, notice: t('volunteer_created')
29
30
    else
      render :new
31
32
33
34
    end
  end

  def update
35
36
    state_was_registered = @volunteer.registered?
    return render :edit unless @volunteer.update(volunteer_params)
37
    if state_was_registered && @volunteer.accepted? && invite_volunteer_user
38
      redirect_to volunteers_path,
39
        notice: t('invite_sent', email: @volunteer.contact.primary_email)
40
    else
41
      redirect_to @volunteer, notice: t('volunteer_updated')
42
    end
43
44
45
46
  end

  def destroy
    @volunteer.destroy
47
    redirect_to volunteers_url, notice: t('volunteer_destroyed')
48
49
  end

50
  def seeking_clients
51
    @q = Volunteer.where(state: Volunteer::SEEKING_CLIENTS).ransack(params[:q])
52
    @seeking_clients = @q.result
53
54
  end

55
56
  private

57
  def invite_volunteer_user
58
    new_user = User.new(
59
      email: @volunteer.contact.primary_email, password: Devise.friendly_token,
60
61
      role: 'volunteer', volunteer: @volunteer
    )
62
    new_user.save && new_user.invite!
63
64
  end

65
66
  def set_volunteer
    @volunteer = Volunteer.find(params[:id])
67
    authorize @volunteer
68
69
70
  end

  def volunteer_params
71
    params.require(:volunteer).permit(volunteer_attributes, :rejection_type, :rejection_text,
72
73
74
      :trial_period, :intro_course, :doc_sent, :bank_account, :evaluation, :flexible,
      :morning, :afternoon, :evening, :workday, :weekend, :detailed_description
    )
75
76
  end
end