module Mongoid::Factory
Instantiates documents that came from the database.
Public Instance Methods
build(klass, attributes = nil)
click to toggle source
Builds a new Document
from the supplied attributes.
@example Build the document.
Mongoid::Factory.build(Person, { "name" => "Durran" })
@param [ Class ] klass The class to instantiate from if _type is not present. @param [ Hash ] attributes The document attributes. @param [ Hash ] options The mass assignment scoping options.
@return [ Document
] The instantiated document.
# File lib/mongoid/factory.rb, line 18 def build(klass, attributes = nil) type = (attributes || {})["_type"] if type && klass._types.include?(type) type.constantize.new(attributes) else klass.new(attributes) end end
from_db(klass, attributes = nil, criteria_instance_id = nil)
click to toggle source
Builds a new Document
from the supplied attributes loaded from the database.
@example Build the document.
Mongoid::Factory.from_db(Person, { "name" => "Durran" })
@param [ Class ] klass The class to instantiate from if _type is not present. @param [ Hash ] attributes The document attributes.
@return [ Document
] The instantiated document.
# File lib/mongoid/factory.rb, line 37 def from_db(klass, attributes = nil, criteria_instance_id = nil) type = (attributes || {})["_type"] if type.blank? klass.instantiate(attributes, criteria_instance_id) else type.camelize.constantize.instantiate(attributes, criteria_instance_id) end end