class Ecs::SaveCourseEvaluationService < ApplicationService include AcceptsNestedAttributesHelper attr_reader :ec_course, :course_evaluation, :params def initialize(course_evaluation, params) @course_evaluation = course_evaluation @ec_course = course_evaluation.ec_course @params = params end def call Ecs::SaveCourseEvaluationForm.new(params).validate! subitem_attributes = build_accepts_nested_attributes( course_evaluation, course_evaluation.ec_course_evaluation_subitems, params.delete(:course_evaluation_subitems), &method(:course_evaluation_subitem_param_handler!) ) course_evaluation.assign_attributes(params) course_evaluation.assign_attributes(ec_course_evaluation_subitems_attributes: subitem_attributes) course_evaluation.save! course_evaluation end private def course_evaluation_subitem_param_handler!(item) item[:name] = item[:name].to_s.strip end end