123456789_123456789_123456789_123456789_123456789_

Class: Mongoid::Association::Proxy

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Instance Chain:
Inherits: Object
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.

Constant Summary

Class Method Summary

Instance Attribute Summary

  • #_association rw
  • #_base rw

    Model instance for the base of the association.

  • #_target (also: #in_memory) rw

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

::Mongoid::Threaded::Lifecycle - Included

#_assigning

Begin the assignment of attributes.

#_assigning?

Is the current thread in assigning mode?

#_binding

Execute a block in binding mode.

#_binding?

Is the current thread in binding mode?

#_building

Execute a block in building mode.

#_building?

Is the current thread in building mode?

#_creating?

Is the current thread in creating mode?

#_loading

Execute a block in loading mode.

#_loading?

Is the current thread in loading mode?

Instance Method Summary

Marshalable - Included

#marshal_dump

Provides the data needed to Marshal.dump an association proxy.

#marshal_load

Takes the provided data and sets it back on the proxy.

Constructor Details

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

Sets the target and the association metadata properties.

Parameters:

Yields:

  • (_self)

Yield Parameters:

  • _self (Proxy)

    the object that the method was called on

[ GitHub ]

  
# 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

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:

[ GitHub ]

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

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

Instance Attribute Details

#_association (rw)

[ GitHub ]

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

attr_accessor :_association

#_base (rw)

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.

[ GitHub ]

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

attr_accessor :_base

#_target (rw) Also known as: #in_memory

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.

[ GitHub ]

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

attr_accessor :_target

Instance Method Details

#extend_proxies(*extension)

Allow extension to be an array and extend each module

[ GitHub ]

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

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

#extend_proxy

[ GitHub ]

  
# File 'lib/mongoid/association/proxy.rb', line 12

alias extend_proxy extend

#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.

[ GitHub ]

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

def klass
  _association&.klass
end

#reset_unloaded

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
[ GitHub ]

  
# 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.

[ GitHub ]

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

def substitutable
  _target
end