Class: Mongoid::Associations::BelongsTo
- Includes:
- Proxy
- Defined in:
- lib/mongoid/associations/belongs_to.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.instantiate(document, options) ⇒ Object
Creates the new association by setting the internal document as the passed in Document.
-
.macro ⇒ Object
Returns the macro used to create the association.
-
.update(parent, child, options) ⇒ Object
Perform an update of the relationship of the parent and child.
Instance Method Summary collapse
-
#find(id) ⇒ Object
Returns the parent document.
-
#initialize(document, options) ⇒ BelongsTo
constructor
Creates the new association by setting the internal document as the passed in Document.
-
#method_missing(name, *args, &block) ⇒ Object
Delegate all missing methods over to the parent
Document
.
Methods included from Proxy
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, ) @document, @options = document, 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
#document ⇒ Object (readonly)
Returns the value of attribute document.
7 8 9 |
# File 'lib/mongoid/associations/belongs_to.rb', line 7 def document @document end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/mongoid/associations/belongs_to.rb', line 7 def @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, ) parent = document._parent parent.nil? ? nil : new(parent, ) end |
.macro ⇒ Object
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, ) child.parentize(parent, .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 |