aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-01-09 18:49:32 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2011-01-11 13:45:09 -0800
commitd88caa6e4a8f0f64601fc8bf07b61b682263d712 (patch)
tree9bfa6c2f5d9c93372b9bf3a76b11a4a7c82c86d6 /activerecord
parentf4a88e810f66bfde5e6148cf7c276c2d4087f7ca (diff)
downloadrails-d88caa6e4a8f0f64601fc8bf07b61b682263d712.tar.gz
rails-d88caa6e4a8f0f64601fc8bf07b61b682263d712.tar.bz2
rails-d88caa6e4a8f0f64601fc8bf07b61b682263d712.zip
Refactor the code for singular association constructors. This will allow me to define a create_association! method in a minute.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations.rb21
1 files changed, 13 insertions, 8 deletions
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