module Mongoid::Contextual::Eager

Attributes

eager_loaded[RW]

@attribute [rw] #eager_loaded Has the context been eager loaded?

Private Instance Methods

eager_load(docs) click to toggle source

Eager load the inclusions for the provided documents.

@api private

@example Eager load the inclusions.

context.eager_load(docs)

@param [ Array<Document> ] docs The docs returning from the db.

@return [ true ] Always true.

@since 3.0.0

# File lib/mongoid/contextual/eager.rb, line 22
def eager_load(docs)
  load_inclusions(docs)
  self.eager_loaded = true
end
eager_load_one(doc) click to toggle source

Eager load the inclusions for the provided document.

@api private

@example Eager load the inclusions.

context.eager_load(doc)

@param [ Document ] doc The doc returning from the db.

@return [ true ] Always true.

@since 3.0.16

# File lib/mongoid/contextual/eager.rb, line 39
def eager_load_one(doc)
  load_inclusions([ doc ])
  inclusions_loaded[doc.id] = true
end
eager_loadable?(document = nil) click to toggle source

Is this context able to be eager loaded?

@api private

@example Is the context eager loadable?

context.eager_loadable?

@example Is the single document eager loadable?

context.eager_loadable?(document)

@param [ Document ] document The single document to load for.

@return [ true, false ] If the context is able to be eager loaded.

@since 3.0.0

# File lib/mongoid/contextual/eager.rb, line 80
def eager_loadable?(document = nil)
  return false if criteria.inclusions.empty?
  document ? !inclusions_loaded?(document) : !eager_loaded
end
eager_loaded_ids(docs, metadata) click to toggle source

Get the ids that to be used to eager load documents.

@api private

@example Get the ids.

context.eager_load(docs, metadata)

@param [ Array<Document> ] docs The pre-loaded documents. @param [ Metadata ] metadata The relation metadata.

@return [ Array<Object> ] The ids.

@since 3.0.0

# File lib/mongoid/contextual/eager.rb, line 57
def eager_loaded_ids(docs, metadata)
  if metadata.stores_foreign_key?
    docs.flat_map{ |doc| doc.send(metadata.foreign_key) }
  else
    docs.map(&:id)
  end
end
inclusions_loaded() click to toggle source

Has a hash of individual documents that have had their relations reager loaded.

@api private

@example Get the documents with relations eager loaded.

context.inclusions_loaded

@return [ Hash ] The documents that have had eager loaded inclusions.

@since 3.0.16

# File lib/mongoid/contextual/eager.rb, line 96
def inclusions_loaded
  @inclusions_loaded ||= {}
end
inclusions_loaded?(document) click to toggle source

Has the document had its inclusions loaded?

@api private

@example Has the document had its inclusions loaded?

context.inclusions_loaded?(document)

@param [ Document ] document The document to check.

@return [ true, false ] If the document had it's inclusions loaded.

@since 3.0.16

# File lib/mongoid/contextual/eager.rb, line 112
def inclusions_loaded?(document)
  inclusions_loaded.has_key?(document.id)
end
load_inclusions(docs) click to toggle source

Eager load the inclusions for the provided documents.

@api private

@example Eager load the inclusions.

context.load_inclusions(docs)

@param [ Array<Document> ] docs The docs returning from the db.

@return [ true ] Always true.

@since 3.0.16

# File lib/mongoid/contextual/eager.rb, line 128
def load_inclusions(docs)
  criteria.inclusions.each do |metadata|
    metadata.eager_load(eager_loaded_ids(docs, metadata)) if !docs.empty?
  end
end
with_eager_loading(document) click to toggle source

If the provided document exists, eager load its dependencies or return nil.

@api private

@example Eager load if the document is not nil.

context.with_eager_loading(document)

@param [ Hash ] document The document from the database.

@return [ Document, nil ] The instantiated model document.

@since 3.0.0

# File lib/mongoid/contextual/eager.rb, line 147
def with_eager_loading(document)
  selecting do
    return nil unless document
    doc = Factory.from_db(klass, document, criteria.object_id)
    eager_load_one(doc) if eager_loadable?(doc)
    doc
  end
end