From 9b173685494b062b3a8e1402d9034e874393e87f Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 6 Mar 2010 21:30:44 -0800 Subject: [PATCH] deleting repeated code --- activerecord/lib/active_record/associations.rb | 24 +++++++----------------- 1 files changed, 7 insertions(+), 17 deletions(-) diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index 57785b4..86edfc3 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 -- 1.6.6