volunteers_controller.rb 1.97 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
10
    authorize Volunteer
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
11
    @q = Volunteer.ransack(params[:q])
12
    @volunteers = @q.result
13
14
  end

15
  def show; end
16
17
18

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

22
  def edit; end
23
24
25

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

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

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

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

57
58
  private

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

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

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