mongoid 0.10.6 → 0.11.0

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.
Files changed (42) hide show
  1. data/Rakefile +3 -3
  2. data/VERSION +1 -1
  3. data/lib/mongoid.rb +2 -2
  4. data/lib/mongoid/associations.rb +1 -1
  5. data/lib/mongoid/associations/belongs_to.rb +1 -1
  6. data/lib/mongoid/associations/has_many.rb +8 -6
  7. data/lib/mongoid/associations/has_one.rb +6 -5
  8. data/lib/mongoid/commands.rb +94 -41
  9. data/lib/mongoid/commands/delete_all.rb +2 -3
  10. data/lib/mongoid/commands/deletion.rb +1 -1
  11. data/lib/mongoid/commands/destroy_all.rb +2 -0
  12. data/lib/mongoid/commands/save.rb +1 -1
  13. data/lib/mongoid/criteria.rb +1 -1
  14. data/lib/mongoid/document.rb +43 -32
  15. data/lib/mongoid/extensions.rb +3 -3
  16. data/lib/mongoid/extensions/hash/assimilation.rb +2 -3
  17. data/lib/mongoid/extensions/string/inflections.rb +1 -0
  18. data/mongoid.gemspec +13 -9
  19. data/perf/benchmark.rb +8 -4
  20. data/spec/integration/mongoid/commands_spec.rb +103 -0
  21. data/spec/integration/mongoid/document_spec.rb +13 -1
  22. data/spec/integration/mongoid/inheritance_spec.rb +105 -0
  23. data/spec/spec_helper.rb +24 -2
  24. data/spec/unit/mongoid/associations/belongs_to_spec.rb +3 -3
  25. data/spec/unit/mongoid/associations/has_many_spec.rb +92 -31
  26. data/spec/unit/mongoid/associations/has_one_spec.rb +57 -4
  27. data/spec/unit/mongoid/associations_spec.rb +4 -4
  28. data/spec/unit/mongoid/attributes_spec.rb +2 -2
  29. data/spec/unit/mongoid/commands/delete_all_spec.rb +4 -3
  30. data/spec/unit/mongoid/commands/delete_spec.rb +1 -1
  31. data/spec/unit/mongoid/commands/destroy_all_spec.rb +3 -2
  32. data/spec/unit/mongoid/commands/destroy_spec.rb +1 -1
  33. data/spec/unit/mongoid/commands/save_spec.rb +3 -3
  34. data/spec/unit/mongoid/commands_spec.rb +6 -6
  35. data/spec/unit/mongoid/criteria_spec.rb +45 -36
  36. data/spec/unit/mongoid/document_spec.rb +198 -29
  37. data/spec/unit/mongoid/extensions/array/conversions_spec.rb +2 -2
  38. data/spec/unit/mongoid/extensions/hash/assimilation_spec.rb +33 -8
  39. data/spec/unit/mongoid/extensions/object/conversions_spec.rb +2 -2
  40. data/spec/unit/mongoid/finders_spec.rb +1 -1
  41. data/spec/unit/mongoid/timestamps_spec.rb +1 -1
  42. metadata +9 -5
@@ -19,7 +19,7 @@ describe Mongoid::Criteria do
19
19
 
20
20
  before do
21
21
  Person.expects(:collection).returns(@collection)
22
- @collection.expects(:find).with({ :title => "Sir" }, {}).returns(@cursor)
22
+ @collection.expects(:find).with({ :title => "Sir", :_type => "Person" }, {}).returns(@cursor)
23
23
  end
24
24
 
25
25
  it "executes the criteria and returns the element at the index" do
@@ -42,7 +42,7 @@ describe Mongoid::Criteria do
42
42
  end
43
43
 
44
44
  it "calls group on the collection with the aggregate js" do
45
- @collection.expects(:group).with([:field1], {}, {:count => 0}, @reduce)
45
+ @collection.expects(:group).with([:field1], {:_type => "Person"}, {:count => 0}, @reduce)
46
46
  @criteria.only(:field1).aggregate
47
47
  end
48
48
 
@@ -57,7 +57,7 @@ describe Mongoid::Criteria do
57
57
  end
58
58
 
59
59
  it "calls group on the collection with the aggregate js" do
60
- @collection.expects(:group).with([:field1], {}, {:count => 0}, @reduce)
60
+ @collection.expects(:group).with([:field1], {:_type => "Person"}, {:count => 0}, @reduce)
61
61
  @criteria.only(:field1).aggregate(Person)
62
62
  end
63
63
 
@@ -69,7 +69,7 @@ describe Mongoid::Criteria do
69
69
 
70
70
  it "adds the $all query to the selector" do
71
71
  @criteria.all(:title => ["title1", "title2"])
72
- @criteria.selector.should == { :title => { "$all" => ["title1", "title2"] } }
72
+ @criteria.selector.should == { :_type => "Person", :title => { "$all" => ["title1", "title2"] } }
73
73
  end
74
74
 
75
75
  it "returns self" do
@@ -84,7 +84,7 @@ describe Mongoid::Criteria do
84
84
 
85
85
  it "adds the clause to the selector" do
86
86
  @criteria.and(:title => "Title", :text => "Text")
87
- @criteria.selector.should == { :title => "Title", :text => "Text" }
87
+ @criteria.selector.should == { :_type => "Person", :title => "Title", :text => "Text" }
88
88
  end
89
89
 
90
90
  end
@@ -93,7 +93,7 @@ describe Mongoid::Criteria do
93
93
 
94
94
  it "adds the $where clause to the selector" do
95
95
  @criteria.and("this.date < new Date()")
96
- @criteria.selector.should == { "$where" => "this.date < new Date()" }
96
+ @criteria.selector.should == { :_type => "Person", "$where" => "this.date < new Date()" }
97
97
  end
98
98
 
99
99
  end
@@ -172,7 +172,7 @@ describe Mongoid::Criteria do
172
172
 
173
173
  before do
174
174
  @criteria = Mongoid::Criteria.new(Person)
175
- @selector = { :test => "Testing" }
175
+ @selector = { :_type => "Person", :test => "Testing" }
176
176
  @criteria.where(@selector)
177
177
  @collection = mock
178
178
  @cursor = mock
@@ -202,7 +202,7 @@ describe Mongoid::Criteria do
202
202
 
203
203
  before do
204
204
  Person.expects(:collection).returns(@collection)
205
- @collection.expects(:find).with({ :title => "Sir" }, {}).returns(@cursor)
205
+ @collection.expects(:find).with({ :_type => "Person", :title => "Sir" }, {}).returns(@cursor)
206
206
  end
207
207
 
208
208
  it "executes the criteria" do
@@ -217,7 +217,7 @@ describe Mongoid::Criteria do
217
217
 
218
218
  before do
219
219
  Person.expects(:collection).returns(@collection)
220
- @collection.expects(:find).with({ :title => "Sir" }, {}).returns(@cursor)
220
+ @collection.expects(:find).with({ :_type => "Person", :title => "Sir" }, {}).returns(@cursor)
221
221
  end
222
222
 
223
223
  it "calls each on the existing results" do
@@ -277,7 +277,7 @@ describe Mongoid::Criteria do
277
277
 
278
278
  it "adds the $ne query to the selector" do
279
279
  @criteria.excludes(:title => "Bad Title", :text => "Bad Text")
280
- @criteria.selector.should == { :title => { "$ne" => "Bad Title"}, :text => { "$ne" => "Bad Text" } }
280
+ @criteria.selector.should == { :_type => "Person", :title => { "$ne" => "Bad Title"}, :text => { "$ne" => "Bad Text" } }
281
281
  end
282
282
 
283
283
  it "returns self" do
@@ -348,7 +348,7 @@ describe Mongoid::Criteria do
348
348
  end
349
349
 
350
350
  it "calls group on the collection with the aggregate js" do
351
- @collection.expects(:group).with([:field1], {}, {:group => []}, @reduce).returns(@grouping)
351
+ @collection.expects(:group).with([:field1], {:_type => "Person"}, {:group => []}, @reduce).returns(@grouping)
352
352
  @criteria.only(:field1).group
353
353
  end
354
354
 
@@ -363,7 +363,7 @@ describe Mongoid::Criteria do
363
363
  end
364
364
 
365
365
  it "calls group on the collection with the aggregate js" do
366
- @collection.expects(:group).with([:field1], {}, {:group => []}, @reduce).returns(@grouping)
366
+ @collection.expects(:group).with([:field1], {:_type => "Person"}, {:group => []}, @reduce).returns(@grouping)
367
367
  @criteria.only(:field1).group
368
368
  end
369
369
 
@@ -376,7 +376,7 @@ describe Mongoid::Criteria do
376
376
  it "adds the _id query to the selector" do
377
377
  id = Mongo::ObjectID.new.to_s
378
378
  @criteria.id(id)
379
- @criteria.selector.should == { :_id => id }
379
+ @criteria.selector.should == { :_type => "Person", :_id => id }
380
380
  end
381
381
 
382
382
  it "returns self" do
@@ -390,7 +390,7 @@ describe Mongoid::Criteria do
390
390
 
391
391
  it "adds the $in clause to the selector" do
392
392
  @criteria.in(:title => ["title1", "title2"], :text => ["test"])
393
- @criteria.selector.should == { :title => { "$in" => ["title1", "title2"] }, :text => { "$in" => ["test"] } }
393
+ @criteria.selector.should == { :_type => "Person", :title => { "$in" => ["title1", "title2"] }, :text => { "$in" => ["test"] } }
394
394
  end
395
395
 
396
396
  it "returns self" do
@@ -399,6 +399,15 @@ describe Mongoid::Criteria do
399
399
 
400
400
  end
401
401
 
402
+ describe "#initialize" do
403
+
404
+ it "sets the _type value on the selector" do
405
+ criteria = Mongoid::Criteria.new(Person)
406
+ criteria.selector.should == { :_type => "Person" }
407
+ end
408
+
409
+ end
410
+
402
411
  describe "#last" do
403
412
 
404
413
  context "when documents exist" do
@@ -488,7 +497,7 @@ describe Mongoid::Criteria do
488
497
  before do
489
498
  @other = Mongoid::Criteria.new(Person)
490
499
  @other.where(:name => "Chloe").order_by([[:name, :asc]])
491
- @selector = { :title => "Sir", :age => 30, :name => "Chloe" }
500
+ @selector = { :_type => "Person", :title => "Sir", :age => 30, :name => "Chloe" }
492
501
  @options = { :skip => 40, :limit => 20, :sort => [[:name, :asc]] }
493
502
  end
494
503
 
@@ -504,7 +513,7 @@ describe Mongoid::Criteria do
504
513
 
505
514
  before do
506
515
  @other = Mongoid::Criteria.new(Person)
507
- @selector = { :title => "Sir", :age => 30 }
516
+ @selector = { :_type => "Person", :title => "Sir", :age => 30 }
508
517
  @options = { :skip => 40, :limit => 20 }
509
518
  end
510
519
 
@@ -528,7 +537,7 @@ describe Mongoid::Criteria do
528
537
 
529
538
  it "merges the criteria with the next one" do
530
539
  @new_criteria = @criteria.accepted
531
- @new_criteria.selector.should == { :title => "Sir", :terms => true }
540
+ @new_criteria.selector.should == { :_type => "Person", :title => "Sir", :terms => true }
532
541
  end
533
542
 
534
543
  context "chaining more than one scope" do
@@ -539,7 +548,7 @@ describe Mongoid::Criteria do
539
548
 
540
549
  it "returns the final merged criteria" do
541
550
  @criteria.selector.should ==
542
- { :title => "Sir", :terms => true, :age => { "$gt" => 50 } }
551
+ { :_type => "Person", :title => "Sir", :terms => true, :age => { "$gt" => 50 } }
543
552
  end
544
553
 
545
554
  end
@@ -578,7 +587,7 @@ describe Mongoid::Criteria do
578
587
 
579
588
  it "adds the exclusion to the selector" do
580
589
  @criteria.not_in(:title => ["title1", "title2"], :text => ["test"])
581
- @criteria.selector.should == { :title => { "$nin" => ["title1", "title2"] }, :text => { "$nin" => ["test"] } }
590
+ @criteria.selector.should == { :_type => "Person", :title => { "$nin" => ["title1", "title2"] }, :text => { "$nin" => ["test"] } }
582
591
  end
583
592
 
584
593
  it "returns self" do
@@ -730,7 +739,7 @@ describe Mongoid::Criteria do
730
739
  @collection = mock
731
740
  Person.expects(:collection).returns(@collection)
732
741
  @criteria = Person.where(:_id => "1").skip(60).limit(20)
733
- @collection.expects(:find).with({:_id => "1"}, :skip => 60, :limit => 20).returns([])
742
+ @collection.expects(:find).with({:_type => "Person", :_id => "1"}, :skip => 60, :limit => 20).returns([])
734
743
  @results = @criteria.paginate
735
744
  end
736
745
 
@@ -858,7 +867,7 @@ describe Mongoid::Criteria do
858
867
  end
859
868
 
860
869
  it "returns a criteria with a selector from the conditions" do
861
- @criteria.selector.should == { :title => "Test" }
870
+ @criteria.selector.should == { :_type => "Person", :title => "Test" }
862
871
  end
863
872
 
864
873
  it "returns a criteria with klass Person" do
@@ -874,7 +883,7 @@ describe Mongoid::Criteria do
874
883
  end
875
884
 
876
885
  it "returns a criteria with a selector from the conditions" do
877
- @criteria.selector.should == { :title => "Test" }
886
+ @criteria.selector.should == { :_type => "Person", :title => "Test" }
878
887
  end
879
888
 
880
889
  it "returns a criteria with klass Person" do
@@ -890,7 +899,7 @@ describe Mongoid::Criteria do
890
899
  end
891
900
 
892
901
  it "returns a criteria with a selector from the conditions" do
893
- @criteria.selector.should == { :title => "Test" }
902
+ @criteria.selector.should == { :_type => "Person", :title => "Test" }
894
903
  end
895
904
 
896
905
  it "returns a criteria with klass Person" do
@@ -905,7 +914,7 @@ describe Mongoid::Criteria do
905
914
  end
906
915
 
907
916
  it "adds the criteria and the options" do
908
- @criteria.selector.should == { :title => "Test" }
917
+ @criteria.selector.should == { :_type => "Person", :title => "Test" }
909
918
  @criteria.options.should == { :skip => 10 }
910
919
  end
911
920
 
@@ -923,7 +932,7 @@ describe Mongoid::Criteria do
923
932
 
924
933
  it "adds the clause to the selector" do
925
934
  @criteria.where(:title => "Title", :text => "Text")
926
- @criteria.selector.should == { :title => "Title", :text => "Text" }
935
+ @criteria.selector.should == { :_type => "Person", :title => "Title", :text => "Text" }
927
936
  end
928
937
 
929
938
  end
@@ -934,7 +943,7 @@ describe Mongoid::Criteria do
934
943
 
935
944
  it "returns those matching an all clause" do
936
945
  @criteria.where(:title.all => ["Sir"])
937
- @criteria.selector.should == { :title => { "$all" => ["Sir"] } }
946
+ @criteria.selector.should == { :_type => "Person", :title => { "$all" => ["Sir"] } }
938
947
  end
939
948
 
940
949
  end
@@ -943,7 +952,7 @@ describe Mongoid::Criteria do
943
952
 
944
953
  it "returns those matching an exists clause" do
945
954
  @criteria.where(:title.exists => true)
946
- @criteria.selector.should == { :title => { "$exists" => true } }
955
+ @criteria.selector.should == { :_type => "Person", :title => { "$exists" => true } }
947
956
  end
948
957
 
949
958
  end
@@ -952,7 +961,7 @@ describe Mongoid::Criteria do
952
961
 
953
962
  it "returns those matching a gt clause" do
954
963
  @criteria.where(:age.gt => 30)
955
- @criteria.selector.should == { :age => { "$gt" => 30 } }
964
+ @criteria.selector.should == { :_type => "Person", :age => { "$gt" => 30 } }
956
965
  end
957
966
 
958
967
  end
@@ -961,7 +970,7 @@ describe Mongoid::Criteria do
961
970
 
962
971
  it "returns those matching a gte clause" do
963
972
  @criteria.where(:age.gte => 33)
964
- @criteria.selector.should == { :age => { "$gte" => 33 } }
973
+ @criteria.selector.should == { :_type => "Person", :age => { "$gte" => 33 } }
965
974
  end
966
975
 
967
976
  end
@@ -970,7 +979,7 @@ describe Mongoid::Criteria do
970
979
 
971
980
  it "returns those matching an in clause" do
972
981
  @criteria.where(:title.in => ["Sir", "Madam"])
973
- @criteria.selector.should == { :title => { "$in" => ["Sir", "Madam"] } }
982
+ @criteria.selector.should == { :_type => "Person", :title => { "$in" => ["Sir", "Madam"] } }
974
983
  end
975
984
 
976
985
  end
@@ -979,7 +988,7 @@ describe Mongoid::Criteria do
979
988
 
980
989
  it "returns those matching a lt clause" do
981
990
  @criteria.where(:age.lt => 34)
982
- @criteria.selector.should == { :age => { "$lt" => 34 } }
991
+ @criteria.selector.should == { :_type => "Person", :age => { "$lt" => 34 } }
983
992
  end
984
993
 
985
994
  end
@@ -988,7 +997,7 @@ describe Mongoid::Criteria do
988
997
 
989
998
  it "returns those matching a lte clause" do
990
999
  @criteria.where(:age.lte => 33)
991
- @criteria.selector.should == { :age => { "$lte" => 33 } }
1000
+ @criteria.selector.should == { :_type => "Person", :age => { "$lte" => 33 } }
992
1001
  end
993
1002
 
994
1003
  end
@@ -997,7 +1006,7 @@ describe Mongoid::Criteria do
997
1006
 
998
1007
  it "returns those matching a ne clause" do
999
1008
  @criteria.where(:age.ne => 50)
1000
- @criteria.selector.should == { :age => { "$ne" => 50 } }
1009
+ @criteria.selector.should == { :_type => "Person", :age => { "$ne" => 50 } }
1001
1010
  end
1002
1011
 
1003
1012
  end
@@ -1006,7 +1015,7 @@ describe Mongoid::Criteria do
1006
1015
 
1007
1016
  it "returns those matching a nin clause" do
1008
1017
  @criteria.where(:title.nin => ["Esquire", "Congressman"])
1009
- @criteria.selector.should == { :title => { "$nin" => ["Esquire", "Congressman"] } }
1018
+ @criteria.selector.should == { :_type => "Person", :title => { "$nin" => ["Esquire", "Congressman"] } }
1010
1019
  end
1011
1020
 
1012
1021
  end
@@ -1015,7 +1024,7 @@ describe Mongoid::Criteria do
1015
1024
 
1016
1025
  it "returns those matching a size clause" do
1017
1026
  @criteria.where(:aliases.size => 2)
1018
- @criteria.selector.should == { :aliases => { "$size" => 2 } }
1027
+ @criteria.selector.should == { :_type => "Person", :aliases => { "$size" => 2 } }
1019
1028
  end
1020
1029
 
1021
1030
  end
@@ -1028,7 +1037,7 @@ describe Mongoid::Criteria do
1028
1037
 
1029
1038
  it "adds the $where clause to the selector" do
1030
1039
  @criteria.where("this.date < new Date()")
1031
- @criteria.selector.should == { "$where" => "this.date < new Date()" }
1040
+ @criteria.selector.should == { :_type => "Person", "$where" => "this.date < new Date()" }
1032
1041
  end
1033
1042
 
1034
1043
  end
@@ -4,14 +4,16 @@ describe Mongoid::Document do
4
4
 
5
5
  before do
6
6
  @collection = stub(:name => "people")
7
- @database = stub(:collection => @collection)
7
+ @canvas_collection = stub(:name => "canvases")
8
+ @database = mock
8
9
  Mongoid.stubs(:database).returns(@database)
10
+ @database.stubs(:collection).with("people").returns(@collection)
11
+ @database.stubs(:collection).with("canvases").returns(@canvas_collection)
9
12
  end
10
13
 
11
14
  after do
12
- Person.instance_variable_set(:@collection, nil)
13
- @database = nil
14
- @collection = nil
15
+ Person._collection = nil
16
+ Canvas._collection = nil
15
17
  end
16
18
 
17
19
  describe "#==" do
@@ -54,6 +56,14 @@ describe Mongoid::Document do
54
56
 
55
57
  end
56
58
 
59
+ context "when comapring parent to its subclass" do
60
+
61
+ it "returns false" do
62
+ Canvas.new.should_not == Firefox.new
63
+ end
64
+
65
+ end
66
+
57
67
  end
58
68
 
59
69
  describe "#alias_method_chain" do
@@ -126,15 +136,28 @@ describe Mongoid::Document do
126
136
 
127
137
  end
128
138
 
129
- describe ".collection_name" do
139
+ describe ".collection_name=" do
140
+
141
+ context "on a parent class" do
142
+
143
+ it "sets the collection name on the document class" do
144
+ Patient.collection_name = "pats"
145
+ Patient.collection_name.should == "pats"
146
+ end
130
147
 
131
- before do
132
- @coll = stub(:name => "population")
133
148
  end
134
149
 
135
- it "sets the collection name on the document class" do
136
- Mongoid.database.expects(:collection).with("population").returns(@coll)
137
- Patient.collection.should == @coll
150
+ context "on a subclass" do
151
+
152
+ after do
153
+ Canvas.collection_name = "canvases"
154
+ end
155
+
156
+ it "sets the collection name for the entire hierarchy" do
157
+ Firefox.collection_name = "browsers"
158
+ Canvas.collection_name.should == "browsers"
159
+ end
160
+
138
161
  end
139
162
 
140
163
  end
@@ -149,12 +172,28 @@ describe Mongoid::Document do
149
172
 
150
173
  describe "#defaults" do
151
174
 
152
- before do
153
- @game = Game.new
175
+ context "on parent classes" do
176
+
177
+ before do
178
+ @shape = Shape.new
179
+ end
180
+
181
+ it "does not return subclass defaults" do
182
+ @shape.defaults.should == { "x" => 0, "y" => 0 }
183
+ end
184
+
154
185
  end
155
186
 
156
- it "returns the class defaults" do
157
- @game.defaults.should == { "high_score" => 500, "score" => 0 }
187
+ context "on subclasses" do
188
+
189
+ before do
190
+ @circle = Circle.new
191
+ end
192
+
193
+ it "has the parent and child defaults" do
194
+ @circle.defaults.should == { "x" => 0, "y" => 0, "radius" => 0 }
195
+ end
196
+
158
197
  end
159
198
 
160
199
  end
@@ -199,6 +238,15 @@ describe Mongoid::Document do
199
238
 
200
239
  end
201
240
 
241
+ context "when a subclass is embedded" do
242
+
243
+ it "returns true" do
244
+ circle = Circle.new
245
+ circle.should be_embedded
246
+ end
247
+
248
+ end
249
+
202
250
  end
203
251
 
204
252
  describe ".field" do
@@ -232,7 +280,8 @@ describe Mongoid::Document do
232
280
 
233
281
  it "allows proper access to the object" do
234
282
  @person.mixed_drink.should == @drink
235
- @person.attributes[:mixed_drink].except(:_id).should == { "name" => "Jack and Coke" }
283
+ @person.attributes[:mixed_drink].except(:_id).except(:_type).should ==
284
+ { "name" => "Jack and Coke" }
236
285
  end
237
286
 
238
287
  end
@@ -275,6 +324,38 @@ describe Mongoid::Document do
275
324
 
276
325
  end
277
326
 
327
+ describe "#fields" do
328
+
329
+ context "on parent classes" do
330
+
331
+ before do
332
+ @shape = Shape.new
333
+ end
334
+
335
+ it "does not return subclass fields" do
336
+ @shape.fields.keys.should include("x")
337
+ @shape.fields.keys.should include("y")
338
+ @shape.fields.keys.should_not include("radius")
339
+ end
340
+
341
+ end
342
+
343
+ context "on subclasses" do
344
+
345
+ before do
346
+ @circle = Circle.new
347
+ end
348
+
349
+ it "has the parent and child fields" do
350
+ @circle.fields.keys.should include("x")
351
+ @circle.fields.keys.should include("y")
352
+ @circle.fields.keys.should include("radius")
353
+ end
354
+
355
+ end
356
+
357
+ end
358
+
278
359
  describe ".human_name" do
279
360
 
280
361
  it "returns the class name underscored and humanized" do
@@ -315,12 +396,21 @@ describe Mongoid::Document do
315
396
 
316
397
  end
317
398
 
399
+ context "when indexing a subclass" do
400
+
401
+ it "sets the index on the root class collection" do
402
+ @canvas_collection.expects(:create_index).with(:name, true)
403
+ Firefox.index :name, :unique => true
404
+ end
405
+
406
+ end
407
+
318
408
  end
319
409
 
320
410
  describe ".instantiate" do
321
411
 
322
412
  before do
323
- @attributes = { :_id => "1", :title => "Sir", :age => 30 }
413
+ @attributes = { :_id => "1", :_type => "Person", :title => "Sir", :age => 30 }
324
414
  @person = Person.new(@attributes)
325
415
  end
326
416
 
@@ -364,6 +454,18 @@ describe Mongoid::Document do
364
454
 
365
455
  end
366
456
 
457
+ context "when key is on a subclass" do
458
+
459
+ before do
460
+ Firefox.key :name
461
+ end
462
+
463
+ it "sets the key for the entire hierarchy" do
464
+ Canvas.primary_key.should == [:name]
465
+ end
466
+
467
+ end
468
+
367
469
  end
368
470
 
369
471
  describe "#new" do
@@ -434,6 +536,14 @@ describe Mongoid::Document do
434
536
 
435
537
  end
436
538
 
539
+ context "without a type specified" do
540
+
541
+ it "sets the type" do
542
+ Person.new._type.should == "Person"
543
+ end
544
+
545
+ end
546
+
437
547
  end
438
548
 
439
549
  describe "#new_record?" do
@@ -464,7 +574,7 @@ describe Mongoid::Document do
464
574
 
465
575
  end
466
576
 
467
- describe "#parent" do
577
+ describe "#_parent" do
468
578
 
469
579
  before do
470
580
  @attributes = { :title => "Sir",
@@ -477,7 +587,7 @@ describe Mongoid::Document do
477
587
  context "when document is embedded" do
478
588
 
479
589
  it "returns the parent document" do
480
- @person.addresses.first.parent.should == @person
590
+ @person.addresses.first._parent.should == @person
481
591
  end
482
592
 
483
593
  end
@@ -485,7 +595,7 @@ describe Mongoid::Document do
485
595
  context "when document is root" do
486
596
 
487
597
  it "returns nil" do
488
- @person.parent.should be_nil
598
+ @person._parent.should be_nil
489
599
  end
490
600
 
491
601
  end
@@ -501,7 +611,7 @@ describe Mongoid::Document do
501
611
 
502
612
  it "sets the parent on each element" do
503
613
  @child.parentize(@parent, :child)
504
- @child.parent.should == @parent
614
+ @child._parent.should == @parent
505
615
  end
506
616
 
507
617
  end
@@ -555,7 +665,7 @@ describe Mongoid::Document do
555
665
 
556
666
  end
557
667
 
558
- describe "#root" do
668
+ describe "#_root" do
559
669
 
560
670
  before do
561
671
  @person = Person.new(:title => "Mr")
@@ -568,7 +678,7 @@ describe Mongoid::Document do
568
678
  context "when document is the root" do
569
679
 
570
680
  it "returns self" do
571
- @person.root.should == @person
681
+ @person._root.should == @person
572
682
  end
573
683
 
574
684
  end
@@ -576,7 +686,7 @@ describe Mongoid::Document do
576
686
  context "when document is embedded one level" do
577
687
 
578
688
  it "returns the parent" do
579
- @phone_number.root.should == @person
689
+ @phone_number._root.should == @person
580
690
  end
581
691
 
582
692
  end
@@ -584,7 +694,39 @@ describe Mongoid::Document do
584
694
  context "when document is embedded multiple levels" do
585
695
 
586
696
  it "returns the top level parent" do
587
- @country_code.root.should == @person
697
+ @country_code._root.should == @person
698
+ end
699
+
700
+ end
701
+
702
+ end
703
+
704
+ describe ".store_in" do
705
+
706
+ context "on a parent class" do
707
+
708
+ before do
709
+ Patient.store_in :population
710
+ end
711
+
712
+ it "sets the collection name for the document" do
713
+ Patient.collection_name.should == "population"
714
+ end
715
+
716
+ end
717
+
718
+ context "on a subclass" do
719
+
720
+ before do
721
+ Firefox.store_in :browsers
722
+ end
723
+
724
+ after do
725
+ Firefox.store_in :canvases
726
+ end
727
+
728
+ it "changes the collection name for the entire hierarchy" do
729
+ Canvas.collection_name.should == "browsers"
588
730
  end
589
731
 
590
732
  end
@@ -733,10 +875,14 @@ describe Mongoid::Document do
733
875
 
734
876
  before do
735
877
  @person = Person.new
878
+ @canvas = Canvas.new
879
+ @firefox = Firefox.new
736
880
  end
737
881
 
738
882
  after do
739
883
  Person.validations.clear
884
+ Canvas.validations.clear
885
+ Firefox.validations.clear
740
886
  end
741
887
 
742
888
  describe "#validates_acceptance_of" do
@@ -843,12 +989,35 @@ describe Mongoid::Document do
843
989
 
844
990
  describe "#validates_presence_of" do
845
991
 
846
- it "fails if the field is nil" do
847
- Person.class_eval do
848
- validates_presence_of :title
992
+ context "on a parent class" do
993
+
994
+ it "fails if the field is nil on the parent" do
995
+ Person.class_eval do
996
+ validates_presence_of :title
997
+ end
998
+ @person.valid?.should be_false
999
+ @person.errors.on(:title).should_not be_nil
849
1000
  end
850
- @person.valid?.should be_false
851
- @person.errors.on(:title).should_not be_nil
1001
+
1002
+ it "fails if the field is nil on a subclass" do
1003
+ Canvas.class_eval do
1004
+ validates_presence_of :name
1005
+ end
1006
+ @firefox.valid?.should be_false
1007
+ @firefox.errors.on(:name).should_not be_nil
1008
+ end
1009
+
1010
+ end
1011
+
1012
+ context "on a subclass" do
1013
+
1014
+ it "parent class does not get subclass validations" do
1015
+ Firefox.class_eval do
1016
+ validates_presence_of :name
1017
+ end
1018
+ @canvas.valid?.should be_true
1019
+ end
1020
+
852
1021
  end
853
1022
 
854
1023
  end