Class: Mongoid::Associations::HasManyRelated
- Includes:
- Proxy
- Defined in:
- lib/mongoid/associations/has_many_related.rb
Overview
:nodoc:
Class Method Summary collapse
-
.instantiate(document, options, target = nil) ⇒ Object
Preferred method for creating the new
HasManyRelated
association. -
.macro ⇒ Object
Returns the macro used to create the association.
-
.update(target, document, options) ⇒ Object
Perform an update of the relationship of the parent and child.
Instance Method Summary collapse
-
#<<(*objects) ⇒ Object
Appends the object to the
Array
, setting its parent in the process. -
#build(attributes = {}) ⇒ Object
Builds a new Document and adds it to the association collection.
-
#concat(*objects) ⇒ Object
Delegates to <<.
-
#create(attributes) ⇒ Object
Creates a new Document and adds it to the association collection.
-
#find(*args) ⇒ Object
Finds a document in this association.
-
#initialize(document, options, target = nil) ⇒ HasManyRelated
constructor
Initializing a related association only requires looking up the objects by their ids.
-
#method_missing(name, *args, &block) ⇒ Object
Override the default behavior to allow the criteria to get reset on each call into the association.
-
#push(*objects) ⇒ Object
Delegates to <<.
Methods included from Proxy
Constructor Details
#initialize(document, options, target = nil) ⇒ HasManyRelated
Initializing a related association only requires looking up the objects by their ids.
Options:
document: The Document
that contains the relationship. options: The association Options
.
64 65 66 67 68 69 70 |
# File 'lib/mongoid/associations/has_many_related.rb', line 64 def initialize(document, , target = nil) @parent, @klass = document, .klass @foreign_key = .foreign_key @base = lambda { @klass.all(:conditions => { @foreign_key => document.id }) } @target = target || @base.call extends() end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Override the default behavior to allow the criteria to get reset on each call into the association.
Example:
person.posts.where(:title => "New")
person.posts # resets the criteria
Returns:
A Criteria object or Array.
83 84 85 86 |
# File 'lib/mongoid/associations/has_many_related.rb', line 83 def method_missing(name, *args, &block) @target = @base.call unless @target.is_a?(Array) @target.send(name, *args, &block) end |
Class Method Details
.instantiate(document, options, target = nil) ⇒ Object
Preferred method for creating the new HasManyRelated
association.
Options:
document: The Document
that contains the relationship. options: The association Options
.
106 107 108 |
# File 'lib/mongoid/associations/has_many_related.rb', line 106 def instantiate(document, , target = nil) new(document, , target) end |
.macro ⇒ Object
Returns the macro used to create the association.
111 112 113 |
# File 'lib/mongoid/associations/has_many_related.rb', line 111 def macro :has_many_related end |
.update(target, document, options) ⇒ Object
Perform an update of the relationship of the parent and child. This will assimilate the child Document
into the parent’s object graph.
Options:
related: The related object parent: The parent Document
to update. options: The association Options
Example:
RelatesToOne.update(game, person, options)
127 128 129 130 131 |
# File 'lib/mongoid/associations/has_many_related.rb', line 127 def update(target, document, ) name = document.class.to_s.underscore target.each { |child| child.send("#{name}=", document) } instantiate(document, , target) end |
Instance Method Details
#<<(*objects) ⇒ Object
Appends the object to the Array
, setting its parent in the process.
9 10 11 12 13 14 15 16 |
# File 'lib/mongoid/associations/has_many_related.rb', line 9 def <<(*objects) load_target objects.flatten.each do |object| object.send("#{@foreign_key}=", @parent.id) @target << object object.save unless @parent.new_record? end end |
#build(attributes = {}) ⇒ Object
Builds a new Document and adds it to the association collection. The document created will be of the same class as the others in the association, and the attributes will be passed into the constructor.
Returns the newly created object.
23 24 25 26 27 28 29 |
# File 'lib/mongoid/associations/has_many_related.rb', line 23 def build(attributes = {}) load_target name = @parent.class.to_s.underscore object = @klass.instantiate(attributes.merge(name => @parent)) @target << object object end |
#concat(*objects) ⇒ Object
Delegates to <<
32 33 34 |
# File 'lib/mongoid/associations/has_many_related.rb', line 32 def concat(*objects) self << objects end |
#create(attributes) ⇒ Object
Creates a new Document and adds it to the association collection. The document created will be of the same class as the others in the association, and the attributes will be passed into the constructor and the new object will then be saved.
Returns the newly created object.
42 43 44 45 46 47 48 |
# File 'lib/mongoid/associations/has_many_related.rb', line 42 def create(attributes) object = build(attributes) object.run_callbacks(:before_create) object.save object.run_callbacks(:after_create) object end |
#find(*args) ⇒ Object
Finds a document in this association. If an id is passed, will return the document for that id.
52 53 54 55 |
# File 'lib/mongoid/associations/has_many_related.rb', line 52 def find(*args) args[1][:conditions].merge!(@foreign_key.to_sym => @parent.id) if args.size > 1 @klass.find(*args) end |
#push(*objects) ⇒ Object
Delegates to <<
89 90 91 |
# File 'lib/mongoid/associations/has_many_related.rb', line 89 def push(*objects) self << objects end |