Simplify and memoize polymorphic?

This commit is contained in:
John Hawthorn 2021-03-24 11:25:19 -07:00
parent 7702ce8243
commit df15d36970
1 changed files with 6 additions and 9 deletions

View File

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