aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/associations.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2010-03-06 21:30:44 -0800
committerwycats <wycats@gmail.com>2010-03-06 21:33:29 -0800
commit939291e8f2b68e11cde12f5f08886f8fca3ef2b2 (patch)
treea446fae590832948fe67db16f95feb5c353e3d82 /activerecord/lib/active_record/associations.rb
parent4bc2cbc3cfa231b3dea0dce38a6fa2723c7ed94d (diff)
downloadrails-939291e8f2b68e11cde12f5f08886f8fca3ef2b2.tar.gz
rails-939291e8f2b68e11cde12f5f08886f8fca3ef2b2.tar.bz2
rails-939291e8f2b68e11cde12f5f08886f8fca3ef2b2.zip
deleting repeated code
Signed-off-by: wycats <wycats@gmail.com>
Diffstat (limited to 'activerecord/lib/active_record/associations.rb')
-rwxr-xr-xactiverecord/lib/active_record/associations.rb24
1 files changed, 7 insertions, 17 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 57785b4c93..86edfc386d 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -1537,35 +1537,25 @@ module ActiveRecord
# has_one associated objects, according to the defined :dependent rule.
def configure_dependency_for_has_one(reflection)
if reflection.options.include?(:dependent)
- case reflection.options[:dependent]
- when :destroy
- method_name = "has_one_dependent_destroy_for_#{reflection.name}".to_sym
+ name = reflection.options[:dependent]
+ method_name = :"has_one_dependent_#{name}_for_#{reflection.name}"
+
+ case name
+ when :destroy, :delete
define_method(method_name) do
association = send(reflection.name)
association.destroy unless association.nil?
end
- before_destroy method_name
- when :delete
- method_name = "has_one_dependent_delete_for_#{reflection.name}".to_sym
- define_method(method_name) do
- # Retrieve the associated object and delete it. The retrieval
- # is necessary because there may be multiple associated objects
- # with foreign keys pointing to this object, and we only want
- # to delete the correct one, not all of them.
- association = send(reflection.name)
- association.delete unless association.nil?
- end
- before_destroy method_name
when :nullify
- method_name = "has_one_dependent_nullify_for_#{reflection.name}".to_sym
define_method(method_name) do
association = send(reflection.name)
association.update_attribute(reflection.primary_key_name, nil) unless association.nil?
end
- before_destroy method_name
else
raise ArgumentError, "The :dependent option expects either :destroy, :delete or :nullify (#{reflection.options[:dependent].inspect})"
end
+
+ before_destroy method_name
end
end