From 3053f52671164530b811238f86954cb96f6ebc16 Mon Sep 17 00:00:00 2001 From: Paco Guzman Date: Sun, 29 Jan 2012 15:24:03 +0100 Subject: Same method for has_many and has_one associations --- .../active_record/associations/builder/association.rb | 15 +++++++++++++++ .../lib/active_record/associations/builder/has_many.rb | 14 -------------- .../lib/active_record/associations/builder/has_one.rb | 18 ++---------------- 3 files changed, 17 insertions(+), 30 deletions(-) diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index 54e51b7f44..45d3f37089 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -65,5 +65,20 @@ module ActiveRecord::Associations::Builder ActiveSupport::Deprecation.warn msg end end + + def define_restrict_dependency_method + name = self.name + mixin.redefine_method(dependency_method_name) do + # has_many or has_one associations + if send(name).respond_to?(:exists?) ? send(name).exists? : !send(name).nil? + if dependent_restrict_raises? + raise ActiveRecord::DeleteRestrictionError.new(name) + else + errors.add(:base, :restrict_dependent_destroy, :model => name) + return false + end + end + end + end end end diff --git a/activerecord/lib/active_record/associations/builder/has_many.rb b/activerecord/lib/active_record/associations/builder/has_many.rb index 7d85f3fdb2..9ddfd433e4 100644 --- a/activerecord/lib/active_record/associations/builder/has_many.rb +++ b/activerecord/lib/active_record/associations/builder/has_many.rb @@ -53,20 +53,6 @@ module ActiveRecord::Associations::Builder end end - def define_restrict_dependency_method - name = self.name - mixin.redefine_method(dependency_method_name) do - if send(name).exists? - if dependent_restrict_raises? - raise ActiveRecord::DeleteRestrictionError.new(name) - else - errors.add(:base, :restrict_dependent_destroy, :model => name) - return false - end - end - end - end - def dependency_method_name "has_many_dependent_for_#{name}" end diff --git a/activerecord/lib/active_record/associations/builder/has_one.rb b/activerecord/lib/active_record/associations/builder/has_one.rb index 9187f111a2..bc8a212bee 100644 --- a/activerecord/lib/active_record/associations/builder/has_one.rb +++ b/activerecord/lib/active_record/associations/builder/has_one.rb @@ -40,10 +40,6 @@ module ActiveRecord::Associations::Builder end end - def dependency_method_name - "has_one_dependent_#{options[:dependent]}_for_#{name}" - end - def define_destroy_dependency_method name = self.name mixin.redefine_method(dependency_method_name) do @@ -53,18 +49,8 @@ module ActiveRecord::Associations::Builder alias :define_delete_dependency_method :define_destroy_dependency_method alias :define_nullify_dependency_method :define_destroy_dependency_method - def define_restrict_dependency_method - name = self.name - mixin.redefine_method(dependency_method_name) do - unless send(name).nil? - if dependent_restrict_raises? - raise ActiveRecord::DeleteRestrictionError.new(name) - else - errors.add(:base, :restrict_dependent_destroy, :model => name) - return false - end - end - end + def dependency_method_name + "has_one_dependent_#{options[:dependent]}_for_#{name}" end end end -- cgit v1.2.3