123456789_123456789_123456789_123456789_123456789_

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

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: activerecord/lib/active_record/associations/preloader/batch.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(preloaders, available_records:) ⇒ Batch

[ GitHub ]

  
# 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 Attribute Details

#loaders (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/batch.rb', line 38

attr_reader :loaders

Instance Method Details

#call

[ GitHub ]

  
# 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

#group_and_load_similar(loaders) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/batch.rb', line 40

def group_and_load_similar(loaders)
  loaders.grep_v(ThroughAssociation).group_by(&:loader_query).each_pair do |query, similar_loaders|
    query.load_records_in_batch(similar_loaders)
  end
end