Module: Mongoid::Relations::Synchronization
- Extended by:
- ActiveSupport::Concern
- Included in:
- Mongoid::Relations
- Defined in:
- lib/mongoid/relations/synchronization.rb
Overview
This module handles the behaviour for synchronizing foreign keys between both sides of a many to many relations.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#remove_inverse_keys(meta) ⇒ Object
Update the inverse keys on destroy.
-
#syncable?(metadata) ⇒ true, false
Is the document able to be synced on the inverse side? This is only if the key has changed and the relation bindings have not been run.
-
#synced ⇒ Hash
Get the synced foreign keys.
-
#synced?(foreign_key) ⇒ true, false
Has the document been synced for the foreign key?.
-
#update_inverse_keys(meta) ⇒ Object
Update the inverse keys for the relation.
Instance Method Details
#remove_inverse_keys(meta) ⇒ Object
Update the inverse keys on destroy.
61 62 63 64 65 66 |
# File 'lib/mongoid/relations/synchronization.rb', line 61 def remove_inverse_keys() foreign_keys = send(.foreign_key) unless foreign_keys.nil? || foreign_keys.empty? .criteria(foreign_keys, self.class).pull(.inverse_foreign_key => id) end end |
#syncable?(metadata) ⇒ true, false
Is the document able to be synced on the inverse side? This is only if the key has changed and the relation bindings have not been run.
21 22 23 |
# File 'lib/mongoid/relations/synchronization.rb', line 21 def syncable?() !synced?(.foreign_key) && send(.foreign_key_check) end |
#synced ⇒ Hash
Get the synced foreign keys.
33 34 35 |
# File 'lib/mongoid/relations/synchronization.rb', line 33 def synced @synced ||= {} end |
#synced?(foreign_key) ⇒ true, false
Has the document been synced for the foreign key?
47 48 49 |
# File 'lib/mongoid/relations/synchronization.rb', line 47 def synced?(foreign_key) !!synced[foreign_key] end |
#update_inverse_keys(meta) ⇒ Object
Update the inverse keys for the relation.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/mongoid/relations/synchronization.rb', line 78 def update_inverse_keys() if changes.has_key?(.foreign_key) old, new = changes[.foreign_key] adds, subs = new - (old || []), (old || []) - new # If we are autosaving we don't want a duplicate to get added - the # $addToSet would run previously and then the $pushAll from the # inverse on the autosave would cause this. We delete each id from # what's in memory in case a mix of id addition and object addition # had occurred. if .autosave? send(.name).in_memory.each do |doc| adds.delete_one(doc.id) end end unless adds.empty? .criteria(adds, self.class)..add_to_set(.inverse_foreign_key => id) end unless subs.empty? .criteria(subs, self.class)..pull(.inverse_foreign_key => id) end end end |