提交 d88caa6e 编写于 作者: J Jon Leighton 提交者: Aaron Patterson

Refactor the code for singular association constructors. This will allow me to...

Refactor the code for singular association constructors. This will allow me to define a create_association! method in a minute.
上级 f4a88e81
......@@ -1132,8 +1132,7 @@ def has_one(association_id, options = {})
reflection = create_has_one_through_reflection(association_id, options)
else
reflection = create_has_one_reflection(association_id, options)
association_constructor_method(:build, reflection)
association_constructor_method(:create, reflection)
association_constructor_methods(reflection)
configure_dependency_for_has_one(reflection)
end
association_accessor_methods(reflection)
......@@ -1256,8 +1255,7 @@ def belongs_to(association_id, options = {})
association_accessor_methods(reflection)
unless reflection.options[:polymorphic]
association_constructor_method(:build, reflection)
association_constructor_method(:create, reflection)
association_constructor_methods(reflection)
end
add_counter_cache_callbacks(reflection) if options[:counter_cache]
......@@ -1533,10 +1531,17 @@ def collection_accessor_methods(reflection, writer = true)
end
end
def association_constructor_method(constructor, reflection)
redefine_method("#{constructor}_#{reflection.name}") do |*params|
attributes = params.first unless params.empty?
association_proxy(reflection.name).send(constructor, attributes)
def association_constructor_methods(reflection)
constructors = {
"build_#{reflection.name}" => "build",
"create_#{reflection.name}" => "create"
}
constructors.each do |name, proxy_name|
redefine_method(name) do |*params|
attributes = params.first unless params.empty?
association_proxy(reflection.name).send(proxy_name, attributes)
end
end
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册