Commit 4355d89b authored by Jiri Strojil's avatar Jiri Strojil
Browse files

Validations for multiple feedbacks

parent 70d023c4
Pipeline #30805 passed with stage
in 36 minutes and 56 seconds
......@@ -5,29 +5,35 @@ class SemesterProcessVolunteersController < ApplicationController
include SemesterProcessVolunteerHelper
def review_semester; end
def review_semester
initialize_nested_objects
end
def submit_review
# you shall not pass
return if @semester_process_volunteer.commited_at
initialize_nested_objects
set_reviewed
assign_volunteer_attributes
build_nested_objects
@semester_process_volunteer.volunteer.validate_waive_and_bank = true
build_nested_objects
ActiveRecord::Base.transaction do
@semester_process_volunteer.save!
@volunteer.save!
@feedbacks.each(&:save!)
@hours.each(&:save!)
@nested_objects.each do |_key, hash|
hash.each { |_id, obj| obj.save! }
end
end
redirect_to review_semester_semester_process_volunteer_path(@semester_process_volunteer), notice: t('.success')
rescue ActiveRecord::RecordInvalid => exception
logger.error exception.message
null_reviewed
render :review_semester
end
......@@ -53,22 +59,6 @@ class SemesterProcessVolunteersController < ApplicationController
private
def build_nested_objects
@feedbacks, @hours = [], []
review_params[:semester_feedbacks_attributes].each do |_key, hash|
@feedbacks << SemesterFeedback.new(hash[:semester_feedback]
.merge({ author: current_user, semester_process_volunteer: @semester_process_volunteer }))
if hash[:hour][:hours]&.to_i.positive?
spv_mission = SemesterProcessVolunteerMission.find(hash[:hour][:spv_mission_id])
@hours << Hour.new(hash[:hour].merge({
volunteer: spv_mission.volunteer,
meeting_date: Time.zone.now,
semester_process_volunteer: @semester_process_volunteer
}))
end
end
end
def prepare_review
@semester_process_volunteer = SemesterProcessVolunteer.find(params[:id])
authorize @semester_process_volunteer
......
......@@ -5,6 +5,31 @@ module SemesterProcessVolunteerHelper
@semester_process_volunteer.commited_at = Time.zone.now
end
def initialize_nested_objects
@nested_objects = {}
@semester_process_volunteer.semester_process_volunteer_missions.each do |spvm|
@nested_objects[spvm.id.to_s] = { feedback: @semester_process_volunteer.semester_feedback_with_mission(spvm.mission) || SemesterFeedback.new }
end
@nested_objects
end
def build_nested_objects
review_params[:semester_feedbacks_attributes].each do |_key, hash|
spv_mission = SemesterProcessVolunteerMission.find(hash[:semester_feedback][:spv_mission_id])
@nested_objects[spv_mission.id.to_s][:feedback] = SemesterFeedback.new(hash[:semester_feedback].merge({
author: current_user, semester_process_volunteer: @semester_process_volunteer
}))
if hash[:hour][:hours].to_i.positive?
@nested_objects[spv_mission.id.to_s][:hours] = Hour.new(hash[:hour].merge({
volunteer: spv_mission.volunteer,
meeting_date: Time.zone.now,
semester_process_volunteer: @semester_process_volunteer
}))
end
end
end
def assign_volunteer_attributes
@volunteer.assign_attributes(review_params[:volunteer_attributes]
.slice(:waive, :bank, :iban))
......
......@@ -11,7 +11,7 @@
url: polymorphic_path([mission.volunteer, mission.polymorph_url_object]),
target: '_blank'
= spvm.simple_fields_for :semester_feedback, @semester_process_volunteer.semester_feedback_with_mission(mission) do |ff|
= spvm.simple_fields_for :semester_feedback, @nested_objects[spvm.object.id.to_s][:feedback] do |ff|
= ff.input :spv_mission_id, as: :hidden, input_html: { value: spvm.object.id }
- [:goals, :achievements, :future, :comments, :conversation].each do |field|
.row
......
Supports Markdown
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