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

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

  def index
11
    authorize Volunteer
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
12
    @q = Volunteer.ransack(params[:q])
13
    respond_to do |format|
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
14
      format.xlsx do
15
        @volunteers = @q.result
Kaspar Vollenweider's avatar
Kaspar Vollenweider committed
16
17
      end
      format.html do
18
19
20
        @volunteers = @q.result.paginate(page: params[:page])
      end
    end
21
22
  end

23
  def show; end
24
25
26

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

30
  def edit; end
31
32
33

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

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

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

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

65
  def volunteer_hours
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
66
    @volunteer_hours = Hour.where(volunteer_id: @volunteer)
67
68
  end

69
70
  private

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

79
80
  def set_volunteer
    @volunteer = Volunteer.find(params[:id])
81
    authorize @volunteer
82
83
84
  end

  def volunteer_params
Chrysanthi Lagodimou's avatar
Chrysanthi Lagodimou committed
85
    params.require(:volunteer).permit(volunteer_attributes, :bank, :iban, :waive)
86
87
  end
end