123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Association::EagerLoad::JoinedInclusion Private

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: Mongoid::Association::EagerLoad::AssociationInclusion
Defined in: lib/mongoid/association/eager_load/inclusion.rb

Overview

A referenced inclusion: contributes a $lookup whose sub-pipeline holds its own children. When it lives inside an embedded document (a non-empty chain), the $lookup is distributed onto that embedded path instead of standing at the top level.

For a has_many :albums it contributes:

{ '$lookup' => {
'from' => 'albums',
'localField' => '_id',        # the band's _id...
'foreignField' => 'band_id',  # ...matched against each album's band_id
'as' => 'albums',             # matches are written to this field
'pipeline' => [
  { '$sort' => {
    '_id' => 1
  } },
  <children>
]
} }

Class Method Summary

AssociationInclusion - Inherited

.for

Builds the right kind of inclusion for the association.

.for?

Whether this kind handles the given association.

.new

Instance Attribute Summary

Instance Method Summary

  • #contribute(destination, chain) Internal use only

    Append the $lookup, with the children in its sub-pipeline, to the destination; or distribute it onto the embedded path when nested in one.

Inclusion - Inherited

#contribute

Add this inclusion's stages to the destination.

Constructor Details

This class inherits a constructor from Mongoid::Association::EagerLoad::AssociationInclusion

Class Method Details

.for?(association) ⇒ true | false

The default kind: a referenced, non-polymorphic association, i.e. the one no sibling kind claims.

Parameters:

Returns:

  • (true | false)

    Whether it handles it.

[ GitHub ]

  
# File 'lib/mongoid/association/eager_load/inclusion.rb', line 94

def for?(association)
  (superclass.subclasses - [ self ]).none? { |kind| kind.for?(association) }
end

Instance Method Details

#contribute(destination, chain)

Append the $lookup, with the children in its sub-pipeline, to the destination; or distribute it onto the embedded path when nested in one.

Parameters:

  • destination (Array<Hash>)

    The pipeline (or sub-pipeline) the stages are appended to.

  • chain (Array<Mongoid::Association::Relatable>)

    The embedded path accumulated from the ancestors above this inclusion (empty at the top).

[ GitHub ]

  
# File 'lib/mongoid/association/eager_load/inclusion.rb', line 106

def contribute(destination, chain)
  stage = @pipeline.lookup_stage_for(@association)
  @children.each { |child| child.contribute(stage['$lookup']['pipeline'], []) }

  if chain.empty?
    destination << stage
  else
    destination.concat(@pipeline.distribute(@association, chain, stage))
  end
end