Class: Mongoid::Fields::Internal::ForeignKeys::Object
- Includes:
- Serializable
- Defined in:
- lib/mongoid/fields/internal/foreign_keys/object.rb
Overview
Defines the behaviour for integer foreign key fields.
Instance Attribute Summary
Attributes included from Serializable
#default_val, #label, #localize, #name, #options
Instance Method Summary collapse
-
#object_id_field? ⇒ true, false
Is the field a BSON::ObjectId?.
-
#serialize(object) ⇒ Array
Serialize the object from the type defined in the model to a MongoDB compatible object to store.
Methods included from Serializable
#constraint, #deserialize, #eval_default, #foreign_key?, #localized?, #metadata, #resizable?, #selection, #type, #versioned?
Instance Method Details
#object_id_field? ⇒ true, false
Is the field a BSON::ObjectId?
19 20 21 22 |
# File 'lib/mongoid/fields/internal/foreign_keys/object.rb', line 19 def object_id_field? @object_id_field ||= .polymorphic? ? true : .klass.using_object_ids? end |
#serialize(object) ⇒ Array
Serialize the object from the type defined in the model to a MongoDB compatible object to store.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mongoid/fields/internal/foreign_keys/object.rb', line 35 def serialize(object) return nil if object.blank? if object_id_field? constraint.convert(object) else case object when ::Array object.replace(object.map { |arg| serialize(arg) }) when ::Hash object.each_pair do |key, value| object[key] = serialize(value) end else .klass.fields["_id"].serialize(object) end end end |