semester_processes_controller.rb 3.03 KB
Newer Older
1
class SemesterProcessesController < ApplicationController
2
  before_action :set_semester_process, only: [:show, :edit, :update]
3
  before_action :set_semester, only: [:new, :create]
4
5
6
7
8
9
10
11
12

  def index
    authorize SemesterProcess
    @semester_processes = SemesterProcess.all.paginate(page: params[:page])
  end

  def show; end

  def new
13
14
    @semester_process = SemesterProcess.new(semester: @selected_semester)
    @semester_process.build_semester_volunteers(@volunteers)
15
    authorize @semester_process
Jiri Strojil's avatar
Jiri Strojil committed
16
    @spvs_sorted = @semester_process.semester_process_volunteers.sort { |spv1, spv2| spv1.volunteer.contact.full_name <=> spv2.volunteer.contact.full_name}
17
18
19
20
21
22
23
24
    if EmailTemplate.half_year_process_email.active.any?
      template = EmailTemplate.half_year_process_email.active.first.slice(:subject, :body)
      @semester_process.assign_attributes(mail_body_template: template[:body], mail_subject_template: template[:subject])
    else
      redirect_to new_email_template_path,
        notice: 'Sie müssen eine aktive E-Mailvorlage haben,
        bevor Sie eine Halbjahres Erinnerung erstellen können.'
    end
25
26
27
28
29
  end

  def edit; end

  def create
30
    @semester_process = SemesterProcess.new(semester_process_params.slice(:semester))
31
    @semester_process.creator = current_user
32
33
    authorize @semester_process

34
35
36
37
38
    @semester_process.assign_attributes(
      mail_body_template:    semester_process_params[:body],
      mail_subject_template: semester_process_params[:subject]
    )

Jiri Strojil's avatar
Jiri Strojil committed
39
    @semester_process.build_semester_volunteers(@volunteers, selected_volunteers: selected_volunteers)
40
    @semester_process.build_volunteers_hours_feedbacks_and_mails
41

42
    if @semester_process.save
Jiri Strojil's avatar
Jiri Strojil committed
43
      redirect_to semester_process_volunteers_path, notice: 'Semester process was successfully created and emails delivered.'
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    else
      render :new
    end
  end

  def update
    if @semester_process.update(semester_process_params)
      redirect_to @semester_process, notice: 'Semester process was successfully updated.'
    else
      render :edit
    end
  end

  private

  def set_semester_process
    @semester_process = SemesterProcess.find(params[:id])
    authorize @semester_process
  end

64
65
  def set_semester
    @semester = Semester.new
66
67
68
69
70
71
72
73
74
    if params[:semester]
      @selected_semester = Semester.parse(params[:semester])
    else
      @selected_semester = @semester.previous
      params[:semester] = Semester.to_s(@selected_semester)
    end

    semester_form_param = Semester.parse(params[:semester_process]&.fetch(:semester))
    @volunteers = Volunteer.semester_process_eligible(semester_form_param || @selected_semester)
75
76
  end

77
78
79
80
81
82
  def selected_volunteers
    semester_process_params[:semester_process_volunteers_attributes]
      .select { |_key, value| value['selected'] == '1' }
      .to_h.map { |_key, value| value[:volunteer_id].to_i }
  end

83
  def semester_process_params
84
85
    params.require(:semester_process).permit(
      :semester,
86
87
88
      :kind,
      :subject,
      :body,
89
90
91
92
      semester_process_volunteers_attributes: [
        :volunteer_id, :selected
      ]
    )
93
94
  end
end