123456789_123456789_123456789_123456789_123456789_

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

Do not use. This class is for internal use only.
Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Association
Instance Chain:
self, Association
Inherits: ActiveRecord::Associations::Preloader::Association
Defined in: activerecord/lib/active_record/associations/preloader/through_association.rb

Class Method Summary

Association - Inherited

Instance Attribute Summary

Instance Method Summary

Constructor Details

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

Instance Attribute Details

#data_available?Boolean (readonly, private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/through_association.rb', line 65

def data_available?
  owners.all? { |owner| loaded?(owner) } ||
    through_preloaders.all?(&:run?) && source_preloaders.all?(&:run?)
end

Instance Method Details

#future_classes

[ GitHub ]

  
# File 'activerecord/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

#middle_records (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/through_association.rb', line 74

def middle_records
  through_records_by_owner.values.flatten
end

#preload_index (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/through_association.rb', line 98

def preload_index
  @preload_index ||= preloaded_records.each_with_object({}).with_index do |(record, result), index|
    result[record] = index
  end
end

#preloaded_records

[ GitHub ]

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

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

#records_by_owner

[ GitHub ]

  
# File 'activerecord/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_loaders

[ GitHub ]

  
# File 'activerecord/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

#source_preloaders (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/through_association.rb', line 70

def source_preloaders
  @source_preloaders ||= ActiveRecord::Associations::Preloader.new(records: middle_records, associations: source_reflection.name, scope: scope, associate_by_default: false).loaders
end

#source_records_by_owner (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/through_association.rb', line 90

def source_records_by_owner
  @source_records_by_owner ||= source_preloaders.map(&:records_by_owner).reduce(:merge)
end

#source_reflection (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/through_association.rb', line 86

def source_reflection
  reflection.source_reflection
end

#through_preloaders (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/through_association.rb', line 78

def through_preloaders
  @through_preloaders ||= ActiveRecord::Associations::Preloader.new(records: owners, associations: through_reflection.name, scope: through_scope, associate_by_default: false).loaders
end

#through_records_by_owner (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/through_association.rb', line 94

def through_records_by_owner
  @through_records_by_owner ||= through_preloaders.map(&:records_by_owner).reduce(:merge)
end

#through_reflection (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/through_association.rb', line 82

def through_reflection
  reflection.through_reflection
end

#through_scope (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/preloader/through_association.rb', line 104

def through_scope
  scope = through_reflection.klass.unscoped
  options = reflection.options

  return scope if options[:disable_joins]

  values = reflection_scope.values
  if annotations = values[:annotate]
    scope.annotate!(*annotations)
  end

  if options[:source_type]
    scope.where! reflection.foreign_type => options[:source_type]
  elsif !reflection_scope.where_clause.empty?
    scope.where_clause = reflection_scope.where_clause

    if includes = values[:includes]
      scope.includes!(source_reflection.name => includes)
    else
      scope.includes!(source_reflection.name)
    end

    if values[:references] && !values[:references].empty?
      scope.references_values |= values[:references]
    else
      scope.references!(source_reflection.table_name)
    end

    if joins = values[:joins]
      scope.joins!(source_reflection.name => joins)
    end

    if left_outer_joins = values[:left_outer_joins]
      scope.left_outer_joins!(source_reflection.name => left_outer_joins)
    end

    if scope.eager_loading? && order_values = values[:order]
      scope = scope.order(order_values)
    end
  end

  cascade_strict_loading(scope)
end