Class: Mongoid::Associations::BelongsTo

Inherits:
Object
  • Object
show all
Includes:
Proxy
Defined in:
lib/mongoid/associations/belongs_to.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Proxy

included

Constructor Details

#initialize(document, options) ⇒ BelongsTo

Creates the new association by setting the internal document as the passed in Document. This should be the parent.

All method calls on this object will then be delegated to the internal document itself.

Options:

document: The parent Document options: The association options



20
21
22
# File 'lib/mongoid/associations/belongs_to.rb', line 20

def initialize(document, options)
  @document, @options = document, options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Delegate all missing methods over to the parent Document.



32
33
34
# File 'lib/mongoid/associations/belongs_to.rb', line 32

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

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



7
8
9
# File 'lib/mongoid/associations/belongs_to.rb', line 7

def document
  @document
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/mongoid/associations/belongs_to.rb', line 7

def options
  @options
end

Class Method Details

.instantiate(document, options) ⇒ Object

Creates the new association by setting the internal document as the passed in Document. This should be the parent.

Options:

document: The parent Document options: The association options



45
46
47
48
# File 'lib/mongoid/associations/belongs_to.rb', line 45

def instantiate(document, options)
  parent = document._parent
  parent.nil? ? nil : new(parent, options)
end

.macroObject

Returns the macro used to create the association.



51
52
53
# File 'lib/mongoid/associations/belongs_to.rb', line 51

def macro
  :belongs_to
end

.update(parent, child, options) ⇒ Object

Perform an update of the relationship of the parent and child. This is initialized by setting a parent object as the association on the Document. Will properly set a has_one or a has_many.



58
59
60
61
62
# File 'lib/mongoid/associations/belongs_to.rb', line 58

def update(parent, child, options)
  child.parentize(parent, options.inverse_of)
  child.notify
  parent
end

Instance Method Details

#find(id) ⇒ Object

Returns the parent document. The id param is present for compatibility with rails, however this could be overwritten in the future.



27
28
29
# File 'lib/mongoid/associations/belongs_to.rb', line 27

def find(id)
  @document
end