mongoid 0.6.3 → 0.6.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongoid/associations.rb +92 -75
- data/lib/mongoid/associations/accessor.rb +41 -0
- data/lib/mongoid/associations/{belongs_to_association.rb → belongs_to.rb} +15 -1
- data/lib/mongoid/associations/{has_many_association.rb → has_many.rb} +22 -1
- data/lib/mongoid/associations/{has_one_association.rb → has_one.rb} +11 -2
- data/lib/mongoid/document.rb +8 -4
- data/mongoid.gemspec +14 -14
- data/spec/integration/mongoid/document_spec.rb +51 -0
- data/spec/unit/mongoid/associations/accessor_spec.rb +108 -0
- data/spec/unit/mongoid/associations/belongs_to_spec.rb +103 -0
- data/spec/unit/mongoid/associations/{has_many_association_spec.rb → has_many_spec.rb} +30 -11
- data/spec/unit/mongoid/associations/has_one_spec.rb +69 -0
- data/spec/unit/mongoid/associations_spec.rb +19 -0
- metadata +14 -14
- data/lib/mongoid/associations/factory.rb +0 -21
- data/spec/unit/mongoid/associations/belongs_to_association_spec.rb +0 -59
- data/spec/unit/mongoid/associations/factory_spec.rb +0 -67
- data/spec/unit/mongoid/associations/has_one_association_spec.rb +0 -39
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.4
|
data/lib/mongoid/associations.rb
CHANGED
@@ -1,87 +1,104 @@
|
|
1
1
|
require "mongoid/associations/decorator"
|
2
|
-
require "mongoid/associations/
|
3
|
-
require "mongoid/associations/
|
4
|
-
require "mongoid/associations/
|
5
|
-
require "mongoid/associations/
|
2
|
+
require "mongoid/associations/accessor"
|
3
|
+
require "mongoid/associations/belongs_to"
|
4
|
+
require "mongoid/associations/has_many"
|
5
|
+
require "mongoid/associations/has_one"
|
6
6
|
|
7
7
|
module Mongoid # :nodoc:
|
8
|
-
module Associations
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
#
|
15
|
-
# Options:
|
16
|
-
#
|
17
|
-
# association_name: A +Symbol+ that matches the name of the parent class.
|
18
|
-
#
|
19
|
-
# Example:
|
20
|
-
#
|
21
|
-
# class Person < Mongoid::Document
|
22
|
-
# has_many :addresses
|
23
|
-
# end
|
24
|
-
#
|
25
|
-
# class Address < Mongoid::Document
|
26
|
-
# belongs_to :person
|
27
|
-
# end
|
28
|
-
def belongs_to(association_name)
|
29
|
-
@embedded = true
|
30
|
-
add_association(:belongs_to, association_name.to_s.classify, association_name)
|
8
|
+
module Associations #:nodoc:
|
9
|
+
def self.included(base)
|
10
|
+
base.class_eval do
|
11
|
+
include InstanceMethods
|
12
|
+
extend ClassMethods
|
13
|
+
end
|
31
14
|
end
|
32
15
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
# Options:
|
38
|
-
#
|
39
|
-
# association_name: A +Symbol+ that is the plural child class name.
|
40
|
-
#
|
41
|
-
# Example:
|
42
|
-
#
|
43
|
-
# class Person < Mongoid::Document
|
44
|
-
# has_many :addresses
|
45
|
-
# end
|
46
|
-
#
|
47
|
-
# class Address < Mongoid::Document
|
48
|
-
# belongs_to :person
|
49
|
-
# end
|
50
|
-
def has_many(association_name, options = {})
|
51
|
-
add_association(:has_many, association_name.to_s.classify, association_name, options)
|
16
|
+
module InstanceMethods
|
17
|
+
def associations
|
18
|
+
self.class.associations
|
19
|
+
end
|
52
20
|
end
|
53
21
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
22
|
+
module ClassMethods
|
23
|
+
def associations
|
24
|
+
@associations ||= HashWithIndifferentAccess.new
|
25
|
+
end
|
26
|
+
# Adds the association back to the parent document. This macro is
|
27
|
+
# necessary to set the references from the child back to the parent
|
28
|
+
# document. If a child does not define this association calling
|
29
|
+
# persistence methods on the child object will cause a save to fail.
|
30
|
+
#
|
31
|
+
# Options:
|
32
|
+
#
|
33
|
+
# association_name: A +Symbol+ that matches the name of the parent class.
|
34
|
+
#
|
35
|
+
# Example:
|
36
|
+
#
|
37
|
+
# class Person < Mongoid::Document
|
38
|
+
# has_many :addresses
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# class Address < Mongoid::Document
|
42
|
+
# belongs_to :person
|
43
|
+
# end
|
44
|
+
def belongs_to(association_name)
|
45
|
+
@embedded = true
|
46
|
+
add_association(:belongs_to, association_name.to_s.classify, association_name)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Adds the association from a parent document to its children. The name
|
50
|
+
# of the association needs to be a pluralized form of the child class
|
51
|
+
# name.
|
52
|
+
#
|
53
|
+
# Options:
|
54
|
+
#
|
55
|
+
# association_name: A +Symbol+ that is the plural child class name.
|
56
|
+
#
|
57
|
+
# Example:
|
58
|
+
#
|
59
|
+
# class Person < Mongoid::Document
|
60
|
+
# has_many :addresses
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# class Address < Mongoid::Document
|
64
|
+
# belongs_to :person
|
65
|
+
# end
|
66
|
+
def has_many(association_name, options = {})
|
67
|
+
add_association(:has_many, association_name.to_s.classify, association_name, options)
|
68
|
+
end
|
74
69
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
70
|
+
# Adds the association from a parent document to its child. The name
|
71
|
+
# of the association needs to be a singular form of the child class
|
72
|
+
# name.
|
73
|
+
#
|
74
|
+
# Options:
|
75
|
+
#
|
76
|
+
# association_name: A +Symbol+ that is the plural child class name.
|
77
|
+
#
|
78
|
+
# Example:
|
79
|
+
#
|
80
|
+
# class Person < Mongoid::Document
|
81
|
+
# has_many :addresses
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# class Address < Mongoid::Document
|
85
|
+
# belongs_to :person
|
86
|
+
# end
|
87
|
+
def has_one(association_name, options = {})
|
88
|
+
add_association(:has_one, association_name.to_s.titleize, association_name, options)
|
81
89
|
end
|
82
|
-
|
83
|
-
|
84
|
-
|
90
|
+
|
91
|
+
private
|
92
|
+
# Adds the association to the associations hash with the type as the key,
|
93
|
+
# then adds the accessors for the association.
|
94
|
+
def add_association(type, class_name, name, options = {})
|
95
|
+
associations[name] = type
|
96
|
+
define_method(name) do
|
97
|
+
Associations::Accessor.get(type, name, self, options)
|
98
|
+
end
|
99
|
+
define_method("#{name}=") do |object|
|
100
|
+
Associations::Accessor.set(type, name, self, object, options)
|
101
|
+
end
|
85
102
|
end
|
86
103
|
end
|
87
104
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Mongoid #:nodoc:
|
2
|
+
module Associations #:nodoc:
|
3
|
+
class Accessor #:nodoc:
|
4
|
+
class << self
|
5
|
+
# Gets an association, based on the type provided and
|
6
|
+
# passes the name and document into the newly instantiated
|
7
|
+
# association.
|
8
|
+
#
|
9
|
+
# If the type is invalid a InvalidAssociationError will be thrown.
|
10
|
+
def get(type, name, document, options = {})
|
11
|
+
case type
|
12
|
+
when :belongs_to then BelongsTo.new(document)
|
13
|
+
when :has_many then HasMany.new(name, document, options)
|
14
|
+
when :has_one then HasOne.new(name, document, options)
|
15
|
+
else raise InvalidAssociationError
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Set an object association. This is used to set the parent reference
|
20
|
+
# in a +BelongsTo+, a child reference in a +HasOne+, or many child
|
21
|
+
# references in a +HasMany+.
|
22
|
+
#
|
23
|
+
# Options:
|
24
|
+
#
|
25
|
+
# type: The association type
|
26
|
+
# name: The name of the association
|
27
|
+
# document: The base document to handle the access for.
|
28
|
+
# object: The object that was passed in to the setter method.
|
29
|
+
# options: optional options.
|
30
|
+
def set(type, name, document, object, options ={})
|
31
|
+
case type
|
32
|
+
when :belongs_to then BelongsTo.update(document, object)
|
33
|
+
when :has_many then HasMany.update(object, document, name)
|
34
|
+
when :has_one then HasOne.update(object, document, name)
|
35
|
+
else raise InvalidAssociationError
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Mongoid #:nodoc:
|
2
2
|
module Associations #:nodoc:
|
3
|
-
class
|
3
|
+
class BelongsTo #:nodoc:
|
4
4
|
include Decorator
|
5
5
|
|
6
6
|
# Creates the new association by setting the internal
|
@@ -21,6 +21,20 @@ module Mongoid #:nodoc:
|
|
21
21
|
@document
|
22
22
|
end
|
23
23
|
|
24
|
+
class << self
|
25
|
+
# Perform an update of the relationship of the parent and child. This
|
26
|
+
# is initialized by setting a parent object as the association on the
|
27
|
+
# +Document+. Will properly set a has_one or a has_many.
|
28
|
+
def update(child, parent)
|
29
|
+
name = child.class.name.demodulize.downcase
|
30
|
+
if parent.associations[name]
|
31
|
+
child.parentize(parent, name)
|
32
|
+
else
|
33
|
+
child.parentize(parent, name.tableize)
|
34
|
+
end
|
35
|
+
child.notify
|
36
|
+
end
|
37
|
+
end
|
24
38
|
end
|
25
39
|
end
|
26
40
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Mongoid #:nodoc:
|
2
2
|
module Associations #:nodoc:
|
3
|
-
class
|
3
|
+
class HasMany < DelegateClass(Array) #:nodoc:
|
4
4
|
|
5
5
|
attr_accessor :association_name
|
6
6
|
|
@@ -12,6 +12,14 @@ module Mongoid #:nodoc:
|
|
12
12
|
object.is_a?(Array) ? object.each(&:notify) : object.notify
|
13
13
|
end
|
14
14
|
|
15
|
+
# Clears the association, and notifies the parents of the removal.
|
16
|
+
def clear
|
17
|
+
object = @documents.first
|
18
|
+
object.changed(true)
|
19
|
+
object.notify_observers(object, true)
|
20
|
+
super
|
21
|
+
end
|
22
|
+
|
15
23
|
# Appends the object to the +Array+, setting its parent in
|
16
24
|
# the process.
|
17
25
|
def push(object)
|
@@ -65,6 +73,19 @@ module Mongoid #:nodoc:
|
|
65
73
|
super(@documents)
|
66
74
|
end
|
67
75
|
|
76
|
+
class << self
|
77
|
+
# Perform an update of the relationship of the parent and child. This
|
78
|
+
# is initialized by setting the has_many to the supplied +Enumerable+
|
79
|
+
# and setting up the parentization.
|
80
|
+
def update(children, parent, name)
|
81
|
+
parent.attributes.delete(name)
|
82
|
+
children.each do |child|
|
83
|
+
child.parentize(parent, name)
|
84
|
+
child.notify
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
68
89
|
end
|
69
90
|
end
|
70
91
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Mongoid #:nodoc:
|
2
2
|
module Associations #:nodoc:
|
3
|
-
class
|
3
|
+
class HasOne #:nodoc:
|
4
4
|
include Decorator
|
5
5
|
|
6
6
|
delegate :valid?, :to => :document
|
@@ -16,10 +16,19 @@ module Mongoid #:nodoc:
|
|
16
16
|
klass = class_name ? class_name.constantize : name.to_s.camelize.constantize
|
17
17
|
attributes = document.attributes[name]
|
18
18
|
@document = klass.new(attributes)
|
19
|
-
@document.
|
19
|
+
@document.parentize(document, name)
|
20
20
|
decorate!
|
21
21
|
end
|
22
22
|
|
23
|
+
class << self
|
24
|
+
# Perform an update of the relationship of the parent and child. This
|
25
|
+
# is initialized by setting the has_one to the supplied child.
|
26
|
+
def update(child, parent, name)
|
27
|
+
child.parentize(parent, name)
|
28
|
+
child.notify
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
23
32
|
end
|
24
33
|
end
|
25
34
|
end
|
data/lib/mongoid/document.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
module Mongoid #:nodoc:
|
2
2
|
class Document
|
3
3
|
include ActiveSupport::Callbacks
|
4
|
-
include Attributes, Commands, Observable, Validatable
|
5
|
-
extend Associations
|
4
|
+
include Associations, Attributes, Commands, Observable, Validatable
|
6
5
|
|
7
6
|
attr_accessor :association_name, :parent
|
8
7
|
attr_reader :attributes, :new_record
|
@@ -199,6 +198,10 @@ module Mongoid #:nodoc:
|
|
199
198
|
generate_key
|
200
199
|
end
|
201
200
|
|
201
|
+
def inspect
|
202
|
+
"Class: #{self.class.name} | Attributes: #{@attributes.inspect} | Parent: #{@parent.class.name}"
|
203
|
+
end
|
204
|
+
|
202
205
|
# Return the +Document+ primary key.
|
203
206
|
def primary_key
|
204
207
|
self.class.primary_key
|
@@ -231,8 +234,9 @@ module Mongoid #:nodoc:
|
|
231
234
|
end
|
232
235
|
|
233
236
|
# Update the document based on notify from child
|
234
|
-
def update(child)
|
235
|
-
@attributes.insert(child.association_name, child.attributes)
|
237
|
+
def update(child, clear = false)
|
238
|
+
@attributes.insert(child.association_name, child.attributes) unless clear
|
239
|
+
@attributes.delete(child.association_name) if clear
|
236
240
|
notify
|
237
241
|
end
|
238
242
|
|
data/mongoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{mongoid}
|
8
|
-
s.version = "0.6.
|
8
|
+
s.version = "0.6.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Durran Jordan"]
|
12
|
-
s.date = %q{2009-11-
|
12
|
+
s.date = %q{2009-11-02}
|
13
13
|
s.email = %q{durran@gmail.com}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"README.textile"
|
@@ -23,11 +23,11 @@ Gem::Specification.new do |s|
|
|
23
23
|
"VERSION",
|
24
24
|
"lib/mongoid.rb",
|
25
25
|
"lib/mongoid/associations.rb",
|
26
|
-
"lib/mongoid/associations/
|
26
|
+
"lib/mongoid/associations/accessor.rb",
|
27
|
+
"lib/mongoid/associations/belongs_to.rb",
|
27
28
|
"lib/mongoid/associations/decorator.rb",
|
28
|
-
"lib/mongoid/associations/
|
29
|
-
"lib/mongoid/associations/
|
30
|
-
"lib/mongoid/associations/has_one_association.rb",
|
29
|
+
"lib/mongoid/associations/has_many.rb",
|
30
|
+
"lib/mongoid/associations/has_one.rb",
|
31
31
|
"lib/mongoid/attributes.rb",
|
32
32
|
"lib/mongoid/commands.rb",
|
33
33
|
"lib/mongoid/commands/create.rb",
|
@@ -61,11 +61,11 @@ Gem::Specification.new do |s|
|
|
61
61
|
"spec/integration/mongoid/document_spec.rb",
|
62
62
|
"spec/spec.opts",
|
63
63
|
"spec/spec_helper.rb",
|
64
|
-
"spec/unit/mongoid/associations/
|
64
|
+
"spec/unit/mongoid/associations/accessor_spec.rb",
|
65
|
+
"spec/unit/mongoid/associations/belongs_to_spec.rb",
|
65
66
|
"spec/unit/mongoid/associations/decorator_spec.rb",
|
66
|
-
"spec/unit/mongoid/associations/
|
67
|
-
"spec/unit/mongoid/associations/
|
68
|
-
"spec/unit/mongoid/associations/has_one_association_spec.rb",
|
67
|
+
"spec/unit/mongoid/associations/has_many_spec.rb",
|
68
|
+
"spec/unit/mongoid/associations/has_one_spec.rb",
|
69
69
|
"spec/unit/mongoid/associations_spec.rb",
|
70
70
|
"spec/unit/mongoid/attributes_spec.rb",
|
71
71
|
"spec/unit/mongoid/commands/create_spec.rb",
|
@@ -103,11 +103,11 @@ Gem::Specification.new do |s|
|
|
103
103
|
s.test_files = [
|
104
104
|
"spec/integration/mongoid/document_spec.rb",
|
105
105
|
"spec/spec_helper.rb",
|
106
|
-
"spec/unit/mongoid/associations/
|
106
|
+
"spec/unit/mongoid/associations/accessor_spec.rb",
|
107
|
+
"spec/unit/mongoid/associations/belongs_to_spec.rb",
|
107
108
|
"spec/unit/mongoid/associations/decorator_spec.rb",
|
108
|
-
"spec/unit/mongoid/associations/
|
109
|
-
"spec/unit/mongoid/associations/
|
110
|
-
"spec/unit/mongoid/associations/has_one_association_spec.rb",
|
109
|
+
"spec/unit/mongoid/associations/has_many_spec.rb",
|
110
|
+
"spec/unit/mongoid/associations/has_one_spec.rb",
|
111
111
|
"spec/unit/mongoid/associations_spec.rb",
|
112
112
|
"spec/unit/mongoid/attributes_spec.rb",
|
113
113
|
"spec/unit/mongoid/commands/create_spec.rb",
|
@@ -10,6 +10,23 @@ class Person < Mongoid::Document
|
|
10
10
|
has_one :name
|
11
11
|
end
|
12
12
|
|
13
|
+
class PetOwner < Mongoid::Document
|
14
|
+
field :title
|
15
|
+
has_one :pet
|
16
|
+
end
|
17
|
+
|
18
|
+
class Pet < Mongoid::Document
|
19
|
+
field :name
|
20
|
+
field :weight, :type => Float, :default => 0.0
|
21
|
+
has_many :vet_visits
|
22
|
+
belongs_to :pet_owner
|
23
|
+
end
|
24
|
+
|
25
|
+
class VetVisit < Mongoid::Document
|
26
|
+
field :date, :type => Date
|
27
|
+
belongs_to :pet
|
28
|
+
end
|
29
|
+
|
13
30
|
class Address < Mongoid::Document
|
14
31
|
field :street
|
15
32
|
field :city
|
@@ -172,6 +189,24 @@ describe Mongoid::Document do
|
|
172
189
|
|
173
190
|
end
|
174
191
|
|
192
|
+
context "when has many exists through a has one" do
|
193
|
+
|
194
|
+
before do
|
195
|
+
@owner = PetOwner.new(:title => "Sir")
|
196
|
+
@pet = Pet.new(:name => "Fido")
|
197
|
+
@visit = VetVisit.new(:date => Date.today)
|
198
|
+
@pet.vet_visits << @visit
|
199
|
+
@owner.pet = @pet
|
200
|
+
end
|
201
|
+
|
202
|
+
it "can clear the association" do
|
203
|
+
@owner.pet.vet_visits.size.should == 1
|
204
|
+
@owner.pet.vet_visits.clear
|
205
|
+
@owner.pet.vet_visits.size.should == 0
|
206
|
+
end
|
207
|
+
|
208
|
+
end
|
209
|
+
|
175
210
|
context "the lot" do
|
176
211
|
|
177
212
|
before do
|
@@ -215,6 +250,22 @@ describe Mongoid::Document do
|
|
215
250
|
|
216
251
|
end
|
217
252
|
|
253
|
+
context "setting belongs_to" do
|
254
|
+
|
255
|
+
before do
|
256
|
+
@person = Person.new(:title => "Mr")
|
257
|
+
@address = Address.new(:street => "Bloomsbury Ave")
|
258
|
+
@person.save!
|
259
|
+
end
|
260
|
+
|
261
|
+
it "allows the parent reference to change" do
|
262
|
+
@address.person = @person
|
263
|
+
@address.save!
|
264
|
+
@person.addresses.first.should == @address
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|
268
|
+
|
218
269
|
context "typecasting" do
|
219
270
|
|
220
271
|
before do
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
+
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
field :title
|
5
|
+
has_many :addresses
|
6
|
+
has_one :name
|
7
|
+
end
|
8
|
+
|
9
|
+
class Address < Mongoid::Document
|
10
|
+
field :street
|
11
|
+
key :street
|
12
|
+
belongs_to :person
|
13
|
+
end
|
14
|
+
|
15
|
+
class Name < Mongoid::Document
|
16
|
+
field :first_name
|
17
|
+
field :last_name
|
18
|
+
key :first_name, :last_name
|
19
|
+
belongs_to :person
|
20
|
+
end
|
21
|
+
|
22
|
+
describe Mongoid::Associations::Accessor do
|
23
|
+
|
24
|
+
describe "#get" do
|
25
|
+
|
26
|
+
before do
|
27
|
+
@document = Person.new
|
28
|
+
@object = stub
|
29
|
+
end
|
30
|
+
|
31
|
+
context "when type is has_many" do
|
32
|
+
|
33
|
+
it "returns a HasMany" do
|
34
|
+
association = Mongoid::Associations::Accessor.get(:has_many, :addresses, @document)
|
35
|
+
association.should be_a_kind_of(Mongoid::Associations::HasMany)
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when type is has_one" do
|
41
|
+
|
42
|
+
it "returns a HasOne" do
|
43
|
+
association = Mongoid::Associations::Accessor.get(:has_one, :name, @document)
|
44
|
+
association.should be_a_kind_of(Mongoid::Associations::HasOne)
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
context "when type is belongs_to" do
|
50
|
+
|
51
|
+
it "returns a BelongsTo" do
|
52
|
+
association = Mongoid::Associations::Accessor.get(:belongs_to, :person, @document)
|
53
|
+
association.should be_a_kind_of(Mongoid::Associations::BelongsTo)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
context "when type is invalid" do
|
59
|
+
|
60
|
+
it "raises an InvalidAssociationError" do
|
61
|
+
lambda { Mongoid::Associations::Accessor.get(:something, :person, @document) }.should raise_error
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "#set" do
|
69
|
+
|
70
|
+
context "when type is has_many" do
|
71
|
+
|
72
|
+
it "returns a HasMany" do
|
73
|
+
Mongoid::Associations::HasMany.expects(:update).with(@document, @object, :addresses)
|
74
|
+
Mongoid::Associations::Accessor.set(:has_many, :addresses, @document, @object)
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
context "when type is has_one" do
|
80
|
+
|
81
|
+
it "returns a HasOne" do
|
82
|
+
Mongoid::Associations::HasOne.expects(:update).with(@document, @object, :name)
|
83
|
+
Mongoid::Associations::Accessor.set(:has_one, :name, @document, @object)
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
context "when type is belongs_to" do
|
89
|
+
|
90
|
+
it "returns a BelongsTo" do
|
91
|
+
Mongoid::Associations::BelongsTo.expects(:update).with(@document, @object)
|
92
|
+
Mongoid::Associations::Accessor.set(:belongs_to, :person, @document, @object)
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
context "when type is invalid" do
|
98
|
+
|
99
|
+
it "raises an InvalidAssociationError" do
|
100
|
+
lambda {
|
101
|
+
Mongoid::Associations::Accessor.set(:something, :person, @document, @object)
|
102
|
+
}.should raise_error
|
103
|
+
end
|
104
|
+
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
+
|
3
|
+
class Person < Mongoid::Document
|
4
|
+
field :title
|
5
|
+
has_one :name
|
6
|
+
has_many :addresses
|
7
|
+
end
|
8
|
+
|
9
|
+
class Name < Mongoid::Document
|
10
|
+
field :first_name
|
11
|
+
field :last_name
|
12
|
+
key :first_name, :last_name
|
13
|
+
belongs_to :person
|
14
|
+
end
|
15
|
+
|
16
|
+
class Address < Mongoid::Document
|
17
|
+
field :street
|
18
|
+
belongs_to :person
|
19
|
+
end
|
20
|
+
|
21
|
+
describe Mongoid::Associations::BelongsTo do
|
22
|
+
|
23
|
+
describe "#update" do
|
24
|
+
|
25
|
+
context "when child is a has one" do
|
26
|
+
|
27
|
+
before do
|
28
|
+
@name = Name.new(:first_name => "Test", :last_name => "User")
|
29
|
+
@person = Person.new(:title => "Mrs")
|
30
|
+
Mongoid::Associations::BelongsTo.update(@name, @person)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "updates the parent document" do
|
34
|
+
@person.name.should == @name
|
35
|
+
@person.attributes[:name].except(:_id).should ==
|
36
|
+
{ "first_name" => "Test", "last_name" => "User" }
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when child is a has many" do
|
42
|
+
|
43
|
+
before do
|
44
|
+
@address = Address.new(:street => "Broadway")
|
45
|
+
@person = Person.new(:title => "Mrs")
|
46
|
+
Mongoid::Associations::BelongsTo.update(@address, @person)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "updates the parent document" do
|
50
|
+
@person.addresses.first.should == @address
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "#find" do
|
58
|
+
|
59
|
+
before do
|
60
|
+
@parent = Name.new(:first_name => "Drexel")
|
61
|
+
@document = stub(:parent => @parent)
|
62
|
+
@association = Mongoid::Associations::BelongsTo.new(@document)
|
63
|
+
end
|
64
|
+
|
65
|
+
context "when finding by id" do
|
66
|
+
|
67
|
+
it "returns the document in the array with that id" do
|
68
|
+
name = @association.find(Mongo::ObjectID.new.to_s)
|
69
|
+
name.should == @parent
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
context "when decorating" do
|
77
|
+
|
78
|
+
before do
|
79
|
+
@parent = Name.new(:first_name => "Drexel")
|
80
|
+
@document = stub(:parent => @parent)
|
81
|
+
@association = Mongoid::Associations::BelongsTo.new(@document)
|
82
|
+
end
|
83
|
+
|
84
|
+
context "when getting values" do
|
85
|
+
|
86
|
+
it "delegates to the document" do
|
87
|
+
@association.first_name.should == "Drexel"
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
context "when setting values" do
|
93
|
+
|
94
|
+
it "delegates to the document" do
|
95
|
+
@association.first_name = "Test"
|
96
|
+
@association.first_name.should == "Test"
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
@@ -11,7 +11,7 @@ class Address < Mongoid::Document
|
|
11
11
|
belongs_to :person
|
12
12
|
end
|
13
13
|
|
14
|
-
describe Mongoid::Associations::
|
14
|
+
describe Mongoid::Associations::HasMany do
|
15
15
|
|
16
16
|
before do
|
17
17
|
@attributes = { :addresses => [
|
@@ -20,10 +20,29 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
20
20
|
@document = stub(:attributes => @attributes, :add_observer => true, :update => true)
|
21
21
|
end
|
22
22
|
|
23
|
+
describe "#update" do
|
24
|
+
|
25
|
+
before do
|
26
|
+
@address = Address.new(:street => "Madison Ave")
|
27
|
+
@person = Person.new(:title => "Sir")
|
28
|
+
Mongoid::Associations::HasMany.update([@address], @person, :addresses)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "parentizes the child document" do
|
32
|
+
@address.parent.should == @person
|
33
|
+
end
|
34
|
+
|
35
|
+
it "sets the attributes of the child on the parent" do
|
36
|
+
@person.attributes[:addresses].should ==
|
37
|
+
[{ "_id" => "madison-ave", "street" => "Madison Ave" }]
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
23
42
|
describe "#[]" do
|
24
43
|
|
25
44
|
before do
|
26
|
-
@association = Mongoid::Associations::
|
45
|
+
@association = Mongoid::Associations::HasMany.new(:addresses, @document)
|
27
46
|
end
|
28
47
|
|
29
48
|
context "when the index is present in the association" do
|
@@ -48,7 +67,7 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
48
67
|
describe "#<<" do
|
49
68
|
|
50
69
|
before do
|
51
|
-
@association = Mongoid::Associations::
|
70
|
+
@association = Mongoid::Associations::HasMany.new(:addresses, @document)
|
52
71
|
@address = Address.new
|
53
72
|
end
|
54
73
|
|
@@ -69,7 +88,7 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
69
88
|
describe "#concat" do
|
70
89
|
|
71
90
|
before do
|
72
|
-
@association = Mongoid::Associations::
|
91
|
+
@association = Mongoid::Associations::HasMany.new(:addresses, @document)
|
73
92
|
@address = Address.new
|
74
93
|
end
|
75
94
|
|
@@ -84,7 +103,7 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
84
103
|
describe "#push" do
|
85
104
|
|
86
105
|
before do
|
87
|
-
@association = Mongoid::Associations::
|
106
|
+
@association = Mongoid::Associations::HasMany.new(:addresses, @document)
|
88
107
|
@address = Address.new
|
89
108
|
end
|
90
109
|
|
@@ -99,7 +118,7 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
99
118
|
describe "#build" do
|
100
119
|
|
101
120
|
before do
|
102
|
-
@association = Mongoid::Associations::
|
121
|
+
@association = Mongoid::Associations::HasMany.new(:addresses, @document)
|
103
122
|
end
|
104
123
|
|
105
124
|
it "adds a new document to the array with the suppied parameters" do
|
@@ -120,7 +139,7 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
120
139
|
describe "#find" do
|
121
140
|
|
122
141
|
before do
|
123
|
-
@association = Mongoid::Associations::
|
142
|
+
@association = Mongoid::Associations::HasMany.new(:addresses, @document)
|
124
143
|
end
|
125
144
|
|
126
145
|
context "when finding all" do
|
@@ -147,7 +166,7 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
147
166
|
context "when there are elements in the array" do
|
148
167
|
|
149
168
|
before do
|
150
|
-
@association = Mongoid::Associations::
|
169
|
+
@association = Mongoid::Associations::HasMany.new(:addresses, @document)
|
151
170
|
end
|
152
171
|
|
153
172
|
it "returns the first element" do
|
@@ -160,7 +179,7 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
160
179
|
context "when the array is empty" do
|
161
180
|
|
162
181
|
before do
|
163
|
-
@association = Mongoid::Associations::
|
182
|
+
@association = Mongoid::Associations::HasMany.new(:addresses, Person.new)
|
164
183
|
end
|
165
184
|
|
166
185
|
it "returns nil" do
|
@@ -176,7 +195,7 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
176
195
|
context "#length" do
|
177
196
|
|
178
197
|
it "returns the length of the delegated array" do
|
179
|
-
@association = Mongoid::Associations::
|
198
|
+
@association = Mongoid::Associations::HasMany.new(:addresses, @document)
|
180
199
|
@association.length.should == 2
|
181
200
|
end
|
182
201
|
|
@@ -187,7 +206,7 @@ describe Mongoid::Associations::HasManyAssociation do
|
|
187
206
|
describe "#push" do
|
188
207
|
|
189
208
|
before do
|
190
|
-
@association = Mongoid::Associations::
|
209
|
+
@association = Mongoid::Associations::HasMany.new(:addresses, @document)
|
191
210
|
end
|
192
211
|
|
193
212
|
it "appends the document to the end of the array" do
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
+
|
3
|
+
class MixedDrink < Mongoid::Document
|
4
|
+
field :name
|
5
|
+
end
|
6
|
+
|
7
|
+
class Person < Mongoid::Document
|
8
|
+
field :title
|
9
|
+
has_one :name
|
10
|
+
end
|
11
|
+
|
12
|
+
class Name < Mongoid::Document
|
13
|
+
field :first_name
|
14
|
+
key :first_name
|
15
|
+
belongs_to :person
|
16
|
+
end
|
17
|
+
|
18
|
+
describe Mongoid::Associations::HasOne do
|
19
|
+
|
20
|
+
before do
|
21
|
+
@attributes = { :mixed_drink => { :name => "Jack and Coke" } }
|
22
|
+
@document = stub(:attributes => @attributes, :update => true)
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#update" do
|
26
|
+
|
27
|
+
before do
|
28
|
+
@name = Name.new(:first_name => "Donald")
|
29
|
+
@person = Person.new(:title => "Sir")
|
30
|
+
Mongoid::Associations::HasOne.update(@name, @person, :name)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "parentizes the child document" do
|
34
|
+
@name.parent.should == @person
|
35
|
+
end
|
36
|
+
|
37
|
+
it "sets the attributes of the child on the parent" do
|
38
|
+
@person.attributes[:name].should ==
|
39
|
+
{ "_id" => "donald", "first_name" => "Donald" }
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#decorate!" do
|
45
|
+
|
46
|
+
before do
|
47
|
+
@association = Mongoid::Associations::HasOne.new(:mixed_drink, @document)
|
48
|
+
end
|
49
|
+
|
50
|
+
context "when getting values" do
|
51
|
+
|
52
|
+
it "delegates to the document" do
|
53
|
+
@association.name.should == "Jack and Coke"
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
context "when setting values" do
|
59
|
+
|
60
|
+
it "delegates to the document" do
|
61
|
+
@association.name = "Jack and Coke"
|
62
|
+
@association.name.should == "Jack and Coke"
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
@@ -79,6 +79,25 @@ describe Mongoid::Associations do
|
|
79
79
|
|
80
80
|
end
|
81
81
|
|
82
|
+
context "when setting a parent" do
|
83
|
+
|
84
|
+
before do
|
85
|
+
@person = Person.new(:title => "Mr")
|
86
|
+
@address = Address.new(:street => "Picadilly Circus")
|
87
|
+
@address.person = @person
|
88
|
+
end
|
89
|
+
|
90
|
+
it "re-parentizes the association" do
|
91
|
+
@address.parent.should == @person
|
92
|
+
end
|
93
|
+
|
94
|
+
it "adds the child attributes to the parent" do
|
95
|
+
@person.attributes[:addresses].should ==
|
96
|
+
[{ "_id" => "picadilly-circus", "street" => "Picadilly Circus" }]
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
|
82
101
|
end
|
83
102
|
|
84
103
|
describe "#belongs_to" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Durran Jordan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-02 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -79,11 +79,11 @@ files:
|
|
79
79
|
- VERSION
|
80
80
|
- lib/mongoid.rb
|
81
81
|
- lib/mongoid/associations.rb
|
82
|
-
- lib/mongoid/associations/
|
82
|
+
- lib/mongoid/associations/accessor.rb
|
83
|
+
- lib/mongoid/associations/belongs_to.rb
|
83
84
|
- lib/mongoid/associations/decorator.rb
|
84
|
-
- lib/mongoid/associations/
|
85
|
-
- lib/mongoid/associations/
|
86
|
-
- lib/mongoid/associations/has_one_association.rb
|
85
|
+
- lib/mongoid/associations/has_many.rb
|
86
|
+
- lib/mongoid/associations/has_one.rb
|
87
87
|
- lib/mongoid/attributes.rb
|
88
88
|
- lib/mongoid/commands.rb
|
89
89
|
- lib/mongoid/commands/create.rb
|
@@ -117,11 +117,11 @@ files:
|
|
117
117
|
- spec/integration/mongoid/document_spec.rb
|
118
118
|
- spec/spec.opts
|
119
119
|
- spec/spec_helper.rb
|
120
|
-
- spec/unit/mongoid/associations/
|
120
|
+
- spec/unit/mongoid/associations/accessor_spec.rb
|
121
|
+
- spec/unit/mongoid/associations/belongs_to_spec.rb
|
121
122
|
- spec/unit/mongoid/associations/decorator_spec.rb
|
122
|
-
- spec/unit/mongoid/associations/
|
123
|
-
- spec/unit/mongoid/associations/
|
124
|
-
- spec/unit/mongoid/associations/has_one_association_spec.rb
|
123
|
+
- spec/unit/mongoid/associations/has_many_spec.rb
|
124
|
+
- spec/unit/mongoid/associations/has_one_spec.rb
|
125
125
|
- spec/unit/mongoid/associations_spec.rb
|
126
126
|
- spec/unit/mongoid/attributes_spec.rb
|
127
127
|
- spec/unit/mongoid/commands/create_spec.rb
|
@@ -181,11 +181,11 @@ summary: ODM framework for MongoDB
|
|
181
181
|
test_files:
|
182
182
|
- spec/integration/mongoid/document_spec.rb
|
183
183
|
- spec/spec_helper.rb
|
184
|
-
- spec/unit/mongoid/associations/
|
184
|
+
- spec/unit/mongoid/associations/accessor_spec.rb
|
185
|
+
- spec/unit/mongoid/associations/belongs_to_spec.rb
|
185
186
|
- spec/unit/mongoid/associations/decorator_spec.rb
|
186
|
-
- spec/unit/mongoid/associations/
|
187
|
-
- spec/unit/mongoid/associations/
|
188
|
-
- spec/unit/mongoid/associations/has_one_association_spec.rb
|
187
|
+
- spec/unit/mongoid/associations/has_many_spec.rb
|
188
|
+
- spec/unit/mongoid/associations/has_one_spec.rb
|
189
189
|
- spec/unit/mongoid/associations_spec.rb
|
190
190
|
- spec/unit/mongoid/attributes_spec.rb
|
191
191
|
- spec/unit/mongoid/commands/create_spec.rb
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Mongoid #:nodoc:
|
2
|
-
module Associations #:nodoc:
|
3
|
-
class Factory #:nodoc:
|
4
|
-
|
5
|
-
# Creates a new association, based on the type provided and
|
6
|
-
# passes the name and document into the newly instantiated
|
7
|
-
# association.
|
8
|
-
#
|
9
|
-
# If the type is invalid a InvalidAssociationError will be thrown.
|
10
|
-
def self.create(association_type, association_name, document, options = {})
|
11
|
-
case association_type
|
12
|
-
when :belongs_to then BelongsToAssociation.new(document)
|
13
|
-
when :has_many then HasManyAssociation.new(association_name, document, options)
|
14
|
-
when :has_one then HasOneAssociation.new(association_name, document, options)
|
15
|
-
else raise InvalidAssociationError
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,59 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Name < Mongoid::Document
|
4
|
-
field :first_name
|
5
|
-
field :last_name
|
6
|
-
key :first_name, :last_name
|
7
|
-
belongs_to :person
|
8
|
-
end
|
9
|
-
|
10
|
-
describe Mongoid::Associations::BelongsToAssociation do
|
11
|
-
|
12
|
-
before do
|
13
|
-
@parent = Name.new(:first_name => "Drexel")
|
14
|
-
@document = stub(:parent => @parent)
|
15
|
-
end
|
16
|
-
|
17
|
-
describe "#find" do
|
18
|
-
|
19
|
-
before do
|
20
|
-
@association = Mongoid::Associations::BelongsToAssociation.new(@document)
|
21
|
-
end
|
22
|
-
|
23
|
-
context "when finding by id" do
|
24
|
-
|
25
|
-
it "returns the document in the array with that id" do
|
26
|
-
name = @association.find(Mongo::ObjectID.new.to_s)
|
27
|
-
name.should == @parent
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
context "when decorating" do
|
35
|
-
|
36
|
-
before do
|
37
|
-
@association = Mongoid::Associations::BelongsToAssociation.new(@document)
|
38
|
-
end
|
39
|
-
|
40
|
-
context "when getting values" do
|
41
|
-
|
42
|
-
it "delegates to the document" do
|
43
|
-
@association.first_name.should == "Drexel"
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
context "when setting values" do
|
49
|
-
|
50
|
-
it "delegates to the document" do
|
51
|
-
@association.first_name = "Test"
|
52
|
-
@association.first_name.should == "Test"
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
@@ -1,67 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
-
|
3
|
-
class Person < Mongoid::Document
|
4
|
-
field :title
|
5
|
-
has_many :addresses
|
6
|
-
has_one :name
|
7
|
-
end
|
8
|
-
|
9
|
-
class Address < Mongoid::Document
|
10
|
-
field :street
|
11
|
-
key :street
|
12
|
-
belongs_to :person
|
13
|
-
end
|
14
|
-
|
15
|
-
class Name < Mongoid::Document
|
16
|
-
field :first_name
|
17
|
-
field :last_name
|
18
|
-
key :first_name, :last_name
|
19
|
-
belongs_to :person
|
20
|
-
end
|
21
|
-
|
22
|
-
describe Mongoid::Associations::Factory do
|
23
|
-
|
24
|
-
describe "#create" do
|
25
|
-
|
26
|
-
before do
|
27
|
-
@document = Person.new
|
28
|
-
end
|
29
|
-
|
30
|
-
context "when type is has_many" do
|
31
|
-
|
32
|
-
it "returns a HasManyAssociationProxy" do
|
33
|
-
association = Mongoid::Associations::Factory.create(:has_many, :addresses, @document)
|
34
|
-
association.should be_a_kind_of(Mongoid::Associations::HasManyAssociation)
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
context "when type is has_one" do
|
40
|
-
|
41
|
-
it "returns a HashOneAssociationProxy" do
|
42
|
-
association = Mongoid::Associations::Factory.create(:has_one, :name, @document)
|
43
|
-
association.should be_a_kind_of(Mongoid::Associations::HasOneAssociation)
|
44
|
-
end
|
45
|
-
|
46
|
-
end
|
47
|
-
|
48
|
-
context "when type is belongs_to" do
|
49
|
-
|
50
|
-
it "returns a BelongsToAssociationProxy" do
|
51
|
-
association = Mongoid::Associations::Factory.create(:belongs_to, :person, @document)
|
52
|
-
association.should be_a_kind_of(Mongoid::Associations::BelongsToAssociation)
|
53
|
-
end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
context "when type is invalid" do
|
58
|
-
|
59
|
-
it "raises an InvalidAssociationError" do
|
60
|
-
lambda { Mongoid::Associations::Factory.create(:something, :person, @document) }.should raise_error
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), "/../../../spec_helper.rb")
|
2
|
-
|
3
|
-
class MixedDrink < Mongoid::Document
|
4
|
-
field :name
|
5
|
-
end
|
6
|
-
|
7
|
-
describe Mongoid::Associations::HasOneAssociation do
|
8
|
-
|
9
|
-
before do
|
10
|
-
@attributes = { :mixed_drink => { :name => "Jack and Coke" } }
|
11
|
-
@document = stub(:attributes => @attributes)
|
12
|
-
end
|
13
|
-
|
14
|
-
describe "#decorate!" do
|
15
|
-
|
16
|
-
before do
|
17
|
-
@association = Mongoid::Associations::HasOneAssociation.new(:mixed_drink, @document)
|
18
|
-
end
|
19
|
-
|
20
|
-
context "when getting values" do
|
21
|
-
|
22
|
-
it "delegates to the document" do
|
23
|
-
@association.name.should == "Jack and Coke"
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
context "when setting values" do
|
29
|
-
|
30
|
-
it "delegates to the document" do
|
31
|
-
@association.name = "Jack and Coke"
|
32
|
-
@association.name.should == "Jack and Coke"
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|
36
|
-
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|