Class: Mongoid::Association::EagerLoad::PolymorphicTargets Private

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

The targets of a polymorphic belongs_to, indexed as { type => { primary_key => document } }. Each subclass reaches the types that live in one place (the root's database or elsewhere); .for resolves the whole set, routing each type to the subclass that can reach it.

Direct Known Subclasses

OtherDatabaseTargets, SameDatabaseTargets

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association, keys_by_type) ⇒ PolymorphicTargets

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



43
44
45
46
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 43

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

Class Method Details

.for(association, keys_by_type, root_class) ⇒ Hash

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 every polymorphic target for the foreign keys grouped by type. The types whose documents share the root's database are fetched together in one $facet; those living elsewhere are read through their own models.

Parameters:

  • association (Mongoid::Association::Relatable)

    The polymorphic inclusion.

  • keys_by_type (Hash)

    The foreign keys grouped by type.

  • root_class (Class)

    The class being queried.

Returns:

  • (Hash)

    The targets, as { type => { primary_key => document } }.



23
24
25
26
27
28
29
30
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 23

def for(association, keys_by_type, root_class)
  here, elsewhere = keys_by_type.partition do |type, _keys|
    in_root_database?(association, type, root_class)
  end
  same_database = SameDatabaseTargets.new(association, here.to_h, root_class)
  other_databases = OtherDatabaseTargets.new(association, elsewhere.to_h)
  same_database.fetch.merge(other_databases.fetch)
end

Instance Method Details

#fetchHash

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 The targets, as { type => { primary_key => document } }.

Returns:

  • (Hash)

    The targets, as { type => { primary_key => document } }.

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/mongoid/association/eager_load/polymorphic_targets.rb', line 49

def fetch
  raise NotImplementedError
end