Module: Mongoid::Association::Accessors
- Extended by:
- ActiveSupport::Concern
- Included in:
- Mongoid::Association
- Defined in:
- lib/mongoid/association/accessors.rb
Overview
This module contains all the behaviour related to accessing relations through the getters and setters, and how to delegate to builders to create new ones.
Instance Method Summary collapse
-
#__build__(name, object, association) ⇒ Proxy
Builds the related document and creates the relation unless the document is nil, then sets the relation on this document.
-
#create_relation(object, association) ⇒ Proxy
Create a relation from an object and association.
-
#reset_relation_criteria(name) ⇒ Object
Resets the criteria inside the relation proxy.
-
#set_relation(name, relation) ⇒ Proxy
Set the supplied relation to an instance variable on the class with the provided name.
Instance Method Details
#__build__(name, object, association) ⇒ Proxy
Builds the related document and creates the relation unless the document is nil, then sets the relation on this document.
24 25 26 27 |
# File 'lib/mongoid/association/accessors.rb', line 24 def __build__(name, object, association) relation = create_relation(object, association) set_relation(name, relation) end |
#create_relation(object, association) ⇒ Proxy
Create a relation from an object and association.
40 41 42 43 44 |
# File 'lib/mongoid/association/accessors.rb', line 40 def create_relation(object, association) type = @attributes[association.inverse_type] target = association.build(self, object, type) target ? association.create_relation(self, target) : nil end |
#reset_relation_criteria(name) ⇒ Object
Resets the criteria inside the relation proxy. Used by many-to-many relations to keep the underlying ids array in sync.
55 56 57 58 59 |
# File 'lib/mongoid/association/accessors.rb', line 55 def reset_relation_criteria(name) if instance_variable_defined?("@_#{name}") send(name).reset_unloaded end end |
#set_relation(name, relation) ⇒ Proxy
Set the supplied relation to an instance variable on the class with the provided name. Used as a helper just for code cleanliness.
73 74 75 |
# File 'lib/mongoid/association/accessors.rb', line 73 def set_relation(name, relation) instance_variable_set("@_#{name}", relation) end |