Module: Mongoid::Factory
Overview
Instantiates documents that came from the database.
Constant Summary collapse
- TYPE =
"_type".freeze
Instance Method Summary collapse
-
#build(klass, attributes = nil) ⇒ Document
Builds a new
Document
from the supplied attributes. -
#from_db(klass, attributes = nil, selected_fields = nil) ⇒ Document
Builds a new
Document
from the supplied attributes loaded from the database.
Instance Method Details
#build(klass, attributes = nil) ⇒ Document
Builds a new Document
from the supplied attributes.
19 20 21 22 23 24 25 26 27 |
# File 'lib/mongoid/factory.rb', line 19 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, selected_fields = nil) ⇒ Document
Builds a new Document
from the supplied attributes loaded from the database.
41 42 43 44 45 46 47 48 |
# File 'lib/mongoid/factory.rb', line 41 def from_db(klass, attributes = nil, selected_fields = nil) type = (attributes || {})[TYPE] if type.blank? klass.instantiate(attributes, selected_fields) else type.camelize.constantize.instantiate(attributes, selected_fields) end end |