123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Association::Referenced::HasAndBelongsToMany::Binding

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb

Overview

Binding class for all has_and_belongs_to_many associations.

Instance Attribute Summary

::Mongoid::Association::Bindable - Included

::Mongoid::Threaded::Lifecycle - Included

#_assigning

Begin the assignment of attributes.

#_assigning?

Is the current thread in assigning mode?

#_binding

Execute a block in binding mode.

#_binding?

Is the current thread in binding mode?

#_building

Execute a block in building mode.

#_building?

Is the current thread in building mode?

#_creating?

Is the current thread in creating mode?

#_loading

Execute a block in loading mode.

#_loading?

Is the current thread in loading mode?

Instance Method Summary

::Mongoid::Association::Bindable - Included

#binding

Execute the provided block inside a binding.

#initialize

Create the new binding.

#bind_foreign_key

::Set the id of the related document in the foreign key field on the keyed document.

#bind_from_relational_parent

Bind the provided document with the base from the parent association.

#bind_inverse

Bind the inverse document to the child document so that the in memory instances are the same.

#bind_polymorphic_inverse_type

::Set the type of the related document on the foreign type field, used when associations are polymorphic.

#bind_polymorphic_type

::Set the type of the related document on the foreign type field, used when associations are polymorphic.

#check_inverse!

Check if the inverse is properly defined.

#record_id,
#remove_associated

Remove the associated document from the inverse’s association.

#remove_associated_in_to

Remove the associated document from the inverse’s association.

#remove_associated_many

Remove the associated document from the inverse’s association.

#set_base_association

Ensure that the association on the base is correct, for the cases where we have multiple belongs to definitions and were are setting different parents in memory in order.

#try_method

Convenience method to perform #try but return nil if the method argument is nil.

#unbind_from_relational_parent

Bind the provided document with the base from the parent association.

Instance Method Details

#bind_one(doc)

Binds a single document with the inverse association. Used specifically when appending to the proxy.

Examples:

Bind one document.

person.preferences.bind_one(preference)

Parameters:

  • doc (Document)

    The single document to bind.

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb', line 20

def bind_one(doc)
  binding do
    inverse_keys = try_method(doc, _association.inverse_foreign_key) unless doc.frozen?
    if inverse_keys
      record_id = inverse_record_id(doc)
      unless inverse_keys.include?(record_id)
        try_method(doc, _association.inverse_foreign_key_setter, inverse_keys.push(record_id))
      end
      doc.reset_relation_criteria(_association.inverse)
    end
    _base._synced[_association.foreign_key] = true
    doc._synced[_association.inverse_foreign_key] = true
  end
end

#determine_inverse_association(doc) ⇒ Mongoid::Association::Relatable

Find the inverse association given a document.

Parameters:

  • doc (Mongoid::Document)

    The document for which to determine the inverse association.

Returns:

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb', line 77

def determine_inverse_association(doc)
  doc.relations[_base.class.name.demodulize.underscore.pluralize]
end

#inverse_record_id(doc) ⇒ BSON::ObjectId

Find the inverse id referenced by inverse_keys

Parameters:

Returns:

[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb', line 58

def inverse_record_id(doc)
  if pk = _association.options[:inverse_primary_key]
    _base.send(pk)
  else
    inverse_association = determine_inverse_association(doc)
    if inverse_association
      _base.__send__(inverse_association.primary_key)
    else
      _base._id
    end
  end
end

#unbind_one(doc)

Unbind a single document.

Examples:

Unbind the document.

person.preferences.unbind_one(document)
[ GitHub ]

  
# File 'lib/mongoid/association/referenced/has_and_belongs_to_many/binding.rb', line 39

def unbind_one(doc)
  binding do
    _base.send(_association.foreign_key).delete_one(record_id(doc))
    inverse_keys = try_method(doc, _association.inverse_foreign_key) unless doc.frozen?
    if inverse_keys
      inverse_keys.delete_one(inverse_record_id(doc))
      doc.reset_relation_criteria(_association.inverse)
    end
    _base._synced[_association.foreign_key] = true
    doc._synced[_association.inverse_foreign_key] = true
  end
end