volunteers_controller.rb 1.42 KB
Newer Older
1
class VolunteersController < ApplicationController
2
  include NestedAttributes
3
4
5
6
7
8
9
10
11
12
  before_action :set_volunteer, only: [:show, :edit, :update, :destroy]

  def index
    @volunteers = Volunteer.all
  end

  def show; end

  def new
    @volunteer = Volunteer.new
13
    @volunteer.schedules << Schedule.build
14
15
16
17
18
19
  end

  def edit; end

  def create
    @volunteer = Volunteer.new(volunteer_params)
20
21
22
23
    if @volunteer.save
      redirect_to @volunteer, notice: t('volunteer_created')
    else
      render :new
24
25
26
27
    end
  end

  def update
28
    if @volunteer.update(volunteer_params)
29
      if @volunteer.state == 'accepted'
30
31
        new_user = User.new(email: @volunteer.email,
          password: Devise.friendly_token, role: 'volunteer')
32
        new_user.save
33
34
35
36
37
        new_user.invite!
        redirect_to volunteers_path, notice: t('invite_sent', email: new_user.email)
      else
        redirect_to @volunteer, notice: t('volunteer_updated')
      end
38
39
    else
      render :edit
40
41
42
43
44
    end
  end

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

  private

  def set_volunteer
    @volunteer = Volunteer.find(params[:id])
  end

  def volunteer_params
55
    params.require(:volunteer).permit(
56
          volunteer_attributes,
57
58
59
          language_skills_attributes: language_skills_attributes,
          relatives_attributes: relatives_attributes,
          schedules_attributes: schedules_attributes)
60
61
  end
end