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 behavior related to accessing associations through the getters and setters, and how to delegate to builders to create new ones.
Instance Method Summary collapse
-
#__build__(name, object, association, selected_fields = nil) ⇒ Proxy
Builds the related document and creates the association unless the document is nil, then sets the association on this document.
-
#create_relation(object, association, selected_fields = nil) ⇒ Proxy
Create an association from an object and association metadata.
-
#reset_relation_criteria(name) ⇒ Object
Resets the criteria inside the association proxy.
-
#set_relation(name, relation) ⇒ Proxy
Set the supplied association to an instance variable on the class with the provided name.
Instance Method Details
#__build__(name, object, association, selected_fields = nil) ⇒ Proxy
Builds the related document and creates the association unless the document is nil, then sets the association on this document.
26 27 28 29 |
# File 'lib/mongoid/association/accessors.rb', line 26 def __build__(name, object, association, selected_fields = nil) relation = create_relation(object, association, selected_fields) set_relation(name, relation) end |
#create_relation(object, association, selected_fields = nil) ⇒ Proxy
Create an association from an object and association metadata.
43 44 45 46 47 |
# File 'lib/mongoid/association/accessors.rb', line 43 def create_relation(object, association, selected_fields = nil) type = @attributes[association.inverse_type] target = association.build(self, object, type, selected_fields) target ? association.create_relation(self, target) : nil end |
#reset_relation_criteria(name) ⇒ Object
Resets the criteria inside the association proxy. Used by many-to-many associations to keep the underlying ids array in sync.
56 57 58 59 60 |
# File 'lib/mongoid/association/accessors.rb', line 56 def reset_relation_criteria(name) if instance_variable_defined?("@_#{name}") send(name).reset_unloaded end end |
#set_relation(name, relation) ⇒ Proxy
Set the supplied association to an instance variable on the class with the provided name. Used as a helper just for code cleanliness.
72 73 74 |
# File 'lib/mongoid/association/accessors.rb', line 72 def set_relation(name, relation) instance_variable_set("@_#{name}", relation) end |