class Mongoid::Association::Referenced::BelongsTo::Proxy

This class handles all behavior for associations that are either one-to-many or one-to-one, where the foreign key is stored on this side of the association and the reference is to document(s) in another collection.

Public Class Methods

new(base, target, association) click to toggle source

Instantiate a new belongs_to association proxy.

@example Create the new proxy.

Association::BelongsTo::Proxy.new(game, person, association)

@param [ Document ] base The document this association hangs off of. @param [ Document, Array<Document> ] target The target (parent) of the

association.

@param [ Association ] association The association object.

# File lib/mongoid/association/referenced/belongs_to/proxy.rb, line 25
def initialize(base, target, association)
  init(base, target, association) do
    characterize_one(_target)
    bind_one
  end
end

Private Class Methods

eager_loader(association, docs) click to toggle source

Get the Eager object for this type of association.

@example Get the eager loader object

@param [ Association ] association The association object. @param [ Array<Document> ] docs The array of documents.

@since 7.0

# File lib/mongoid/association/referenced/belongs_to/proxy.rb, line 117
def eager_loader(association, docs)
  Eager.new(association, docs)
end
embedded?() click to toggle source

Returns true if the association is an embedded one. In this case always false.

@example Is this association embedded?

Association::BelongsTo::Proxy.embedded?

@return [ false ] Always false.

@since 2.0.0.rc.1

# File lib/mongoid/association/referenced/belongs_to/proxy.rb, line 130
def embedded?
  false
end

Public Instance Methods

nullify() click to toggle source

Removes the association between the base document and the target document by deleting the foreign key and the reference, orphaning the target document in the process.

@example Nullify the association.

person.game.nullify
# File lib/mongoid/association/referenced/belongs_to/proxy.rb, line 39
def nullify
  unbind_one
  _target.save
end
substitute(replacement) click to toggle source

Substitutes the supplied target documents for the existing document in the association.

@example Substitute the association.

name.substitute(new_name)

@param [ Document, Array<Document> ] replacement The replacement.

@return [ self, nil ] The association or nil.

@since 2.0.0.rc.1

# File lib/mongoid/association/referenced/belongs_to/proxy.rb, line 55
def substitute(replacement)
  unbind_one
  if replacement
    self._target = normalize(replacement)
    bind_one
    self
  end
end

Private Instance Methods

binding() click to toggle source

Instantiate the binding associated with this association.

@example Get the binding object.

binding([ address ])

@return [ Binding ] The binding object.

@since 2.0.0.rc.1

# File lib/mongoid/association/referenced/belongs_to/proxy.rb, line 74
def binding
  BelongsTo::Binding.new(_base, _target, _association)
end
normalize(replacement) click to toggle source

Normalize the value provided as a replacement for substitution.

@api private

@example Normalize the substitute.

proxy.normalize(id)

@param [ Document, Object ] replacement The replacement object.

@return [ Document ] The document.

@since 3.1.5

# File lib/mongoid/association/referenced/belongs_to/proxy.rb, line 90
def normalize(replacement)
  return replacement if replacement.is_a?(Document)
  _association.build(klass, replacement)
end
persistable?() click to toggle source

Are we able to persist this association?

@example Can we persist the association?

relation.persistable?

@return [ true, false ] If the association is persistable.

@since 2.1.0

# File lib/mongoid/association/referenced/belongs_to/proxy.rb, line 103
def persistable?
  _target.persisted? && !_binding? && !_building?
end