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

This class handles all behaviour for relations that are either one-to-many or one-to-one, where the foreign key is stored on this side of the relation 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 relation hangs off of. @param [ Document, Array<Document> ] target The target (parent) of the

relation.

@param [ Association ] association The association object.

# File lib/mongoid/association/referenced/belongs_to/proxy.rb, line 22
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 114
def eager_loader(association, docs)
  Eager.new(association, docs)
end
embedded?() click to toggle source

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

@example Is this relation 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 127
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 relation.

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

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

@example Substitute the relation.

name.substitute(new_name)

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

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

@since 2.0.0.rc.1

# File lib/mongoid/association/referenced/belongs_to/proxy.rb, line 52
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 relation.

@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 71
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 87
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 relation?

@example Can we persist the relation?

relation.persistable?

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

@since 2.1.0

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