Class: ActiveRecord::Associations::Preloader::Batch

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/associations/preloader/batch.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(preloaders, available_records:) ⇒ Batch

Returns a new instance of Batch.



7
8
9
10
# File 'activerecord/lib/active_record/associations/preloader/batch.rb', line 7

def initialize(preloaders, available_records:)
  @preloaders = preloaders.reject(&:empty?)
  @available_records = available_records.flatten.group_by { |r| r.class.base_class }
end

Instance Method Details

#callObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'activerecord/lib/active_record/associations/preloader/batch.rb', line 12

def call
  branches = @preloaders.flat_map(&:branches)
  until branches.empty?
    loaders = branches.flat_map(&:runnable_loaders)

    loaders.each { |loader| loader.associate_records_from_unscoped(@available_records[loader.klass.base_class]) }

    if loaders.any?
      future_tables = branches.flat_map do |branch|
        branch.future_classes - branch.runnable_loaders.map(&:klass)
      end.map(&:table_name).uniq

      target_loaders = loaders.reject { |l| future_tables.include?(l.table_name)  }
      target_loaders = loaders if target_loaders.empty?

      group_and_load_similar(target_loaders)
      target_loaders.each(&:run)
    end

    finished, in_progress = branches.partition(&:done?)

    branches = in_progress + finished.flat_map(&:children)
  end
end