You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/services/ecs/create_course_targets_servi...

31 lines
988 B

6 years ago
class Ecs::CreateCourseTargetsService < ApplicationService
include AcceptsNestedAttributesHelper
attr_reader :ec_course, :params
def initialize(ec_course, params)
@ec_course = ec_course
@params = params
end
def call
accepts_attributes = build_accepts_nested_attributes(
ec_course, ec_course.ec_course_targets,
params[:course_targets],
&method(:deal_course_target_attribute!) # 相当于方法后面写代码块,然后把参数传入方法: a(&method(:b)) == a { |c| b(c) }
)
ec_course.assign_attributes(ec_course_targets_attributes: accepts_attributes)
ec_course.save!
ec_course
end
private
def deal_course_target_attribute!(target)
target[:content] = target[:content].to_s.strip
target[:weight] = target[:weight].to_f.round(2)
target[:ec_graduation_subitem_course_targets_attributes] =
[{ ec_graduation_subitem_id: target.delete(:graduation_subitem_id) }]
end
end