Class: ActiveRecord::Associations::Preloader::ThroughAssociation

Inherits:
Association
  • Object
show all
Defined in:
lib/active_record/associations/preloader/through_association.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from Association

#klass

Instance Method Summary collapse

Methods inherited from Association

#associate_records_from_unscoped, #association_key_name, #initialize, #load_records, #loaded?, #loader_query, #owners_by_key, #run, #run?, #scope, #set_inverse, #table_name, #target_for

Constructor Details

This class inherits a constructor from ActiveRecord::Associations::Preloader::Association

Instance Method Details

#future_classesObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_record/associations/preloader/through_association.rb', line 49

def future_classes
  if run?
    []
  elsif through_preloaders.all?(&:run?)
    source_preloaders.flat_map(&:future_classes).uniq
  else
    through_classes = through_preloaders.flat_map(&:future_classes)
    source_classes = source_reflection.
      chain.
      reject { |reflection| reflection.respond_to?(:polymorphic?) && reflection.polymorphic? }.
      map(&:klass)
    (through_classes + source_classes).uniq
  end
end

#preloaded_recordsObject



7
8
9
# File 'lib/active_record/associations/preloader/through_association.rb', line 7

def preloaded_records
  @preloaded_records ||= source_preloaders.flat_map(&:preloaded_records)
end

#records_by_ownerObject



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

def records_by_owner
  @records_by_owner ||= owners.each_with_object({}) do |owner, result|
    if loaded?(owner)
      result[owner] = target_for(owner)
      next
    end

    through_records = through_records_by_owner[owner] || []

    if owners.first.association(through_reflection.name).loaded?
      if source_type = reflection.options[:source_type]
        through_records = through_records.select do |record|
          record[reflection.foreign_type] == source_type
        end
      end
    end

    records = through_records.flat_map do |record|
      source_records_by_owner[record]
    end

    records.compact!
    records.sort_by! { |rhs| preload_index[rhs] } if scope.order_values.any?
    records.uniq! if scope.distinct_value
    result[owner] = records
  end
end

#runnable_loadersObject



39
40
41
42
43
44
45
46
47
# File 'lib/active_record/associations/preloader/through_association.rb', line 39

def runnable_loaders
  if data_available?
    [self]
  elsif through_preloaders.all?(&:run?)
    source_preloaders.flat_map(&:runnable_loaders)
  else
    through_preloaders.flat_map(&:runnable_loaders)
  end
end