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_elements, old_elements) ⇒ 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_elements, old_elements) ⇒ Object
Adds the atomic changes for this type of resizable field.
@todo: Durran: Refactor, big time.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 26 def add_atomic_changes(document, name, key, mods, new_elements, old_elements) old = (old_elements || []) new = (new_elements || []) if new.length > old.length if new.first(old.length) == old document.atomic_array_add_to_sets[key] = new.drop(old.length) else mods[key] = document.attributes[name] end elsif new.length < old.length pulls = old - new if new == old - pulls document.atomic_array_pulls[key] = pulls else mods[key] = document.attributes[name] end elsif new != old mods[key] = document.attributes[name] end end |
#object_id_field? ⇒ true, false
Is the field a BSON::ObjectId?
55 56 57 58 |
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 55 def object_id_field? @object_id_field ||= .polymorphic? ? true : .klass.using_object_ids? end |
#resizable? ⇒ true
Array fields are resizable.
68 |
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 68 def resizable?; true; end |
#serialize(object) ⇒ Array
Serialize the object from the type defined in the model to a MongoDB compatible object to store.
81 82 83 |
# File 'lib/mongoid/fields/internal/foreign_keys/array.rb', line 81 def serialize(object) object ? constraint.convert(object) : [] end |