Module: Mongoid::Factory
Overview
Instantiates documents that came from the database.
Instance Method Summary collapse
-
#build(klass, attributes = nil, options = {}) ⇒ Document
Builds a new
Document
from the supplied attributes. -
#from_db(klass, attributes = nil, criteria_instance_id = nil) ⇒ Document
Builds a new
Document
from the supplied attributes loaded from the database.
Instance Method Details
#build(klass, attributes = nil, options = {}) ⇒ Document
Builds a new Document
from the supplied attributes.
18 19 20 21 22 23 24 25 |
# 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) ⇒ Document
Builds a new Document
from the supplied attributes loaded from the database.
37 38 39 40 41 42 43 44 |
# 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 |