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

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

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

16
  def show; end
17
18
19

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

23
  def edit; end
24
25
26

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

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

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

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

58
59
  private

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

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

  def volunteer_params
74
    params.require(:volunteer).permit(volunteer_attributes, :rejection_type, :rejection_text,
75
      :trial_period, :intro_course, :doc_sent, :bank_account, :evaluation)
76
77
  end
end