Commit 8ac41ab1 authored by Jiri Strojil's avatar Jiri Strojil

Ability to edit semester process

parent 6552b09f
Pipeline #31130 failed with stage
in 12 minutes and 19 seconds
......@@ -35,8 +35,9 @@ class SemesterProcessVolunteersController < ApplicationController
def index
authorize SemesterProcessVolunteer
@spvs = SemesterProcessVolunteer.index(Semester.parse(params[:semester])).page(params[:page])
semester = Semester.parse(params[:semester])
@spvs = SemesterProcessVolunteer.index(semester).page(params[:page])
@semester_process = SemesterProcess.find_by_semester(semester)
@spvs_sorted = @spvs.sort { |spv1, spv2| spv1.volunteer.contact.full_name <=> spv2.volunteer.contact.full_name}
end
......
......@@ -11,9 +11,32 @@ class SemesterProcessesController < ApplicationController
def new
@semester_process = SemesterProcess.new(semester: @selected_semester)
@semester_process.build_semester_volunteers(@volunteers)
new_or_edit
end
def edit
new_or_edit
end
def create
@semester_process = SemesterProcess.new(semester_process_params.slice(:semester))
update_or_create
end
def update
update_or_create
end
private
def new_or_edit
authorize @semester_process
@spvs_sorted = @semester_process.semester_process_volunteers.sort { |spv1, spv2| spv1.volunteer.contact.full_name <=> spv2.volunteer.contact.full_name}
@volunteers = Volunteer.semester_process_eligible(@semester_process.semester)
@semester_process.build_semester_volunteers(@volunteers, nil, false)
@spvs_sorted = @semester_process.new_semester_process_volunteers.sort { |spv1, spv2| spv1.volunteer.contact.full_name <=> spv2.volunteer.contact.full_name}
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])
......@@ -24,19 +47,17 @@ class SemesterProcessesController < ApplicationController
end
end
def edit; end
def create
@semester_process = SemesterProcess.new(semester_process_params.slice(:semester))
@semester_process.creator = current_user
def update_or_create
authorize @semester_process
@semester_process.creator = current_user
@semester_process.assign_attributes(
mail_body_template: semester_process_params[:body],
mail_subject_template: semester_process_params[:subject]
)
@semester_process.build_semester_volunteers(@volunteers, selected_volunteers)
@volunteers = Volunteer.semester_process_eligible(@semester_process.semester)
@semester_process.build_semester_volunteers(@volunteers, selected_volunteers, true)
@semester_process.build_volunteers_hours_feedbacks_and_mails
if @semester_process.save
......@@ -46,16 +67,6 @@ class SemesterProcessesController < ApplicationController
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
......@@ -69,9 +80,6 @@ class SemesterProcessesController < ApplicationController
@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)
end
def selected_volunteers
......
......@@ -16,6 +16,12 @@ class SemesterProcess < ApplicationRecord
has_many :semester_process_mails, through: :semester_process_volunteers
attr_accessor :new_semester_process_volunteers
scope :find_by_semester, lambda { |semester = nil|
where('semester && daterange(?,?)', semester.begin, semester.end).last
}
def mails
semester_process_mails.where(kind: 'mail')
end
......@@ -68,16 +74,19 @@ class SemesterProcess < ApplicationRecord
Semester.period(semester)
end
def build_semester_volunteers(volunteers, selected = nil)
volunteers = volunteers.select{ |volunteer| selected.include? volunteer.id } if selected && selected.any?
semester_process_volunteers << volunteers.to_a.map do |volunteer|
spv = SemesterProcessVolunteer.new(volunteer: volunteer, selected: false)
def build_semester_volunteers(volunteers, selected, save_record = true)
volunteers = volunteers.select{ |volunteer| selected.include? volunteer.id } if selected && selected.any?
@new_semester_process_volunteers = []
@new_semester_process_volunteers = volunteers.to_a.map do |volunteer|
spv = SemesterProcessVolunteer.new(volunteer: volunteer, semester_process: self, selected: false)
spv.build_missions(semester)
spv.save if save_record
spv
end
end
def build_volunteers_hours_feedbacks_and_mails
semester_process_volunteers.map(&:build_hours_and_mails)
@new_semester_process_volunteers.map(&:build_hours_and_mails)
end
end
......@@ -3,4 +3,5 @@ nav.navbar.section-navigation
ul.list-inline
li= button_link 'Neuen Semester Prozess erstellen', new_semester_process_path, dimension: 'sm'
li= render 'semester_filter'
li= button_link 'Semester Prozess bearbeiten', edit_semester_process_path(@semester_process), dimension: 'sm'
hr
......@@ -3,7 +3,8 @@
= f.input_field :kind, value: @semester_process.kind, as: :hidden
.row:.col-xs-12.col-md-4
= f.input :semester, collection: @semester.collection, selected: params[:semester], input_html: { class: 'semester-selector' }
= f.input :semester, collection: @semester&.collection || [Semester.i18n_t(@semester_process.semester, short: false)],
selected: params[:semester], input_html: { class: 'semester-selector' }, disabled: !@semester
.row
.col-xs-12.col-md-4
......@@ -20,14 +21,14 @@
th Freiwilliger/r
th Einsatz
tbody
= f.simple_fields_for :semester_process_volunteers, @spvs_sorted do |fv|
tr.table-row-selectable
td= fv.input :selected, as: :boolean, label: false
td= link_to fv.object.volunteer.contact.full_name, fv.object.volunteer
td.index-action-cell
= fv.hidden_field :volunteer_id
- fv.object.semester_process_volunteer_missions.each do |mission|
= link_to mission.mission.to_label, mission.mission
= f.simple_fields_for :semester_process_volunteers, @spvs_sorted do |fv|
tr.table-row-selectable
td= fv.input :selected, as: :boolean, label: false
td= link_to fv.object.volunteer.contact.full_name, fv.object.volunteer
td.index-action-cell
= fv.hidden_field :volunteer_id
- fv.object.semester_process_volunteer_missions.each do |mission|
= link_to mission.mission.to_label, mission.mission
.row:.col-xs-12
= f.button :submit
......@@ -2,5 +2,4 @@ h1= t_title(:edit)
= render 'form'
= form_navigation_btn :show
= form_navigation_btn :back
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment