Module: Pipeloader::Batch::LoadGrouping

Defined in:
lib/pipeloader/batch/load_grouping.rb

Overview

Prepended onto ActiveRecord::Relation: every set of records loaded by ONE query is stamped with a shared Context (its sibling group), carried on the records. One hook covers every path that matters — a root collection, the IN query a batched has_many runs, and the queries AR’s Preloader runs for a batched belongs_to / has_one all materialize through here. A record loaded on its own (find / find_by, one row) gets no group and falls back to a solo one (Model#_pipeloader_batch_context), which is correct: it has nothing to batch with.

Instance Method Summary collapse

Instance Method Details

#recordsObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pipeloader/batch/load_grouping.rb', line 13

def records
  recs = super
  if recs.length > 1 && klass.include?(Pipeloader::Batch::Model)
    group = Context.new
    recs.each do |record|
      next if record.instance_variable_defined?(:@_pipeloader_batch_context)

      record.instance_variable_set(:@_pipeloader_batch_context, group)
      group.add(record)
    end
  end
  recs
end