Class: Mongoid::Association::Proxy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Marshalable, Threaded::Lifecycle
Defined in:
lib/mongoid/association/proxy.rb

Overview

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

Direct Known Subclasses

Many, One

Constant Summary collapse

KEEPER_METHODS =

specific methods to prevent from being undefined

%i[
  send
  object_id
  equal?
  respond_to?
  respond_to_missing?
  tap
  public_send
  extend_proxy
  extend_proxies
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Marshalable

#marshal_dump, #marshal_load

Constructor Details

#initialize(base, target, association) {|_self| ... } ⇒ Proxy

Sets the target and the association metadata properties.

Parameters:

Yields:

  • (_self)

Yield Parameters:



60
61
62
63
64
# File 'lib/mongoid/association/proxy.rb', line 60

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

Instance Attribute Details

#_associationObject

Returns the value of attribute _association.



41
42
43
# File 'lib/mongoid/association/proxy.rb', line 41

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.



39
40
41
# File 'lib/mongoid/association/proxy.rb', line 39

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.



48
49
50
# File 'lib/mongoid/association/proxy.rb', line 48

def _target
  @_target
end

Class Method Details

.apply_ordering(criteria, association) ⇒ Criteria

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

Examples:

Apply the ordering.

Proxy.apply_ordering(criteria, association)

Parameters:

Returns:



206
207
208
# File 'lib/mongoid/association/proxy.rb', line 206

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



67
68
69
# File 'lib/mongoid/association/proxy.rb', line 67

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

#klassClass

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

Examples:

Get the class.

proxy.klass

Returns:

  • (Class)

    The association class.



77
78
79
# File 'lib/mongoid/association/proxy.rb', line 77

def klass
  _association&.klass
end

#reset_unloadedObject

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

Examples:

Reset the association criteria.

person.preferences.reset_relation_criteria


86
87
88
# File 'lib/mongoid/association/proxy.rb', line 86

def reset_unloaded
  _target.reset_unloaded(criteria)
end

#substitutableObject

The default substitutable object for an association proxy is the clone of the target.

Examples:

Get the substitutable.

proxy.substitutable

Returns:

  • (Object)

    A clone of the target.



97
98
99
# File 'lib/mongoid/association/proxy.rb', line 97

def substitutable
  _target
end