Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Refactor define_restrict_dependency_method using reflection #4835

Merged
merged 1 commit into from

3 participants

@pacoguzman

We need to know if the reflection macro is equal to has_one in two lines so I'd cached in a variable

@isaacsanders

Is this still an issue?

@pacoguzman

@isaacsanders only a little refactor, IMHO reflects better what happening there

@isaacsanders isaacsanders commented on the diff
...lib/active_record/associations/builder/association.rb
@@ -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?

Is this the same as has_one_macro in the code below?

In this commit - 336ff8a

the code is splitted in two files has_one and has_many with that condition so IMHO is the same, in some later commits we merge that code in an unique place

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@carlosantoniodasilva carlosantoniodasilva merged commit 8405aad into rails:master
@carlosantoniodasilva

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
6 activerecord/lib/active_record/associations/builder/association.rb
@@ -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?

Is this the same as has_one_macro in the code below?

In this commit - 336ff8a

the code is splitted in two files has_one and has_many with that condition so IMHO is the same, in some later commits we merge that code in an unique place

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ 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
Something went wrong with that request. Please try again.