123456789_123456789_123456789_123456789_123456789_

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

Instance Method Details

#_synced(association)

::Set up the syncing of many to many foreign keys.

Examples:

::Set up the syncing.

Person._synced(association)

Parameters:

[ GitHub ]

  
# 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.

Examples:

::Set up the destroy syncing.

Person.synced_destroy(association)

Parameters:

Returns:

  • (Class)

    The class getting set up.

[ GitHub ]

  
# 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.

Examples:

::Set up the save syncing.

Person.synced_save(association)

Parameters:

Returns:

  • (Class)

    The class getting set up.

[ GitHub ]

  
# 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