Module: Mongoid::Association::Referenced::Syncable

Included in:
Mongoid::Association
Defined in:
build/mongoid-master/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.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

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.

Examples:

Are the foreign keys syncable?

document._syncable?(association)

Parameters:

  • association (Association)

    The association metadata.

Returns:

  • (true, false)

    If we can sync.



20
21
22
# File 'build/mongoid-master/lib/mongoid/association/referenced/syncable.rb', line 20

def _syncable?(association)
  !_synced?(association.foreign_key) && send(association.foreign_key_check)
end

#_syncedHash

Get the synced foreign keys.

Examples:

Get the synced foreign keys.

document._synced

Returns:

  • (Hash)

    The synced foreign keys.



30
31
32
# File 'build/mongoid-master/lib/mongoid/association/referenced/syncable.rb', line 30

def _synced
  @_synced ||= {}
end

#_synced?(foreign_key) ⇒ true, false

Has the document been synced for the foreign key?

Examples:

Has the document been synced?

document._synced?

Parameters:

  • foreign_key (String)

    The foreign key.

Returns:

  • (true, false)

    If we can sync.



42
43
44
# File 'build/mongoid-master/lib/mongoid/association/referenced/syncable.rb', line 42

def _synced?(foreign_key)
  !!_synced[foreign_key]
end

#remove_inverse_keys(association) ⇒ Object

Update the inverse keys on destroy.

Examples:

Update the inverse keys.

document.remove_inverse_keys(association)

Parameters:

Returns:

  • (Object)

    The updated values.



54
55
56
57
58
59
# File 'build/mongoid-master/lib/mongoid/association/referenced/syncable.rb', line 54

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.

Examples:

Update the inverse keys

document.update_inverse_keys(association)

Parameters:

  • association (Association)

    The document association.

Returns:

  • (Object)

    The updated values.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'build/mongoid-master/lib/mongoid/association/referenced/syncable.rb', line 69

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).without_options.add_to_set(association.inverse_foreign_key => _id)
    end
    unless subs.empty?
      association.criteria(self, subs).without_options.pull(association.inverse_foreign_key => _id)
    end
  end
end