class Ecs::SaveGraduationCourseSupportsService < ApplicationService
  include AcceptsNestedAttributesHelper

  attr_reader :graduation_subitem, :params

  def initialize(graduation_subitem, params)
    @params             = params
    @graduation_subitem = graduation_subitem
  end

  def call
    Ecs::SaveGraduationCourseSupportForm.new(params).validate!

    accepts_attributes = build_accepts_nested_attributes(
      graduation_subitem, graduation_subitem.ec_course_supports,
      params[:course_supports]
    )

    graduation_subitem.assign_attributes(ec_course_supports_attributes: accepts_attributes)
    graduation_subitem.save!
    graduation_subitem
  end
end