From 939291e8f2b68e11cde12f5f08886f8fca3ef2b2 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 6 Mar 2010 21:30:44 -0800 Subject: deleting repeated code Signed-off-by: wycats --- activerecord/lib/active_record/associations.rb | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'activerecord/lib') 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 -- cgit v1.2.3