volunteers_controller.rb 2.22 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
14
15
16
17
18
19
20
21
    respond_to do |format|
      if params[:format] == 'xlsx'
        @volunteers = @q.result
        format.xlsx
      else
        @volunteers = @q.result.paginate(page: params[:page])
        format.html
      end
    end
22
23
  end

24
  def show; end
25
26
27

  def new
    @volunteer = Volunteer.new
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
28
    authorize @volunteer
29
30
  end

31
  def edit; end
32
33
34

  def create
    @volunteer = Volunteer.new(volunteer_params)
35
    @volunteer.registrar = current_user
36
    authorize @volunteer
37
    if @volunteer.save
38
      redirect_to @volunteer, notice: t('volunteer_created')
39
40
    else
      render :new
41
42
43
44
    end
  end

  def update
45
46
    state_was_registered = @volunteer.registered?
    return render :edit unless @volunteer.update(volunteer_params)
47
    if state_was_registered && @volunteer.accepted? && invite_volunteer_user
48
      redirect_to volunteers_path,
49
        notice: t('invite_sent', email: @volunteer.contact.primary_email)
50
    else
51
      redirect_to @volunteer, notice: t('volunteer_updated')
52
    end
53
54
55
56
  end

  def destroy
    @volunteer.destroy
57
    redirect_to volunteers_url, notice: t('volunteer_destroyed')
58
59
  end

60
  def seeking_clients
61
    authorize Volunteer
62
    @q = Volunteer.where(state: Volunteer::SEEKING_CLIENTS).ransack(params[:q])
63
    @seeking_clients = @q.result.paginate(page: params[:page])
64
65
  end

66
67
  private

68
  def invite_volunteer_user
69
    new_user = User.new(
70
      email: @volunteer.contact.primary_email, password: Devise.friendly_token,
71
72
      role: 'volunteer', volunteer: @volunteer
    )
73
    new_user.save && new_user.invite!
74
75
  end

76
77
  def set_volunteer
    @volunteer = Volunteer.find(params[:id])
78
    authorize @volunteer
79
80
81
  end

  def volunteer_params
82
    params.require(:volunteer).permit(volunteer_attributes, :rejection_type, :rejection_text,
83
84
      :trial_period, :intro_course, :doc_sent, :bank_account, :evaluation, :flexible,
      :morning, :afternoon, :evening, :workday, :weekend, :detailed_description)
85
86
  end
end