From d88caa6e4a8f0f64601fc8bf07b61b682263d712 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Sun, 9 Jan 2011 18:49:32 +0000 Subject: Refactor the code for singular association constructors. This will allow me to define a create_association! method in a minute. --- activerecord/lib/active_record/associations.rb | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 45b68d5956..2e0f54e505 100644 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1132,8 +1132,7 @@ module ActiveRecord 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 @@ module ActiveRecord 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 @@ module ActiveRecord 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 -- cgit v1.2.3