Class: Mongoid::Fields::Internal::ForeignKeys::Array
- Includes:
- Serializable
- Defined in:
- lib/mongoid/fields/internal/foreign_keys/array.rb
Overview
Defines the behaviour for array foreign key fields.
Instance Attribute Summary
Attributes included from Serializable
#default_val, #label, #localize, #name, #options
Instance Method Summary collapse
-
#add_atomic_changes(document, name, key, mods, new, old) ⇒ Object
Adds the atomic changes for this type of resizable field.
-
#object_id_field? ⇒ true, false
Is the field a BSON::ObjectId?.
-
#resizable? ⇒ true
Array fields are resizable.
-
#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, #selection, #type, #versioned?
Instance Method Details
#add_atomic_changes(document, name, key, mods, new, old) ⇒ Object
Adds the atomic changes for this type of resizable field.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 24 def add_atomic_changes(document, name, key, mods, new, old) pushes = (new || []) - (old || []) pulls = (old || []) - (new || []) if old.nil? mods[key] = pushes elsif !pushes.empty? && !pulls.empty? mods[key] = document.attributes[name] elsif !pushes.empty? document.atomic_array_add_to_sets[key] = pushes elsif !pulls.empty? document.atomic_array_pulls[key] = pulls end end |
#object_id_field? ⇒ true, false
Is the field a BSON::ObjectId?
46 47 48 49 |
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 46 def object_id_field? @object_id_field ||= .polymorphic? ? true : .klass.using_object_ids? end |
#resizable? ⇒ true
Array fields are resizable.
59 |
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 59 def resizable?; true; end |
#serialize(object) ⇒ Array
Serialize the object from the type defined in the model to a MongoDB compatible object to store.
72 73 74 |
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 72 def serialize(object) object ? constraint.convert(object) : [] end |