semester_process_volunteers_controller.rb 2.66 KB
Newer Older
Jiri Strojil's avatar
Jiri Strojil committed
1
class SemesterProcessVolunteersController < ApplicationController
Jiri Strojil's avatar
Jiri Strojil committed
2
  before_action :prepare_review, only: [:review_semester, :submit_review]
Jiri Strojil's avatar
Jiri Strojil committed
3
  before_action :initialize_feedback, only: [:review_semester, :submit_review]
4
  before_action :set_semester_process_volunteer, only: [:show, :edit, :update]
5
  before_action :set_semester, only: [:index]
Jiri Strojil's avatar
Jiri Strojil committed
6
7
8
9
10
11
12
13
14
15

  include SemesterProcessVolunteerHelper

  def review_semester
    @hour = Hour.new
  end

  def submit_review
    assign_reviewed_attributes
    set_reviewed
Jiri Strojil's avatar
Jiri Strojil committed
16
    @semester_process_volunteer.volunteer.validate_waive_and_bank = true
Jiri Strojil's avatar
Jiri Strojil committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    begin
      ActiveRecord::Base.transaction do
        @semester_process_volunteer.semester_feedbacks << @feedback
        @hour.save! unless @hour.hours == 0 || @hour.hours.blank?
        @semester_process_volunteer.save!
        @volunteer.save!
      end

      redirect_to(
        review_semester_semester_process_volunteer_path(@semester_process_volunteer),
        notice: 'Successfully reviewed.'
      )
      rescue ActiveRecord::RecordInvalid => exception
        null_reviewed
Jiri Strojil's avatar
Jiri Strojil committed
31
        @hours.reload
Jiri Strojil's avatar
Jiri Strojil committed
32
33
34
35
        render :review_semester, notice: exception
    end
  end

36
37
  def index
    authorize SemesterProcessVolunteer
38
39

    @spvs = SemesterProcessVolunteer.index(Semester.parse(params[:semester])).page(params[:page])
40
41
42
43
44
45
46
  end

  def show; end

  def edit; end

  def update
47
48
    if @spv.update(semester_process_params)
      redirect_to @spv, notice: 'Semester process was successfully updated.'
49
50
51
52
53
    else
      render :edit
    end
  end

Jiri Strojil's avatar
Jiri Strojil committed
54
55
  private

Jiri Strojil's avatar
Jiri Strojil committed
56
  def prepare_review
Jiri Strojil's avatar
Jiri Strojil committed
57
58
    # careful cuz mission id can be present in both missions
    @semester_process_volunteer = SemesterProcessVolunteer.find(params[:id])
Jiri Strojil's avatar
Jiri Strojil committed
59
60
    @hours = @semester_process_volunteer.hours
    @volunteer = @semester_process_volunteer.volunteer
Jiri Strojil's avatar
Jiri Strojil committed
61
    @mission = @semester_process_volunteer.missions.first
Jiri Strojil's avatar
Jiri Strojil committed
62
    authorize @semester_process_volunteer
Jiri Strojil's avatar
Jiri Strojil committed
63
64
65
66
67
68
69
70
  end

  def review_params
    params.require(:semester_process_volunteer).permit(
      volunteer_attributes: [:waive, :iban, :bank],
      semester_feedback: [:goals, :achievements, :future, :comments, :conversation],
      hour: [:hours])
  end
Jiri Strojil's avatar
Jiri Strojil committed
71

72
  def set_semester_process_volunteer
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    @spv = SemesterProcessVolunteer.find(params[:id])
    authorize @spv
    @semester_process = @spv.semester_process
    @volunteer = @spv.volunteer
  end

  def set_semester
    @semester = Semester.new
    if params[:semester]
      @selected_semester = Semester.parse(params[:semester])
    else
      @selected_semester = @semester.previous
      params[:semester] = Semester.to_s(@selected_semester)
    end
87
88
89
90
91
  end

  def semester_process_volunteer_params
    params.require(:semester_process_volunteer).permit(:semester)
  end
Jiri Strojil's avatar
Jiri Strojil committed
92
end