Class: Mongoid::Association::EagerLoad::PolymorphicPreloader Private

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/association/eager_load/polymorphic_preloader.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.

Resolves a polymorphic belongs_to onto already-materialized root documents.

A polymorphic belongs_to can't be expressed as a $lookup: its target collection varies per document. So once the roots are materialized, the foreign keys are grouped by type, PolymorphicTargets resolves the documents for those keys, and the result is set on each document.

Instance Method Summary collapse

Constructor Details

#initialize(association, root_class) ⇒ PolymorphicPreloader

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.

Returns a new instance of PolymorphicPreloader.



17
18
19
20
# File 'lib/mongoid/association/eager_load/polymorphic_preloader.rb', line 17

def initialize(association, root_class)
  @association = association
  @root_class = root_class
end

Instance Method Details

#preload_into(documents) ⇒ 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.

Resolve and assign the polymorphic target on each of the documents.



25
26
27
28
# File 'lib/mongoid/association/eager_load/polymorphic_preloader.rb', line 25

def preload_into(documents)
  targets = PolymorphicTargets.for(@association, keys_by_type(documents), @root_class)
  assign(documents, targets)
end