Class: Mongoid::Associations::HasManyRelated

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

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Proxy

included

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
# File 'lib/mongoid/associations/has_many_related.rb', line 64

def initialize(document, options, target = nil)
  @parent, @klass = document, options.klass
  @foreign_key = options.foreign_key
  @target = target || @klass.all(:conditions => { @foreign_key => document.id })
  extends(options)
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.



89
90
91
# File 'lib/mongoid/associations/has_many_related.rb', line 89

def instantiate(document, options, target = nil)
  new(document, options, target)
end

.macroObject

Returns the macro used to create the association.



94
95
96
# File 'lib/mongoid/associations/has_many_related.rb', line 94

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)



110
111
112
113
114
# File 'lib/mongoid/associations/has_many_related.rb', line 110

def update(target, document, options)
  name = document.class.to_s.underscore
  target.each { |child| child.send("#{name}=", document) }
  instantiate(document, options, 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 <<



72
73
74
# File 'lib/mongoid/associations/has_many_related.rb', line 72

def push(*objects)
  self << objects
end