Class: Mongoid::Association::Referenced::BelongsTo::Binding
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
|
|
Inherits: | Object |
Defined in: | lib/mongoid/association/referenced/belongs_to/binding.rb |
Overview
Binding
class for belongs_to 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
-
#bind_one
Binds the base object to the inverse of the association.
-
#unbind_one
Unbinds the base object and the inverse, caused by setting the reference to nil.
-
#check_polymorphic_inverses!(doc)
private
Internal use only
Internal use only
Check for problems with multiple inverse definitions.
::Mongoid::Association::Bindable
- Included
#binding | Execute the provided block inside a binding. |
#initialize | Create the new binding. |
#bind_foreign_key |
|
#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 |
|
#bind_polymorphic_type |
|
#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 |
#unbind_from_relational_parent | Bind the provided document with the base from the parent association. |
Instance Method Details
#bind_one
Binds the base object to the inverse of the association. This is so we are referenced to the actual objects themselves on both sides.
This case sets the association on the inverse object as well as the document itself.
# File 'lib/mongoid/association/referenced/belongs_to/binding.rb', line 22
def bind_one binding do check_polymorphic_inverses!(_target) bind_foreign_key(_base, record_id(_target)) # set the inverse type (e.g. "#{name}_type") for new polymorphic associations if _association.inverse_type && !_base.frozen? key = _association.resolver.default_key_for(_target) bind_polymorphic_inverse_type(_base, key) end if inverse = _association.inverse(_target) if set_base_association if _base.referenced_many? _target.__send__(inverse).push(_base) else remove_associated(_target) _target.set_relation(inverse, _base) end end end end end
#check_polymorphic_inverses!(doc) (private)
Check for problems with multiple inverse definitions.
# File 'lib/mongoid/association/referenced/belongs_to/binding.rb', line 78
def check_polymorphic_inverses!(doc) inverses = _association.inverses(doc) if inverses.length > 1 && _base.send(_association.foreign_key).nil? raise Errors::InvalidSetPolymorphicRelation.new( _association.name, _base.class.name, _target.class.name ) end end
#unbind_one
Unbinds the base object and the inverse, caused by setting the reference to nil.
# File 'lib/mongoid/association/referenced/belongs_to/binding.rb', line 52
def unbind_one binding do inverse = _association.inverse(_target) bind_foreign_key(_base, nil) bind_polymorphic_inverse_type(_base, nil) if inverse set_base_association if _base.referenced_many? _target.__send__(inverse).delete(_base) else _target.set_relation(inverse, nil) end end end end