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

Merge pull request #41754 from jhawthorn/branch_memoize_polymorphic

Simplify and memoize Preloader::Branch#polymorphic?
This commit is contained in:
Rafael França 2021-03-24 16:12:24 -04:00 committed by GitHub
commit bcf3752247
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -39,9 +39,10 @@ module ActiveRecord
def grouped_records
h = {}
polymorphic_parent = !root? && parent.polymorphic?
source_records.each do |record|
reflection = record.class._reflect_on_association(association)
next if polymorphic_parent? && !reflection || !record.association(association).klass
next if polymorphic_parent && !reflection || !record.association(association).klass
(h[reflection] ||= []) << record
end
h
@ -53,17 +54,13 @@ module ActiveRecord
end
end
def polymorphic_parent?
return false if root?
parent.polymorphic?
end
def polymorphic?
return false if root?
return @polymorphic if defined?(@polymorphic)
grouped_records.keys.any? do |reflection|
reflection.options[:polymorphic]
@polymorphic = source_records.any? do |record|
reflection = record.class._reflect_on_association(association)
reflection && reflection.options[:polymorphic]
end
end