class Mongoid::Includes::Inclusion

Public: Represents a relation that needs to be eager loaded.

Public Class Methods

new(metadata, options = {}) click to toggle source
Calls superclass method
# File lib/mongoid/includes/inclusion.rb, line 10
def initialize(metadata, options = {})
  super(metadata)
  @options = options
end

Public Instance Methods

eql?(other) click to toggle source

Public: Checks if the collection already has an inclusion with the specified metadata.

# File lib/mongoid/includes/inclusion.rb, line 22
def eql?(other)
  metadata == other && (!other.respond_to?(:from) || from == other.from)
end
for_class_name(class_name) click to toggle source

Public: Clones the inclusion and changes the Mongoid::Metadata::Relation that it wraps to make it non polymorphic and target a particular class.

Returns an Inclusion that can be eager loaded as usual.

# File lib/mongoid/includes/inclusion.rb, line 55
def for_class_name(class_name)
  Inclusion.new metadata.clone.instance_eval { |relation_metadata|
    @options[:class_name] = @class_name = class_name
    @options[:polymorphic], @options[:as], @polymorphic, @klass = nil
    self
  }, with: @modifier
end
from() click to toggle source

Public: Name of the relation from which a nested inclusion is performed.

# File lib/mongoid/includes/inclusion.rb, line 32
def from
  @from ||= @options[:from]
end
load_documents_for(foreign_key, foreign_key_values) click to toggle source

Public: Preloads the documents for the relation. Uses a custom block if one was provided, or fetches them using the class and the foreign key.

# File lib/mongoid/includes/inclusion.rb, line 43
def load_documents_for(foreign_key, foreign_key_values)
  # NOTE: We assume the upstream code in Mongoid is removing nil keys.
  return klass.none if foreign_key_values.empty?

  docs = klass.any_in(foreign_key => foreign_key_values)
  modifier ? modifier.call(docs) : docs
end
modifier() click to toggle source

Internal: Proc that will modify the documents to include in the relation.

# File lib/mongoid/includes/inclusion.rb, line 37
def modifier
  @modifier ||= @options[:with]
end
nested?() click to toggle source

Public: Returns true if the relation is not direct.

# File lib/mongoid/includes/inclusion.rb, line 16
def nested?
  !!from
end
polymorphic_belongs_to?() click to toggle source

Public: Returns true if the relation is a polymorphic belongs_to.

# File lib/mongoid/includes/inclusion.rb, line 27
def polymorphic_belongs_to?
  metadata.polymorphic? && metadata.relation == Mongoid::Association::Referenced::BelongsTo::Proxy
end