Class: Mongoid::Fields::ForeignKey
- Defined in:
- lib/mongoid/fields/foreign_key.rb
Overview
Represents a BSON document field definition which stores a foreign key that references the ID of another document. Used for association behavior.
Instance Attribute Summary
Attributes inherited from Standard
#default_val, #label, #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.
-
#evolve(object) ⇒ Object
Evolve the object into an id compatible object.
-
#foreign_key? ⇒ true | false
Is this field a foreign key?.
-
#lazy? ⇒ true | false
Does this field do lazy default evaluation?.
-
#mongoize(object) ⇒ Object
Mongoize the object into the Mongo friendly value.
-
#object_id_field? ⇒ true | false
Is the field a BSON::ObjectId?.
-
#resizable? ⇒ true | false
Returns true if an array, false if not.
Methods inherited from Standard
#association, #eval_default, #initialize, #localize_present?, #localized?, #pre_processed?, #type
Constructor Details
This class inherits a constructor from Mongoid::Fields::Standard
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: Refactor, big time.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/mongoid/fields/foreign_key.rb', line 25 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 |
#evolve(object) ⇒ Object
Evolve the object into an id compatible object.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/mongoid/fields/foreign_key.rb', line 64 def evolve(object) if object_id_field? || object.is_a?(Document) if association.polymorphic? association.convert_to_foreign_key(object) elsif object.is_a?(Document) && object.respond_to?(association.primary_key) primary_key_field.evolve(object.send(association.primary_key)) else object.__evolve_object_id__ end else .evolve(object) end end |
#foreign_key? ⇒ true | false
Is this field a foreign key?
52 53 54 |
# File 'lib/mongoid/fields/foreign_key.rb', line 52 def foreign_key? true end |
#lazy? ⇒ true | false
Does this field do lazy default evaluation?
84 85 86 |
# File 'lib/mongoid/fields/foreign_key.rb', line 84 def lazy? type.resizable? end |
#mongoize(object) ⇒ Object
Mongoize the object into the Mongo friendly value.
96 97 98 99 100 101 102 |
# File 'lib/mongoid/fields/foreign_key.rb', line 96 def mongoize(object) if type.resizable? || object_id_field? mongoize_foreign_key(object) else .mongoize(object) end end |
#object_id_field? ⇒ true | false
Is the field a BSON::ObjectId?
110 111 112 113 |
# File 'lib/mongoid/fields/foreign_key.rb', line 110 def object_id_field? @object_id_field ||= association.polymorphic? ? true : association.klass.using_object_ids? end |
#resizable? ⇒ true | false
Returns true if an array, false if not.
121 122 123 |
# File 'lib/mongoid/fields/foreign_key.rb', line 121 def resizable? type.resizable? end |