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

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

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

22
  def show; end
23
24
25

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

29
  def edit; end
30
31
32

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

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

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

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

64
65
  private

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

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

  def volunteer_params
80
    params.require(:volunteer).permit(volunteer_attributes, :bank, :iban, :waive, :external)
81
82
  end
end