mongoid 0.4.5 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.watchr +1 -1
- data/README.textile +5 -0
- data/VERSION +1 -1
- data/lib/mongoid.rb +3 -1
- data/lib/mongoid/associations.rb +84 -1
- data/lib/mongoid/associations/has_many_association.rb +8 -4
- data/lib/mongoid/commands.rb +4 -4
- data/lib/mongoid/criteria.rb +57 -5
- data/lib/mongoid/document.rb +83 -125
- data/lib/mongoid/extensions.rb +16 -1
- data/lib/mongoid/extensions/array/conversions.rb +1 -1
- data/lib/mongoid/extensions/array/parentization.rb +3 -3
- data/lib/mongoid/extensions/hash/accessors.rb +21 -0
- data/lib/mongoid/extensions/object/parentization.rb +4 -2
- data/lib/mongoid/extensions/string/inflections.rb +14 -0
- data/lib/mongoid/extensions/symbol/inflections.rb +14 -0
- data/lib/mongoid/timestamps.rb +30 -0
- data/mongoid.gemspec +18 -4
- data/spec/integration/mongoid/document_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -4
- data/spec/unit/mongoid/associations/has_many_association_spec.rb +1 -1
- data/spec/unit/mongoid/associations_spec.rb +125 -0
- data/spec/unit/mongoid/criteria_spec.rb +154 -0
- data/spec/unit/mongoid/document_spec.rb +110 -150
- data/spec/unit/mongoid/extensions/array/parentization_spec.rb +4 -2
- data/spec/unit/mongoid/extensions/hash/accessors_spec.rb +87 -0
- data/spec/unit/mongoid/extensions/object/parentization_spec.rb +2 -2
- data/spec/unit/mongoid/extensions/string/inflections_spec.rb +45 -0
- data/spec/unit/mongoid/extensions/symbol/inflections_spec.rb +45 -0
- data/spec/unit/mongoid/timestamps_spec.rb +19 -0
- metadata +16 -2
@@ -14,21 +14,6 @@ describe Mongoid::Document do
|
|
14
14
|
@collection = nil
|
15
15
|
end
|
16
16
|
|
17
|
-
describe "#aggregate" do
|
18
|
-
|
19
|
-
before do
|
20
|
-
@reduce = "function(obj, prev) { prev.count++; }"
|
21
|
-
end
|
22
|
-
|
23
|
-
it "returns documents grouped by the supplied fields" do
|
24
|
-
results = [{ "title" => "Sir", "count" => 30 }]
|
25
|
-
@collection.expects(:group).with([:title], nil, {:count => 0}, @reduce).returns(results)
|
26
|
-
grouped = Person.aggregate([:title], {})
|
27
|
-
grouped.first["count"].should == 30
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
17
|
describe "#all" do
|
33
18
|
|
34
19
|
before do
|
@@ -58,34 +43,6 @@ describe Mongoid::Document do
|
|
58
43
|
|
59
44
|
end
|
60
45
|
|
61
|
-
describe "#association=" do
|
62
|
-
|
63
|
-
before do
|
64
|
-
@name = Name.new
|
65
|
-
@person = Person.new
|
66
|
-
end
|
67
|
-
|
68
|
-
it "parentizes the association" do
|
69
|
-
@person.name = @name
|
70
|
-
@name.parent.should == @person
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
describe "#belongs_to" do
|
76
|
-
|
77
|
-
it "creates a reader for the association" do
|
78
|
-
address = Address.new
|
79
|
-
address.should respond_to(:person)
|
80
|
-
end
|
81
|
-
|
82
|
-
it "creates a writer for the association" do
|
83
|
-
address = Address.new
|
84
|
-
address.should respond_to(:person=)
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
46
|
describe "#collection" do
|
90
47
|
|
91
48
|
before do
|
@@ -221,124 +178,58 @@ describe Mongoid::Document do
|
|
221
178
|
|
222
179
|
end
|
223
180
|
|
224
|
-
describe "#
|
225
|
-
|
226
|
-
before do
|
227
|
-
@reduce = "function(obj, prev) { prev.group.push(obj); }"
|
228
|
-
end
|
229
|
-
|
230
|
-
it "returns documents grouped by the supplied fields" do
|
231
|
-
results = [{ "title" => "Sir", "group" => [{ "title" => "Sir", "age" => 30 }] }]
|
232
|
-
@collection.expects(:group).with([:title], nil, { :group => [] }, @reduce).returns(results)
|
233
|
-
grouped = Person.group_by([:title], {})
|
234
|
-
people = grouped.first["group"]
|
235
|
-
people.first.should be_a_kind_of(Person)
|
236
|
-
end
|
237
|
-
|
238
|
-
end
|
239
|
-
|
240
|
-
describe "#has_many" do
|
181
|
+
describe "#index" do
|
241
182
|
|
242
|
-
|
243
|
-
person = Person.new
|
244
|
-
person.addresses.should_not be_nil
|
245
|
-
end
|
183
|
+
context "when unique options are not provided" do
|
246
184
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
185
|
+
it "delegates to collection with unique => false" do
|
186
|
+
@collection.expects(:create_index).with(:title, :unique => false)
|
187
|
+
Person.index :title
|
188
|
+
end
|
251
189
|
|
252
|
-
it "creates a writer for the association" do
|
253
|
-
person = Person.new
|
254
|
-
person.should respond_to(:addresses=)
|
255
190
|
end
|
256
191
|
|
257
|
-
context "when
|
258
|
-
|
259
|
-
before do
|
260
|
-
@attributes = { :title => "Sir",
|
261
|
-
:addresses => [
|
262
|
-
{ :street => "Street 1" },
|
263
|
-
{ :street => "Street 2" } ] }
|
264
|
-
@person = Person.new(@attributes)
|
265
|
-
end
|
192
|
+
context "when unique option is provided" do
|
266
193
|
|
267
|
-
it "
|
268
|
-
|
269
|
-
|
270
|
-
@person.addresses.first.street.should == "New Street"
|
194
|
+
it "delegates to collection with unique option" do
|
195
|
+
@collection.expects(:create_index).with(:title, :unique => true)
|
196
|
+
Person.index :title, :unique => true
|
271
197
|
end
|
272
198
|
|
273
199
|
end
|
274
200
|
|
275
201
|
end
|
276
202
|
|
277
|
-
describe "#
|
203
|
+
describe "#key" do
|
278
204
|
|
279
|
-
|
280
|
-
person = Person.new
|
281
|
-
person.name.should_not be_nil
|
282
|
-
end
|
283
|
-
|
284
|
-
it "creates a reader for the association" do
|
285
|
-
person = Person.new
|
286
|
-
person.should respond_to(:name)
|
287
|
-
end
|
288
|
-
|
289
|
-
it "creates a writer for the association" do
|
290
|
-
person = Person.new
|
291
|
-
person.should respond_to(:name=)
|
292
|
-
end
|
293
|
-
|
294
|
-
context "when setting the association directly" do
|
205
|
+
context "when key is single field" do
|
295
206
|
|
296
207
|
before do
|
297
|
-
|
298
|
-
|
299
|
-
@
|
208
|
+
Address.key :street
|
209
|
+
@address = Address.new(:street => "Testing Street Name")
|
210
|
+
@address.expects(:collection).returns(@collection)
|
211
|
+
@collection.expects(:save)
|
300
212
|
end
|
301
213
|
|
302
|
-
it "
|
303
|
-
|
304
|
-
@
|
305
|
-
@person.name.first_name.should == "New Name"
|
214
|
+
it "adds the callback for primary key generation" do
|
215
|
+
@address.save
|
216
|
+
@address.id.should == "testing-street-name"
|
306
217
|
end
|
307
218
|
|
308
219
|
end
|
309
220
|
|
310
|
-
|
311
|
-
|
312
|
-
describe "#has_timestamps" do
|
221
|
+
context "when key is composite" do
|
313
222
|
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
fields = Tester.instance_variable_get(:@fields)
|
320
|
-
fields[:created_at].should_not be_nil
|
321
|
-
fields[:last_modified].should_not be_nil
|
322
|
-
end
|
323
|
-
|
324
|
-
end
|
325
|
-
|
326
|
-
describe "#index" do
|
327
|
-
|
328
|
-
context "when unique options are not provided" do
|
329
|
-
|
330
|
-
it "delegates to collection with unique => false" do
|
331
|
-
@collection.expects(:create_index).with(:title, :unique => false)
|
332
|
-
Person.index :title
|
223
|
+
before do
|
224
|
+
Address.key :street, :zip
|
225
|
+
@address = Address.new(:street => "Testing Street Name", :zip => "94123")
|
226
|
+
@address.expects(:collection).returns(@collection)
|
227
|
+
@collection.expects(:save)
|
333
228
|
end
|
334
229
|
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
it "delegates to collection with unique option" do
|
340
|
-
@collection.expects(:create_index).with(:title, :unique => true)
|
341
|
-
Person.index :title, :unique => true
|
230
|
+
it "combines all fields" do
|
231
|
+
@address.save
|
232
|
+
@address.id.should == "testing-street-name-94123"
|
342
233
|
end
|
343
234
|
|
344
235
|
end
|
@@ -369,6 +260,35 @@ describe Mongoid::Document do
|
|
369
260
|
|
370
261
|
end
|
371
262
|
|
263
|
+
context "with a primary key" do
|
264
|
+
|
265
|
+
context "when the value for the key exists" do
|
266
|
+
|
267
|
+
before do
|
268
|
+
Address.key :street
|
269
|
+
@address = Address.new(:street => "Test")
|
270
|
+
end
|
271
|
+
|
272
|
+
it "sets the primary key" do
|
273
|
+
@address.id.should == "test"
|
274
|
+
end
|
275
|
+
|
276
|
+
end
|
277
|
+
|
278
|
+
context "when the value for the key is nonexistant" do
|
279
|
+
|
280
|
+
before do
|
281
|
+
@address = Address.new(:street => "Test")
|
282
|
+
end
|
283
|
+
|
284
|
+
it "sets the primary key" do
|
285
|
+
@address.id.should be_nil
|
286
|
+
end
|
287
|
+
|
288
|
+
end
|
289
|
+
|
290
|
+
end
|
291
|
+
|
372
292
|
end
|
373
293
|
|
374
294
|
describe "#new_record?" do
|
@@ -402,32 +322,33 @@ describe Mongoid::Document do
|
|
402
322
|
describe "#paginate" do
|
403
323
|
|
404
324
|
before do
|
405
|
-
@
|
325
|
+
@criteria = stub(:page => 1, :offset => "20")
|
406
326
|
end
|
407
327
|
|
408
328
|
context "when pagination parameters are passed" do
|
409
329
|
|
330
|
+
before do
|
331
|
+
@params = { :conditions => { :test => "Test" }, :page => 2, :per_page => 20 }
|
332
|
+
end
|
333
|
+
|
410
334
|
it "delegates to will paginate with the results" do
|
411
|
-
|
412
|
-
|
335
|
+
Mongoid::Criteria.expects(:translate).with(:all, @params).returns(@criteria)
|
336
|
+
@criteria.expects(:execute).with(Person).returns([])
|
337
|
+
Person.paginate(@params)
|
413
338
|
end
|
414
339
|
|
415
340
|
end
|
416
341
|
|
417
342
|
context "when pagination parameters are not passed" do
|
418
343
|
|
419
|
-
|
420
|
-
@
|
421
|
-
Person.paginate(:conditions => { :test => "Test" })
|
344
|
+
before do
|
345
|
+
@params = { :conditions => { :test => "Test" }}
|
422
346
|
end
|
423
347
|
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
it "adds the sorting parameters in the collection#find" do
|
429
|
-
@collection.expects(:find).with({ :test => "Test" }, { :sort => { :test => -1}, :limit => 20, :skip => 0}).returns(@cursor)
|
430
|
-
Person.paginate(:conditions => { :test => "Test" }, :sort => { :test => -1 })
|
348
|
+
it "delegates to will paginate with default values" do
|
349
|
+
Mongoid::Criteria.expects(:translate).with(:all, @params).returns(@criteria)
|
350
|
+
@criteria.expects(:execute).with(Person).returns([])
|
351
|
+
Person.paginate(:conditions => { :test => "Test" })
|
431
352
|
end
|
432
353
|
|
433
354
|
end
|
@@ -515,6 +436,45 @@ describe Mongoid::Document do
|
|
515
436
|
|
516
437
|
end
|
517
438
|
|
439
|
+
describe "#write_attributes" do
|
440
|
+
|
441
|
+
context "on a child document" do
|
442
|
+
|
443
|
+
context "when child is part of a has one" do
|
444
|
+
|
445
|
+
before do
|
446
|
+
@person = Person.new(:title => "Sir", :age => 30)
|
447
|
+
@name = Name.new(:first_name => "Test", :last_name => "User")
|
448
|
+
@person.name = @name
|
449
|
+
end
|
450
|
+
|
451
|
+
it "sets the child attributes on the parent" do
|
452
|
+
@name.write_attributes(:first_name => "Test2", :last_name => "User2")
|
453
|
+
@person.attributes[:name].should ==
|
454
|
+
{ :first_name => "Test2", :last_name => "User2" }
|
455
|
+
end
|
456
|
+
|
457
|
+
end
|
458
|
+
|
459
|
+
context "when child is part of a has many" do
|
460
|
+
|
461
|
+
before do
|
462
|
+
@person = Person.new(:title => "Sir")
|
463
|
+
@address = Address.new(:street => "Test")
|
464
|
+
@person.addresses << @address
|
465
|
+
end
|
466
|
+
|
467
|
+
it "updates the child attributes on the parent" do
|
468
|
+
@address.write_attributes(:street => "Test2")
|
469
|
+
@person.attributes[:addresses].should == [{ :street => "Test2" }]
|
470
|
+
end
|
471
|
+
|
472
|
+
end
|
473
|
+
|
474
|
+
end
|
475
|
+
|
476
|
+
end
|
477
|
+
|
518
478
|
context "validations" do
|
519
479
|
|
520
480
|
context "when defining using macros" do
|
@@ -11,10 +11,12 @@ describe Mongoid::Extensions::Array::Parentization do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "sets the parent on each element" do
|
14
|
+
@child.expects(:add_observer).with(@parent)
|
14
15
|
@child.expects(:parent=).with(@parent)
|
15
|
-
@
|
16
|
+
@child.expects(:association_name=).with(:child)
|
17
|
+
@array.parentize(@parent, :child)
|
16
18
|
end
|
17
19
|
|
18
20
|
end
|
19
21
|
|
20
|
-
end
|
22
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), "/../../../../spec_helper.rb")
|
2
|
+
|
3
|
+
describe Mongoid::Extensions::Hash::Accessors do
|
4
|
+
|
5
|
+
describe "#insert" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@hash = {
|
9
|
+
:_id => 1,
|
10
|
+
:title => "value",
|
11
|
+
:name => {
|
12
|
+
:_id => 2, :first_name => "Test", :last_name => "User"
|
13
|
+
},
|
14
|
+
:addresses => [
|
15
|
+
{ :_id => 3, :street => "First Street" },
|
16
|
+
{ :_id => 4, :street => "Second Street" }
|
17
|
+
]
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when writing a single attribute" do
|
22
|
+
|
23
|
+
context "when attribute exists" do
|
24
|
+
|
25
|
+
before do
|
26
|
+
@new = { :_id => 2, :first_name => "Test2", :last_name => "User2" }
|
27
|
+
end
|
28
|
+
|
29
|
+
it "updates the existing attribute" do
|
30
|
+
@hash.insert(:name, @new)
|
31
|
+
@hash[:name].should == @new
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
context "when attribute does not exist" do
|
37
|
+
|
38
|
+
before do
|
39
|
+
@hash.delete(:name)
|
40
|
+
@new = { :_id => 2, :first_name => "Test2", :last_name => "User2" }
|
41
|
+
end
|
42
|
+
|
43
|
+
it "updates the existing attribute" do
|
44
|
+
@hash.insert(:name, @new)
|
45
|
+
@hash[:name].should == @new
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when writing to an array of attributes" do
|
53
|
+
|
54
|
+
context "when matching attribute exists" do
|
55
|
+
|
56
|
+
before do
|
57
|
+
@new = { :_id => 3, :street => "New Street" }
|
58
|
+
end
|
59
|
+
|
60
|
+
it "updates the matching attributes" do
|
61
|
+
@hash.insert(:addresses, @new)
|
62
|
+
@hash[:addresses].should include({:street => "New Street", :_id => 3})
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
context "when matching attribute does not exist" do
|
68
|
+
|
69
|
+
before do
|
70
|
+
@new = { :street => "New Street" }
|
71
|
+
end
|
72
|
+
|
73
|
+
it "updates the matching attributes" do
|
74
|
+
@hash.insert(:addresses, @new)
|
75
|
+
@hash[:addresses].should == [
|
76
|
+
{ :_id => 3, :street => "First Street" },
|
77
|
+
{ :_id => 4, :street => "Second Street" },
|
78
|
+
{ :street => "New Street" }
|
79
|
+
]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|