提交 6e5a2cb9 编写于 作者: A Aaron Patterson

remove state from the preloader

上级 5b432811
...@@ -46,8 +46,6 @@ class Preloader #:nodoc: ...@@ -46,8 +46,6 @@ class Preloader #:nodoc:
autoload :BelongsTo, 'active_record/associations/preloader/belongs_to' autoload :BelongsTo, 'active_record/associations/preloader/belongs_to'
end end
attr_reader :records, :associations, :preload_scope, :model
# Eager loads the named associations for the given Active Record record(s). # Eager loads the named associations for the given Active Record record(s).
# #
# In this description, 'association name' shall refer to the name passed # In this description, 'association name' shall refer to the name passed
...@@ -82,28 +80,20 @@ class Preloader #:nodoc: ...@@ -82,28 +80,20 @@ class Preloader #:nodoc:
# [ :books, :author ] # [ :books, :author ]
# { author: :avatar } # { author: :avatar }
# [ :books, { author: :avatar } ] # [ :books, { author: :avatar } ]
def initialize(records, associations, preload_scope = nil)
@records = Array.wrap(records).compact.uniq
@associations = Array.wrap(associations)
@preload_scope = preload_scope || NULL_RELATION
@preloaders = nil
end
NULL_RELATION = Struct.new(:values).new({}) NULL_RELATION = Struct.new(:values).new({})
def run def preload(records, associations, preload_scope = nil)
preloaders.each(&:run) records = Array.wrap(records).compact.uniq
end associations = Array.wrap(associations)
preload_scope = preload_scope || NULL_RELATION
def preloaders
return @preloaders if @preloaders
if records.empty? if records.empty?
@preloaders = [] []
else else
@preloaders = 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
......
...@@ -16,10 +16,10 @@ def associated_records_by_owner ...@@ -16,10 +16,10 @@ def associated_records_by_owner
return @associated_records_by_owner if @associated_records_by_owner return @associated_records_by_owner if @associated_records_by_owner
left_loader = Preloader.new(owners, left_loader = Preloader.new
left_loader.preload(owners,
through_reflection.name, through_reflection.name,
through_scope) through_scope)
left_loader.run
should_reset = (through_scope != through_reflection.klass.unscoped) || should_reset = (through_scope != through_reflection.klass.unscoped) ||
(reflection.options[:source_type] && through_reflection.collection?) (reflection.options[:source_type] && through_reflection.collection?)
...@@ -37,13 +37,12 @@ def associated_records_by_owner ...@@ -37,13 +37,12 @@ def associated_records_by_owner
middle_records = through_records.map { |(_,rec,_)| rec }.flatten middle_records = through_records.map { |(_,rec,_)| rec }.flatten
preloader = Preloader.new(middle_records, preloader = Preloader.new
preloaders = preloader.preload(middle_records,
source_reflection.name, source_reflection.name,
reflection_scope) reflection_scope)
preloader.run middle_to_pl = preloaders.each_with_object({}) do |pl,h|
middle_to_pl = preloader.preloaders.each_with_object({}) do |pl,h|
pl.owners.each { |middle| pl.owners.each { |middle|
h[middle] = pl h[middle] = pl
} }
......
...@@ -600,7 +600,8 @@ def exec_queries ...@@ -600,7 +600,8 @@ def exec_queries
preload = preload_values preload = preload_values
preload += includes_values unless eager_loading? preload += includes_values unless eager_loading?
preload.each do |associations| preload.each do |associations|
ActiveRecord::Associations::Preloader.new(@records, associations).run pl = ActiveRecord::Associations::Preloader.new
pl.preload @records, associations
end end
@records.each { |record| record.readonly! } if readonly_value @records.each { |record| record.readonly! } if readonly_value
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册