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.
27 28 29 30 |
# File 'lib/mongoid/association/accessors.rb', line 27 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.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mongoid/association/accessors.rb', line 44 def create_relation(object, association, selected_fields = nil) key = @attributes[association.inverse_type] type = key ? association.resolver.model_for(key) : nil target = if t = association.build(self, object, type, selected_fields) association.create_relation(self, t) else nil end # Only need to do this on embedded associations. The pending callbacks # are only added when materializing the documents, which only happens # on embedded associations. There is no call to the database in the # construction of a referenced association. if association. Array(target).each do |doc| doc.try(:run_pending_callbacks) end end target 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.
73 74 75 76 77 |
# File 'lib/mongoid/association/accessors.rb', line 73 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.
89 90 91 |
# File 'lib/mongoid/association/accessors.rb', line 89 def set_relation(name, relation) instance_variable_set("@_#{name}", relation) end |