mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
pass the preloader down so we only have to construct one
This commit is contained in:
parent
f34d46ab82
commit
9e60f0f630
7 changed files with 21 additions and 21 deletions
|
@ -93,7 +93,7 @@ module ActiveRecord
|
||||||
else
|
else
|
||||||
associations.flat_map { |association|
|
associations.flat_map { |association|
|
||||||
preloaders_on association, records, preload_scope
|
preloaders_on association, records, preload_scope
|
||||||
}.each(&:run)
|
}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -133,7 +133,9 @@ module ActiveRecord
|
||||||
def preloaders_for_one(association, records, scope)
|
def preloaders_for_one(association, records, scope)
|
||||||
grouped_records(association, records).flat_map do |reflection, klasses|
|
grouped_records(association, records).flat_map do |reflection, klasses|
|
||||||
klasses.map do |rhs_klass, rs|
|
klasses.map do |rhs_klass, rs|
|
||||||
preloader_for(reflection).new(rhs_klass, rs, reflection, scope)
|
loader = preloader_for(reflection).new(rhs_klass, rs, reflection, scope)
|
||||||
|
loader.run self
|
||||||
|
loader
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,13 +20,13 @@ module ActiveRecord
|
||||||
@loaded = false
|
@loaded = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run(preloader)
|
||||||
unless owners.first.association(reflection.name).loaded?
|
unless owners.first.association(reflection.name).loaded?
|
||||||
preload
|
preload(preloader)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def preload
|
def preload(preloader)
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def preloaded_records
|
def preloaded_records
|
||||||
associated_records_by_owner.values.flatten
|
@associated_records_by_owner.values.flatten
|
||||||
end
|
end
|
||||||
|
|
||||||
def loaded?
|
def loaded?
|
||||||
|
@ -82,7 +82,7 @@ module ActiveRecord
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def associated_records_by_owner
|
def associated_records_by_owner(preloader)
|
||||||
@loaded = true
|
@loaded = true
|
||||||
|
|
||||||
return @associated_records_by_owner if @associated_records_by_owner
|
return @associated_records_by_owner if @associated_records_by_owner
|
||||||
|
|
|
@ -9,8 +9,8 @@ module ActiveRecord
|
||||||
super.order(preload_scope.values[:order] || reflection_scope.values[:order])
|
super.order(preload_scope.values[:order] || reflection_scope.values[:order])
|
||||||
end
|
end
|
||||||
|
|
||||||
def preload
|
def preload(preloader)
|
||||||
associated_records_by_owner.each do |owner, records|
|
associated_records_by_owner(preloader).each do |owner, records|
|
||||||
association = owner.association(reflection.name)
|
association = owner.association(reflection.name)
|
||||||
association.loaded!
|
association.loaded!
|
||||||
association.target.concat(records)
|
association.target.concat(records)
|
||||||
|
|
|
@ -34,7 +34,7 @@ module ActiveRecord
|
||||||
# Once we have used the join table column (in super), we manually instantiate the
|
# Once we have used the join table column (in super), we manually instantiate the
|
||||||
# actual records, ensuring that we don't create more than one instances of the same
|
# actual records, ensuring that we don't create more than one instances of the same
|
||||||
# record
|
# record
|
||||||
def associated_records_by_owner
|
def associated_records_by_owner(preloader)
|
||||||
return @associated_records_by_owner if @associated_records_by_owner
|
return @associated_records_by_owner if @associated_records_by_owner
|
||||||
|
|
||||||
records = {}
|
records = {}
|
||||||
|
|
|
@ -4,7 +4,7 @@ module ActiveRecord
|
||||||
class HasManyThrough < CollectionAssociation #:nodoc:
|
class HasManyThrough < CollectionAssociation #:nodoc:
|
||||||
include ThroughAssociation
|
include ThroughAssociation
|
||||||
|
|
||||||
def associated_records_by_owner
|
def associated_records_by_owner(preloader)
|
||||||
return @associated_records_by_owner if @associated_records_by_owner
|
return @associated_records_by_owner if @associated_records_by_owner
|
||||||
|
|
||||||
records_by_owner = super
|
records_by_owner = super
|
||||||
|
|
|
@ -5,8 +5,8 @@ module ActiveRecord
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def preload
|
def preload(preloader)
|
||||||
associated_records_by_owner.each do |owner, associated_records|
|
associated_records_by_owner(preloader).each do |owner, associated_records|
|
||||||
record = associated_records.first
|
record = associated_records.first
|
||||||
|
|
||||||
association = owner.association(reflection.name)
|
association = owner.association(reflection.name)
|
||||||
|
|
|
@ -11,13 +11,12 @@ module ActiveRecord
|
||||||
reflection.source_reflection
|
reflection.source_reflection
|
||||||
end
|
end
|
||||||
|
|
||||||
def associated_records_by_owner
|
def associated_records_by_owner(preloader)
|
||||||
@loaded = true
|
@loaded = true
|
||||||
|
|
||||||
return @associated_records_by_owner if @associated_records_by_owner
|
return @associated_records_by_owner if @associated_records_by_owner
|
||||||
|
|
||||||
left_loader = Preloader.new
|
preloader.preload(owners,
|
||||||
left_loader.preload(owners,
|
|
||||||
through_reflection.name,
|
through_reflection.name,
|
||||||
through_scope)
|
through_scope)
|
||||||
|
|
||||||
|
@ -37,7 +36,6 @@ module ActiveRecord
|
||||||
|
|
||||||
middle_records = through_records.map { |(_,rec,_)| rec }.flatten
|
middle_records = through_records.map { |(_,rec,_)| rec }.flatten
|
||||||
|
|
||||||
preloader = Preloader.new
|
|
||||||
preloaders = preloader.preload(middle_records,
|
preloaders = preloader.preload(middle_records,
|
||||||
source_reflection.name,
|
source_reflection.name,
|
||||||
reflection_scope)
|
reflection_scope)
|
||||||
|
|
Loading…
Reference in a new issue