volunteers_controller.rb 1.6 KB
Newer Older
1
class VolunteersController < ApplicationController
2
  include NestedAttributes
3
  include VolunteerAttributes
4
  before_action :set_volunteer, only: [:show, :edit, :update, :destroy]
5
  before_action :set_state, only: [:update]
6
7

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

12
  def show; end
13
14
15

  def new
    @volunteer = Volunteer.new
16
    @volunteer.schedules << Schedule.build
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
17
    authorize @volunteer
18
19
  end

20
  def edit; end
21
22
23

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

  def update
33
    if @volunteer.update(volunteer_params)
34
      if @state == Volunteer::INTERESTED && @volunteer.state == Volunteer::ACCEPTED
35
36
        new_user = User.new(email: @volunteer.email,
          password: Devise.friendly_token, role: 'volunteer')
37
        new_user.save
38
39
40
41
42
        new_user.invite!
        redirect_to volunteers_path, notice: t('invite_sent', email: new_user.email)
      else
        redirect_to @volunteer, notice: t('volunteer_updated')
      end
43
44
    else
      render :edit
45
46
47
48
49
    end
  end

  def destroy
    @volunteer.destroy
50
    redirect_to volunteers_url, notice: t('volunteer_destroyed')
51
52
53
54
  end

  private

55
56
57
58
  def set_state
    @state = @volunteer.state
  end

59
60
  def set_volunteer
    @volunteer = Volunteer.find(params[:id])
61
    authorize @volunteer
62
63
  end

64
65


66
  def volunteer_params
67
    params.require(:volunteer).permit(
68
69
      volunteer_attributes,
      language_skills_attributes, relatives_attributes, schedules_attributes
70
    )
71
72
  end
end