Class: Mongoid::Association::Proxy

Inherits:
Object
  • Object
show all
Includes:
Marshalable, Threaded::Lifecycle
Defined in:
build/mongoid-7.0/lib/mongoid/association/proxy.rb

Overview

This class is the superclass for all relation proxy objects, and contains common behaviour for all of them.

Direct Known Subclasses

Many, One

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Marshalable

#marshal_dump, #marshal_load

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)

Default behavior of method missing should be to delegate all calls to the target of the proxy. This can be overridden in special cases.

Parameters:

  • name (String, Symbol)

    The name of the method.

  • args (Array)

    The arguments passed to the method.



132
133
134
# File 'build/mongoid-7.0/lib/mongoid/association/proxy.rb', line 132

def method_missing(name, *args, &block)
  _target.send(name, *args, &block)
end

Instance Attribute Details

#_associationObject

Returns the value of attribute _association.



27
28
29
# File 'build/mongoid-7.0/lib/mongoid/association/proxy.rb', line 27

def _association
  @_association
end

#_baseObject

Model instance for the base of the association.

For example, if a Post embeds_many Comments, _base is a particular instance of the Post model.



25
26
27
# File 'build/mongoid-7.0/lib/mongoid/association/proxy.rb', line 25

def _base
  @_base
end

#_targetObject

Model instance for one to one associations, or array of model instances for one to many associations, for the target of the association.

For example, if a Post embeds_many Comments, _target is an array of Comment models embedded in a particular Post.



34
35
36
# File 'build/mongoid-7.0/lib/mongoid/association/proxy.rb', line 34

def _target
  @_target
end

Class Method Details

.apply_ordering(criteria, association) ⇒ Criteria

Apply ordering to the criteria if it was defined on the relation.

Examples:

Apply the ordering.

Proxy.apply_ordering(criteria, association)

Parameters:

  • criteria (Criteria)

    The criteria to modify.

  • association (Association)

    The association metadata.

Returns:

Since:

  • 3.0.6



195
196
197
# File 'build/mongoid-7.0/lib/mongoid/association/proxy.rb', line 195

def apply_ordering(criteria, association)
  association.order ? criteria.order_by(association.order) : criteria
end

Instance Method Details

#extend_proxies(*extension) ⇒ Object

Allow extension to be an array and extend each module



59
60
61
# File 'build/mongoid-7.0/lib/mongoid/association/proxy.rb', line 59

def extend_proxies(*extension)
  extension.flatten.each {|ext| extend_proxy(ext) }
end

#init(base, target, association) {|_self| ... } ⇒ Object

Convenience for setting the target and the association metadata properties since all proxies will need to do this.

Examples:

Initialize the proxy.

proxy.init(person, name, association)

Parameters:

Yields:

  • (_self)

Yield Parameters:

Since:

  • 2.0.0.rc.1



52
53
54
55
56
# File 'build/mongoid-7.0/lib/mongoid/association/proxy.rb', line 52

def init(base, target, association)
  @_base, @_target, @_association = base, target, association
  yield(self) if block_given?
  extend_proxies(association.extension) if association.extension
end

#klassClass

Get the class from the association, or return nil if no association present.

Examples:

Get the class.

proxy.klass

Returns:

  • (Class)

    The relation class.

Since:

  • 3.0.15



71
72
73
# File 'build/mongoid-7.0/lib/mongoid/association/proxy.rb', line 71

def klass
  _association ? _association.klass : nil
end

#reset_unloadedObject

Resets the criteria inside the relation proxy. Used by many to many relations to keep the underlying ids array in sync.

Examples:

Reset the relation criteria.

person.preferences.reset_relation_criteria

Since:

  • 3.0.14



82
83
84
# File 'build/mongoid-7.0/lib/mongoid/association/proxy.rb', line 82

def reset_unloaded
  _target.reset_unloaded(criteria)
end

#substitutableObject

The default substitutable object for a relation proxy is the clone of the target.

Examples:

Get the substitutable.

proxy.substitutable

Returns:

  • (Object)

    A clone of the target.

Since:

  • 2.1.6



95
96
97
# File 'build/mongoid-7.0/lib/mongoid/association/proxy.rb', line 95

def substitutable
  _target
end