mongoid 0.9.8 → 0.9.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +5 -4
- data/VERSION +1 -1
- data/lib/mongoid.rb +3 -28
- data/lib/mongoid/associations.rb +63 -44
- data/lib/mongoid/associations/{relates_to_one.rb → belongs_to_related.rb} +4 -4
- data/lib/mongoid/associations/has_many.rb +26 -12
- data/lib/mongoid/associations/has_many_related.rb +100 -0
- data/lib/mongoid/associations/has_one.rb +13 -2
- data/lib/mongoid/associations/{relates_to_many.rb → has_one_related.rb} +34 -9
- data/lib/mongoid/attributes.rb +108 -8
- data/lib/mongoid/commands.rb +3 -11
- data/lib/mongoid/commands/save.rb +2 -6
- data/lib/mongoid/document.rb +6 -70
- data/lib/mongoid/errors.rb +34 -0
- data/mongoid.gemspec +14 -11
- data/spec/integration/mongoid/associations_spec.rb +19 -14
- data/spec/spec_helper.rb +6 -3
- data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +112 -0
- data/spec/unit/mongoid/associations/has_many_related_spec.rb +292 -0
- data/spec/unit/mongoid/associations/has_many_spec.rb +17 -0
- data/spec/unit/mongoid/associations/has_one_related_spec.rb +130 -0
- data/spec/unit/mongoid/associations/has_one_spec.rb +53 -0
- data/spec/unit/mongoid/associations_spec.rb +36 -8
- data/spec/unit/mongoid/attributes_spec.rb +218 -0
- data/spec/unit/mongoid/commands/save_spec.rb +4 -2
- data/spec/unit/mongoid/commands_spec.rb +4 -17
- data/spec/unit/mongoid/criteria_spec.rb +0 -5
- data/spec/unit/mongoid/document_spec.rb +26 -156
- data/spec/unit/mongoid/errors_spec.rb +87 -0
- metadata +14 -11
- data/lib/mongoid/commands/quick_save.rb +0 -19
- data/spec/unit/mongoid/associations/relates_to_many_spec.rb +0 -76
- data/spec/unit/mongoid/associations/relates_to_one_spec.rb +0 -112
- data/spec/unit/mongoid/commands/quick_save_spec.rb +0 -24
@@ -6,21 +6,26 @@ describe Mongoid::Associations do
|
|
6
6
|
Person.delete_all; Game.delete_all; Post.delete_all
|
7
7
|
end
|
8
8
|
|
9
|
-
|
9
|
+
context "one-to-one relational associations" do
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
before do
|
12
|
+
@person = Person.new(:title => "Sir")
|
13
|
+
@game = Game.new(:score => 1)
|
14
|
+
@person.game = @game
|
15
|
+
@person.save
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
it "sets the association on save" do
|
19
|
+
@from_db = Person.find(@person.id)
|
20
|
+
@from_db.game.should == @game
|
21
|
+
end
|
22
|
+
|
23
|
+
it "sets the reverse association" do
|
24
|
+
@from_db = Game.find(@game.id)
|
25
|
+
@game.person.should == @person
|
26
|
+
end
|
22
27
|
|
23
|
-
|
28
|
+
end
|
24
29
|
|
25
30
|
context "one-to_many relational associations" do
|
26
31
|
|
@@ -32,8 +37,8 @@ describe Mongoid::Associations do
|
|
32
37
|
end
|
33
38
|
|
34
39
|
it "sets the association on save" do
|
35
|
-
|
36
|
-
|
40
|
+
@from_db = Person.find(@person.id)
|
41
|
+
@from_db.posts.should == [@post]
|
37
42
|
end
|
38
43
|
|
39
44
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -37,13 +37,15 @@ class Person < Mongoid::Document
|
|
37
37
|
has_one :name
|
38
38
|
has_one :pet, :class_name => "Animal"
|
39
39
|
|
40
|
+
accepts_nested_attributes_for :addresses, :name
|
41
|
+
|
40
42
|
index :title
|
41
43
|
index :dob
|
42
44
|
index :addresses
|
43
45
|
index :name
|
44
46
|
|
45
|
-
|
46
|
-
|
47
|
+
has_one_related :game
|
48
|
+
has_many_related :posts
|
47
49
|
|
48
50
|
def update_addresses
|
49
51
|
addresses.each_with_index do |address, i|
|
@@ -136,12 +138,13 @@ end
|
|
136
138
|
class Post < Mongoid::Document
|
137
139
|
include Mongoid::Versioning
|
138
140
|
field :title
|
139
|
-
|
141
|
+
belongs_to_related :person
|
140
142
|
end
|
141
143
|
|
142
144
|
class Game < Mongoid::Document
|
143
145
|
field :high_score, :default => 500
|
144
146
|
field :score, :type => Integer, :default => 0
|
147
|
+
belongs_to_related :person
|
145
148
|
end
|
146
149
|
|
147
150
|
if RUBY_VERSION == '1.8.6'
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Associations::BelongsToRelated do
|
4
|
+
|
5
|
+
describe ".initialize" do
|
6
|
+
|
7
|
+
context "when related id has been set" do
|
8
|
+
|
9
|
+
before do
|
10
|
+
@document = stub(:person_id => "5")
|
11
|
+
@options = Mongoid::Associations::Options.new(:name => :person)
|
12
|
+
@related = stub
|
13
|
+
end
|
14
|
+
|
15
|
+
it "finds the object by id" do
|
16
|
+
Person.expects(:find).with(@document.person_id).returns(@related)
|
17
|
+
association = Mongoid::Associations::BelongsToRelated.new(@document, "5", @options)
|
18
|
+
association.should == @related
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe ".instantiate" do
|
26
|
+
|
27
|
+
context "when foreign key is not nil" do
|
28
|
+
|
29
|
+
before do
|
30
|
+
@document = stub(:person_id => "5")
|
31
|
+
@options = Mongoid::Associations::Options.new(:name => :person)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "delegates to new" do
|
35
|
+
Mongoid::Associations::BelongsToRelated.expects(:new).with(@document, "5", @options)
|
36
|
+
Mongoid::Associations::BelongsToRelated.instantiate(@document, @options)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
context "when foreign key is nil" do
|
42
|
+
|
43
|
+
before do
|
44
|
+
@document = stub(:person_id => nil)
|
45
|
+
@options = Mongoid::Associations::Options.new(:name => :person)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "returns nil" do
|
49
|
+
Mongoid::Associations::BelongsToRelated.instantiate(@document, @options).should be_nil
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#method_missing" do
|
57
|
+
|
58
|
+
before do
|
59
|
+
@person = Person.new(:title => "Mr")
|
60
|
+
@document = stub(:person_id => "5")
|
61
|
+
@options = Mongoid::Associations::Options.new(:name => :person)
|
62
|
+
Person.expects(:find).with(@document.person_id).returns(@person)
|
63
|
+
@association = Mongoid::Associations::BelongsToRelated.new(@document, "5", @options)
|
64
|
+
end
|
65
|
+
|
66
|
+
context "when getting values" do
|
67
|
+
|
68
|
+
it "delegates to the document" do
|
69
|
+
@association.title.should == "Mr"
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
context "when setting values" do
|
75
|
+
|
76
|
+
it "delegates to the document" do
|
77
|
+
@association.title = "Sir"
|
78
|
+
@association.title.should == "Sir"
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
describe ".macro" do
|
86
|
+
|
87
|
+
it "returns :belongs_to_related" do
|
88
|
+
Mongoid::Associations::BelongsToRelated.macro.should == :belongs_to_related
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
describe ".update" do
|
94
|
+
|
95
|
+
before do
|
96
|
+
@related = stub(:id => "5")
|
97
|
+
@child = Game.new
|
98
|
+
@options = Mongoid::Associations::Options.new(:name => :person)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "sets the related object id on the parent" do
|
102
|
+
Mongoid::Associations::BelongsToRelated.update(@related, @child, @options)
|
103
|
+
@child.person_id.should == "5"
|
104
|
+
end
|
105
|
+
|
106
|
+
it "returns the related object" do
|
107
|
+
Mongoid::Associations::BelongsToRelated.update(@related, @child, @options).should == @related
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
@@ -0,0 +1,292 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::Associations::HasManyRelated do
|
4
|
+
|
5
|
+
let(:options) { Mongoid::Associations::Options.new(:name => :posts) }
|
6
|
+
|
7
|
+
describe "#<<" do
|
8
|
+
|
9
|
+
before do
|
10
|
+
@child = stub
|
11
|
+
@second = stub
|
12
|
+
@children = [@child, @second]
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when parent document has been saved" do
|
16
|
+
|
17
|
+
before do
|
18
|
+
@parent = stub(:id => "1", :new_record? => false, :class => Person)
|
19
|
+
Post.expects(:all).returns([])
|
20
|
+
@association = Mongoid::Associations::HasManyRelated.new(@parent, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "saves and appends the child document" do
|
24
|
+
@child.expects(:person_id=).with(@parent.id)
|
25
|
+
@child.expects(:save).returns(true)
|
26
|
+
@association << @child
|
27
|
+
@association.size.should == 1
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when parent document has not been saved" do
|
33
|
+
|
34
|
+
before do
|
35
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
36
|
+
Post.expects(:all).returns([])
|
37
|
+
@association = Mongoid::Associations::HasManyRelated.new(@parent, options)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "appends the child document" do
|
41
|
+
@child.expects(:person_id=).with(@parent.id)
|
42
|
+
@association << @child
|
43
|
+
@association.size.should == 1
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
context "with multiple objects" do
|
49
|
+
|
50
|
+
before do
|
51
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
52
|
+
Post.expects(:all).returns([])
|
53
|
+
@association = Mongoid::Associations::HasManyRelated.new(@parent, options)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "appends the child documents" do
|
57
|
+
@child.expects(:person_id=).with(@parent.id)
|
58
|
+
@second.expects(:person_id=).with(@parent.id)
|
59
|
+
@association << [@child, @second]
|
60
|
+
@association.size.should == 2
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
describe "#build" do
|
68
|
+
|
69
|
+
before do
|
70
|
+
@parent = stub(:id => "5", :class => Person)
|
71
|
+
Post.expects(:all).returns([])
|
72
|
+
@association = Mongoid::Associations::HasManyRelated.new(@parent, options)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "adds a new object to the association" do
|
76
|
+
@association.build(:title => "Sassy")
|
77
|
+
@association.size.should == 1
|
78
|
+
end
|
79
|
+
|
80
|
+
it "sets the parent object id on the child" do
|
81
|
+
@association.build(:title => "Sassy")
|
82
|
+
@association.first.person_id.should == @parent.id
|
83
|
+
end
|
84
|
+
|
85
|
+
it "returns the new object" do
|
86
|
+
@association.build(:title => "Sassy").title.should == "Sassy"
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
describe "#concat" do
|
92
|
+
|
93
|
+
before do
|
94
|
+
@child = stub
|
95
|
+
@second = stub
|
96
|
+
end
|
97
|
+
|
98
|
+
context "when parent document has been saved" do
|
99
|
+
|
100
|
+
before do
|
101
|
+
@parent = stub(:id => "1", :new_record? => false, :class => Person)
|
102
|
+
Post.expects(:all).returns([])
|
103
|
+
@association = Mongoid::Associations::HasManyRelated.new(@parent, options)
|
104
|
+
end
|
105
|
+
|
106
|
+
it "saves and appends the child document" do
|
107
|
+
@child.expects(:person_id=).with(@parent.id)
|
108
|
+
@child.expects(:save).returns(true)
|
109
|
+
@association.concat(@child)
|
110
|
+
@association.size.should == 1
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
context "when parent document has not been saved" do
|
116
|
+
|
117
|
+
before do
|
118
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
119
|
+
Post.expects(:all).returns([])
|
120
|
+
@association = Mongoid::Associations::HasManyRelated.new(@parent, options)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "appends the child document" do
|
124
|
+
@child.expects(:person_id=).with(@parent.id)
|
125
|
+
@association.concat(@child)
|
126
|
+
@association.size.should == 1
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
130
|
+
|
131
|
+
context "with multiple objects" do
|
132
|
+
|
133
|
+
before do
|
134
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
135
|
+
Post.expects(:all).returns([])
|
136
|
+
@association = Mongoid::Associations::HasManyRelated.new(@parent, options)
|
137
|
+
end
|
138
|
+
|
139
|
+
it "appends the child documents" do
|
140
|
+
@child.expects(:person_id=).with(@parent.id)
|
141
|
+
@second.expects(:person_id=).with(@parent.id)
|
142
|
+
@association.concat([@child, @second])
|
143
|
+
@association.size.should == 2
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "#create" do
|
151
|
+
|
152
|
+
before do
|
153
|
+
@parent = stub(:id => "5", :class => Person)
|
154
|
+
Post.expects(:all).returns([])
|
155
|
+
Mongoid::Commands::Save.expects(:execute)
|
156
|
+
@association = Mongoid::Associations::HasManyRelated.new(@parent, options)
|
157
|
+
end
|
158
|
+
|
159
|
+
it "builds and saves the new object" do
|
160
|
+
@association.create(:title => "Sassy")
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
describe ".initialize" do
|
166
|
+
|
167
|
+
context "when related id has been set" do
|
168
|
+
|
169
|
+
before do
|
170
|
+
@document = Person.new
|
171
|
+
@criteria = stub
|
172
|
+
@first = stub(:person_id => @document.id)
|
173
|
+
@second = stub(:person_id => @document.id)
|
174
|
+
@related = [@first, @second]
|
175
|
+
end
|
176
|
+
|
177
|
+
it "finds the object by id" do
|
178
|
+
Post.expects(:all).with(:conditions => { "person_id" => @document.id }).returns(@related)
|
179
|
+
association = Mongoid::Associations::HasManyRelated.new(@document, options)
|
180
|
+
association.should == @related
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
describe ".instantiate" do
|
188
|
+
|
189
|
+
context "when related id has been set" do
|
190
|
+
|
191
|
+
before do
|
192
|
+
@document = Person.new
|
193
|
+
end
|
194
|
+
|
195
|
+
it "delegates to new" do
|
196
|
+
Mongoid::Associations::HasManyRelated.expects(:new).with(@document, options)
|
197
|
+
association = Mongoid::Associations::HasManyRelated.instantiate(@document, options)
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
203
|
+
|
204
|
+
describe ".macro" do
|
205
|
+
|
206
|
+
it "returns :has_many_related" do
|
207
|
+
Mongoid::Associations::HasManyRelated.macro.should == :has_many_related
|
208
|
+
end
|
209
|
+
|
210
|
+
end
|
211
|
+
|
212
|
+
describe "#push" do
|
213
|
+
|
214
|
+
before do
|
215
|
+
@child = stub
|
216
|
+
@second = stub
|
217
|
+
end
|
218
|
+
|
219
|
+
context "when parent document has been saved" do
|
220
|
+
|
221
|
+
before do
|
222
|
+
@parent = stub(:id => "1", :new_record? => false, :class => Person)
|
223
|
+
Post.expects(:all).returns([])
|
224
|
+
@association = Mongoid::Associations::HasManyRelated.new(@parent, options)
|
225
|
+
end
|
226
|
+
|
227
|
+
it "saves and appends the child document" do
|
228
|
+
@child.expects(:person_id=).with(@parent.id)
|
229
|
+
@child.expects(:save).returns(true)
|
230
|
+
@association.push(@child)
|
231
|
+
@association.size.should == 1
|
232
|
+
end
|
233
|
+
|
234
|
+
end
|
235
|
+
|
236
|
+
context "when parent document has not been saved" do
|
237
|
+
|
238
|
+
before do
|
239
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
240
|
+
Post.expects(:all).returns([])
|
241
|
+
@association = Mongoid::Associations::HasManyRelated.new(@parent, options)
|
242
|
+
end
|
243
|
+
|
244
|
+
it "appends the child document" do
|
245
|
+
@child.expects(:person_id=).with(@parent.id)
|
246
|
+
@association.push(@child)
|
247
|
+
@association.size.should == 1
|
248
|
+
end
|
249
|
+
|
250
|
+
end
|
251
|
+
|
252
|
+
context "with multiple objects" do
|
253
|
+
|
254
|
+
before do
|
255
|
+
@parent = stub(:id => "1", :new_record? => true, :class => Person)
|
256
|
+
Post.expects(:all).returns([])
|
257
|
+
@association = Mongoid::Associations::HasManyRelated.new(@parent, options)
|
258
|
+
end
|
259
|
+
|
260
|
+
it "appends the child documents" do
|
261
|
+
@child.expects(:person_id=).with(@parent.id)
|
262
|
+
@second.expects(:person_id=).with(@parent.id)
|
263
|
+
@association.push(@child, @second)
|
264
|
+
@association.size.should == 2
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
|
+
describe ".update" do
|
272
|
+
|
273
|
+
before do
|
274
|
+
@first = Post.new
|
275
|
+
@second = Post.new
|
276
|
+
@related = [@first, @second]
|
277
|
+
@parent = Person.new
|
278
|
+
end
|
279
|
+
|
280
|
+
it "sets the related object id on the parent" do
|
281
|
+
Mongoid::Associations::HasManyRelated.update(@related, @parent, options)
|
282
|
+
@first.person_id.should == @parent.id
|
283
|
+
@second.person_id.should == @parent.id
|
284
|
+
end
|
285
|
+
|
286
|
+
it "returns the related objects" do
|
287
|
+
Mongoid::Associations::HasManyRelated.update(@related, @parent, options).should == @related
|
288
|
+
end
|
289
|
+
|
290
|
+
end
|
291
|
+
|
292
|
+
end
|