Module: Mongoid::Association::Referenced::Syncable
| Relationships & Source Files | |
| Namespace Children | |
| Modules: | |
| Extension / Inclusion / Inheritance Descendants | |
| Included In: | |
| Defined in: | lib/mongoid/association/referenced/syncable.rb | 
Overview
This module handles the behavior for synchronizing foreign keys between both sides of a many to many associations.
Instance Method Summary
- 
    
      #_syncable?(association)  ⇒ true | false 
    
    Is the document able to be synced on the inverse side? This is only if the key has changed and the association 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? 
- 
    
      #remove_inverse_keys(association)  ⇒ Object 
    
    Update the inverse keys on destroy. 
- 
    
      #update_inverse_keys(association)  ⇒ Object 
    
    Update the inverse keys for the association. 
Instance Method Details
    #_syncable?(association)  ⇒ true | false 
  
Is the document able to be synced on the inverse side? This is only if the key has changed and the association bindings have not been run.
# File 'lib/mongoid/association/referenced/syncable.rb', line 21
def _syncable?(association) !_synced?(association.foreign_key) && send(association.foreign_key_check) end
#_synced ⇒ Hash
Get the synced foreign keys.
# File 'lib/mongoid/association/referenced/syncable.rb', line 31
def _synced @_synced ||= {} end
    #_synced?(foreign_key)  ⇒ true | false 
  
Has the document been synced for the foreign key?
# File 'lib/mongoid/association/referenced/syncable.rb', line 43
def _synced?(foreign_key) !!_synced[foreign_key] end
    #remove_inverse_keys(association)  ⇒ Object 
  
Update the inverse keys on destroy.
# File 'lib/mongoid/association/referenced/syncable.rb', line 55
def remove_inverse_keys(association) foreign_keys = send(association.foreign_key) unless foreign_keys.nil? || foreign_keys.empty? association.criteria(self, foreign_keys).pull(association.inverse_foreign_key => _id) end end
    #update_inverse_keys(association)  ⇒ Object 
  
Update the inverse keys for the association.
# File 'lib/mongoid/association/referenced/syncable.rb', line 70
def update_inverse_keys(association) if previous_changes.has_key?(association.foreign_key) old, new = previous_changes[association.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 $push and $each 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 association.autosave? send(association.name).in_memory.each do |doc| adds.delete_one(doc._id) end end unless adds.empty? association.criteria(self, adds)..add_to_set(association.inverse_foreign_key => _id) end unless subs.empty? association.criteria(self, subs)..pull(association.inverse_foreign_key => _id) end end end