Module: Mongoid::Association::Referenced::Syncable::ClassMethods
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Extended In:
| |
Defined in: | lib/mongoid/association/referenced/syncable.rb |
Instance Method Summary
-
#_synced(association)
::Set
up the syncing of many to many foreign keys. -
#synced_destroy(association) ⇒ Class
private
::Set
up the sync of inverse keys that needs to happen on a destroy. -
#synced_save(association) ⇒ Class
private
::Set
up the sync of inverse keys that needs to happen on a save.
Instance Method Details
#_synced(association)
::Set
up the syncing of many to many foreign keys.
# File 'lib/mongoid/association/referenced/syncable.rb', line 103
def _synced(association) unless association.forced_nil_inverse? synced_save(association) synced_destroy(association) end end
#synced_destroy(association) ⇒ Class
(private)
::Set
up the sync of inverse keys that needs to happen on a destroy.
# File 'lib/mongoid/association/referenced/syncable.rb', line 143
def synced_destroy(association) set_callback( :destroy, :after ) do |doc| doc.remove_inverse_keys(association) end self end
#synced_save(association) ⇒ Class
(private)
::Set
up the sync of inverse keys that needs to happen on a save.
If the foreign key field has changed and the document is not synced, $addToSet the new ids, $pull the ones no longer in the array from the inverse side.
# File 'lib/mongoid/association/referenced/syncable.rb', line 124
def synced_save(association) set_callback( :save, :after, if: ->(doc) { doc._syncable?(association) } ) do |doc| doc.update_inverse_keys(association) end self end