class Mongoid::Association::Proxy
This class is the superclass for all relation proxy objects, and contains common behaviour for all of them.
Attributes
Protected Class Methods
Apply ordering to the criteria if it was defined on the relation.
@example Apply the ordering.
Proxy.apply_ordering(criteria, association)
@param [ Criteria ] criteria The criteria to modify. @param [ Association ] association The association metadata.
@return [ Criteria ] The ordered criteria.
@since 3.0.6
# File lib/mongoid/association/proxy.rb, line 181 def apply_ordering(criteria, association) association.order ? criteria.order_by(association.order) : criteria end
Public Instance Methods
Allow extension to be an array and extend each module
# File lib/mongoid/association/proxy.rb, line 45 def extend_proxies(*extension) extension.flatten.each {|ext| extend_proxy(ext) } end
Convenience for setting the target and the association metadata properties since all proxies will need to do this.
@example Initialize the proxy.
proxy.init(person, name, association)
@param [ Document ] base The base document on the proxy. @param [ Document, Array<Document> ] target The target of the proxy. @param [ Association ] association The association metadata.
@since 2.0.0.rc.1
# File lib/mongoid/association/proxy.rb, line 38 def init(base, target, association) @_base, @_target, @_association = base, target, association yield(self) if block_given? extend_proxies(association.extension) if association.extension end
Get the class from the association, or return nil if no association present.
@example Get the class.
proxy.klass
@return [ Class ] The relation class.
@since 3.0.15
# File lib/mongoid/association/proxy.rb, line 57 def klass _association ? _association.klass : nil end
Resets the criteria inside the relation proxy. Used by many to many relations to keep the underlying ids array in sync.
@example Reset the relation criteria.
person.preferences.reset_relation_criteria
@since 3.0.14
# File lib/mongoid/association/proxy.rb, line 68 def reset_unloaded _target.reset_unloaded(criteria) end
The default substitutable object for a relation proxy is the clone of the target.
@example Get the substitutable.
proxy.substitutable
@return [ Object ] A clone of the target.
@since 2.1.6
# File lib/mongoid/association/proxy.rb, line 81 def substitutable _target end
Protected Instance Methods
Takes the supplied document and sets the association on it.
@example Set the association metadata.
proxt.characterize_one(name)
@param [ Document ] document The document to set on.
@since 2.0.0.rc.4
# File lib/mongoid/association/proxy.rb, line 108 def characterize_one(document) document._association = _association unless document._association end
Get the collection from the root of the hierarchy.
@example Get the collection.
relation.collection
@return [ Collection ] The root's collection.
@since 2.0.0
# File lib/mongoid/association/proxy.rb, line 95 def collection root = _base._root root.collection unless root.embedded? end
Executes a callback method
@example execute the before add callback
execute_callback(:before_add)
@param [ Symbol ] callback to be executed
@since 3.1.0
# File lib/mongoid/association/proxy.rb, line 158 def execute_callback(callback, doc) _association.get_callbacks(callback).each do |c| if c.is_a? Proc c.call(_base, doc) else _base.send c, doc end end end
Default behavior of method missing should be to delegate all calls to the target of the proxy. This can be overridden in special cases.
@param [ String, Symbol ] name The name of the method. @param [ Array ] args The arguments passed to the method.
# File lib/mongoid/association/proxy.rb, line 118 def method_missing(name, *args, &block) _target.send(name, *args, &block) end
When the base document illegally references an embedded document this error will get raised.
@example Raise the error.
relation.raise_mixed
@raise [ Errors::MixedRelations ] The error.
@since 2.0.0
# File lib/mongoid/association/proxy.rb, line 131 def raise_mixed raise Errors::MixedRelations.new(_base.class, _association.klass) end
When the base is not yet saved and the user calls create or create! on the relation, this error will get raised.
@example Raise the error.
relation.raise_unsaved(post)
@param [ Document ] doc The child document getting created.
@raise [ Errors::UnsavedDocument ] The error.
@since 2.0.0.rc.6
# File lib/mongoid/association/proxy.rb, line 146 def raise_unsaved(doc) raise Errors::UnsavedDocument.new(_base, doc) end