Class: Mongoid::Fields::ForeignKey
- Inherits:
-
Standard
- Object
- Standard
- Mongoid::Fields::ForeignKey
- Defined in:
- lib/mongoid/fields/foreign_key.rb
Instance Attribute Summary
Attributes inherited from Standard
#default_val, #label, #name, #options
Instance Method Summary (collapse)
-
- (Object) add_atomic_changes(document, name, key, mods, new_elements, old_elements)
Adds the atomic changes for this type of resizable field.
-
- (Object) evolve(object)
Evolve the object into an id compatible object.
-
- (true, false) foreign_key?
Is this field a foreign key?.
-
- (Object) mongoize(object)
Mongoize the object into the Mongo friendly value.
-
- (true, false) object_id_field?
Is the field a BSON::ObjectId?.
Methods inherited from Standard
#constraint, #demongoized_identity_for, #eval_default, #initialize, #localized?, #metadata, #pre_processed?, #type, #versioned?
Constructor Details
This class inherits a constructor from Mongoid::Fields::Standard
Instance Method Details
- (Object) add_atomic_changes(document, name, key, mods, new_elements, old_elements)
Adds the atomic changes for this type of resizable field.
@todo: Durran: Refactor, big time.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mongoid/fields/foreign_key.rb', line 21 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) evolve(object)
Evolve the object into an id compatible object.
64 65 66 67 68 |
# File 'lib/mongoid/fields/foreign_key.rb', line 64 def evolve(object) return object.id if object.is_a?(Document) evolved = mongoize(object) type.resizable? ? evolved.first : evolved end |
- (true, false) foreign_key?
Is this field a foreign key?
50 51 52 |
# File 'lib/mongoid/fields/foreign_key.rb', line 50 def foreign_key? true end |
- (Object) mongoize(object)
Mongoize the object into the Mongo friendly value.
80 81 82 83 84 85 86 |
# File 'lib/mongoid/fields/foreign_key.rb', line 80 def mongoize(object) if type.resizable? || object_id_field? type.__mongoize_fk__(constraint, object) else .klass.fields["_id"].mongoize(object) end end |
- (true, false) object_id_field?
Is the field a BSON::ObjectId?
96 97 98 99 |
# File 'lib/mongoid/fields/foreign_key.rb', line 96 def object_id_field? @object_id_field ||= .polymorphic? ? true : .klass.using_object_ids? end |