Skip to content

Commit

Permalink
Merge pull request rails#4835 from pacoguzman/refactor_define_restric…
Browse files Browse the repository at this point in the history
…t_dependency_method

Refactor define_restrict_dependency_method using reflection
  • Loading branch information
carlosantoniodasilva committed May 19, 2012
2 parents f20032f + 1596878 commit 8405aad
Showing 1 changed file with 3 additions and 3 deletions.
Expand Up @@ -69,12 +69,12 @@ def dependent_restrict_deprecation_warning
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?
has_one_macro = association(name).reflection.macro == :has_one
if has_one_macro ? !send(name).nil? : send(name).exists?
if dependent_restrict_raises?
raise ActiveRecord::DeleteRestrictionError.new(name)
else
key = association(name).reflection.macro == :has_one ? "one" : "many"
key = has_one_macro ? "one" : "many"
errors.add(:base, :"restrict_dependent_destroy.#{key}",
:record => self.class.human_attribute_name(name).downcase)
return false
Expand Down

0 comments on commit 8405aad

Please sign in to comment.