Module: Mongoid::Factory

Extended by:
Factory
Included in:
Factory
Defined in:
build/mongoid-7.1/lib/mongoid/factory.rb

Overview

Instantiates documents that came from the database.

Constant Summary collapse

TYPE =
"_type".freeze

Instance Method Summary collapse

Instance Method Details

#build(klass, attributes = nil) ⇒ Document

Builds a new Document from the supplied attributes.

Examples:

Build the document.

Mongoid::Factory.build(Person, { "name" => "Durran" })

Parameters:

  • klass (Class)

    The class to instantiate from if _type is not present.

  • attributes (Hash) (defaults to: nil)

    The document attributes.

Returns:

  • (Document)

    The instantiated document.



21
22
23
24
25
26
27
28
29
# File 'build/mongoid-7.1/lib/mongoid/factory.rb', line 21

def build(klass, attributes = nil)
  attributes ||= {}
  type = attributes[TYPE] || attributes[TYPE.to_sym]
  if type && klass._types.include?(type)
    type.constantize.new(attributes)
  else
    klass.new(attributes)
  end
end

#from_db(klass, attributes = nil, criteria = nil, selected_fields = nil) ⇒ Document

Builds a new Document from the supplied attributes loaded from the database.

If a criteria object is given, it is used in two ways:

  1. If the criteria has a list of fields specified via #only, only those fields are populated in the returned document.

  2. If the criteria has a referencing association (i.e., this document is being instantiated as an association of another document), the other document is also populated in the returned document’s reverse association, if one exists.

Examples:

Build the document.

Mongoid::Factory.from_db(Person, { "name" => "Durran" })

Parameters:

  • klass (Class)

    The class to instantiate from if _type is not present.

  • attributes (Hash) (defaults to: nil)

    The document attributes.

  • criteria (Criteria) (defaults to: nil)

    Optional criteria object.

  • selected_fields (Hash) (defaults to: nil)

    Fields which were retrieved via #only. If selected_fields are specified, fields not listed in it will not be accessible in the returned document.

Returns:

  • (Document)

    The instantiated document.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'build/mongoid-7.1/lib/mongoid/factory.rb', line 53

def from_db(klass, attributes = nil, criteria = nil, selected_fields = nil)
  if criteria
    selected_fields ||= criteria.options[:fields]
  end
  type = (attributes || {})[TYPE]
  if type.blank?
    obj = klass.instantiate(attributes, selected_fields)
    if criteria && criteria.association && criteria.parent_document
      obj.set_relation(criteria.association.inverse, criteria.parent_document)
    end
    obj
  else
    camelized = type.camelize

    # Check if the class exists
    begin
      constantized = camelized.constantize
    rescue NameError
      raise Errors::UnknownModel.new(camelized, type)
    end

    # Check if the class is a Document class
    if !constantized.respond_to?(:instantiate)
      raise Errors::UnknownModel.new(camelized, type)
    end

    constantized.instantiate(attributes, selected_fields)
  end
end