123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Associations::DisableJoinsAssociationScope

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: ActiveRecord::Associations::AssociationScope
Defined in: activerecord/lib/active_record/associations/disable_joins_association_scope.rb

Constant Summary

AssociationScope - Inherited

INSTANCE

Class Method Summary

Instance Attribute Summary

Instance Method Summary

AssociationScope - Inherited

#add_constraints, #apply_scope, #eval_scope, #get_chain, #query_constraint, #redundant_join?, #scope, #join,
#last_chain_scope

Returns the scope with the conditions that tie the records to the owner, and the key conditions among them.

#next_chain_scope

Constructor Details

This class inherits a constructor from ActiveRecord::Associations::AssociationScope

Instance Method Details

#add_constraints(reflection, lookup, owner) (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/disable_joins_association_scope.rb', line 32

def add_constraints(reflection, lookup, owner)
  route = reflection.association_route
  key = route.destination_key.name
  scope = reflection.build_scope(reflection.aliased_table)
  predicate = scope.where(key => lookup.ids).where_clause.ast
  scope._where!(query_constraint(predicate))
  key_clause = scope.where_clause

  route.destination_fixed_values.each do |column, value|
    scope._where!(column => value)
  end
  type_clause = scope.where_clause - key_clause

  relation = reflection.klass.scope_for_association
  scope.merge!(
    relation.except(:select, :create_with, :includes, :preload, :eager_load, :joins, :left_outer_joins)
  )

  scope = reflection.constraints.inject(scope) do |memo, scope_chain_item|
    item = eval_scope(reflection, scope_chain_item, owner)
    scope.unscope!(*item.unscope_values)
    scope.where_clause += item.where_clause
    scope.order_values = item.order_values | scope.order_values
    scope.default_order_values = item.default_order_values | scope.default_order_values
    scope
  end
  reflection.keep_owner_conditions(scope, key_clause, type_clause)

  if order_rank(scope) < lookup.order_rank
    split_scope = DisableJoinsAssociationRelation.create(scope.model, key, lookup.ids)
    split_scope.where_clause += scope.where_clause
    split_scope
  else
    scope
  end
end

#order_rank(scope) (private)

How strongly a link is ordered: not at all, only by a default order, or by an explicit one. A link keeps the order of the previous one unless it is ordered at least as strongly, as an explicit order on any link of a joined chain takes over from the default ones.

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/disable_joins_association_scope.rb', line 73

def order_rank(scope)
  if !scope.order_values.empty?
    2
  elsif !scope.default_order_values.empty?
    1
  else
    0
  end
end

#scope(association, routes = nil)

[ GitHub ]

  
# File 'activerecord/lib/active_record/associations/disable_joins_association_scope.rb', line 10

def scope(association, routes = nil)
  source_reflection = association.reflection
  owner = association.owner
  unscoped = association.klass.unscoped
  routes ||= source_reflection.association_route_chain(origin_class: owner.class, destination_class: association.klass)
  reverse_chain = get_chain(source_reflection, association, unscoped.alias_tracker, routes).reverse

  first_route = reverse_chain.first.association_route
  lookup = Lookup.new(ids: [first_route.origin_key.value_of(owner)], order_rank: 0)

  reverse_chain.each_cons(2) do |reflection, next_reflection|
    records = add_constraints(reflection, lookup, owner)
    lookup = Lookup.new(
      ids: records.pluck(next_reflection.association_route.origin_key.name),
      order_rank: order_rank(records)
    )
  end

  add_constraints(reverse_chain.last, lookup, owner)
end