1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Same method for has_many and has_one associations

This commit is contained in:
Paco Guzman 2012-01-29 15:24:03 +01:00 committed by Manoj
parent 38ffca55ab
commit 3053f52671
3 changed files with 17 additions and 30 deletions

View file

@ -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

View file

@ -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

View file

@ -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