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

Inherits:
AssociationInclusion show all
Defined in:
lib/mongoid/association/eager_load/inclusion.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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>
]
} }

Instance Attribute Summary

Attributes inherited from AssociationInclusion

#association

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AssociationInclusion

for, #initialize

Constructor Details

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

Class Method Details

.for?(association) ⇒ true | false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Parameters:

Returns:

  • (true | false)

    Whether it handles it.



94
95
96
# 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) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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).



106
107
108
109
110
111
112
113
114
115
# 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