mongoid-pre 2.0.0.beta1 → 2.0.0.pre

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 (135) hide show
  1. data/.watchr +15 -10
  2. data/HISTORY +342 -0
  3. data/README.rdoc +8 -1
  4. data/Rakefile +2 -3
  5. data/VERSION +1 -1
  6. data/lib/mongoid/associations/{embedded_in.rb → belongs_to.rb} +4 -4
  7. data/lib/mongoid/associations/belongs_to_related.rb +9 -6
  8. data/lib/mongoid/associations/{embeds_many.rb → has_many.rb} +20 -33
  9. data/lib/mongoid/associations/has_many_related.rb +4 -28
  10. data/lib/mongoid/associations/{embeds_one.rb → has_one.rb} +6 -6
  11. data/lib/mongoid/associations/options.rb +6 -1
  12. data/lib/mongoid/associations.rb +32 -41
  13. data/lib/mongoid/attributes.rb +6 -13
  14. data/lib/mongoid/collection.rb +1 -2
  15. data/lib/mongoid/commands/delete.rb +1 -1
  16. data/lib/mongoid/commands/delete_all.rb +1 -4
  17. data/lib/mongoid/commands/destroy.rb +1 -1
  18. data/lib/mongoid/commands/destroy_all.rb +1 -3
  19. data/lib/mongoid/commands/save.rb +0 -1
  20. data/lib/mongoid/commands.rb +13 -2
  21. data/lib/mongoid/components.rb +1 -6
  22. data/lib/mongoid/config.rb +1 -5
  23. data/lib/mongoid/contexts/enumerable.rb +17 -54
  24. data/lib/mongoid/contexts/mongo.rb +38 -101
  25. data/lib/mongoid/contexts/paging.rb +2 -2
  26. data/lib/mongoid/contexts.rb +0 -21
  27. data/lib/mongoid/criteria.rb +73 -15
  28. data/lib/mongoid/criterion/inclusion.rb +0 -2
  29. data/lib/mongoid/criterion/optional.rb +2 -10
  30. data/lib/mongoid/document.rb +30 -41
  31. data/lib/mongoid/extensions.rb +0 -15
  32. data/lib/mongoid/field.rb +7 -20
  33. data/lib/mongoid/fields.rb +10 -15
  34. data/lib/mongoid/finders.rb +98 -10
  35. data/lib/mongoid/identity.rb +2 -8
  36. data/lib/mongoid/named_scope.rb +0 -2
  37. data/lib/mongoid/validations/associated.rb +8 -3
  38. data/lib/mongoid/validations/uniqueness.rb +7 -2
  39. data/lib/mongoid/validations.rb +2 -2
  40. data/lib/mongoid/versioning.rb +1 -1
  41. data/lib/mongoid.rb +5 -21
  42. data/mongoid.gemspec +19 -59
  43. data/spec/integration/mongoid/associations_spec.rb +3 -42
  44. data/spec/integration/mongoid/attributes_spec.rb +2 -2
  45. data/spec/integration/mongoid/commands_spec.rb +13 -27
  46. data/spec/integration/mongoid/contexts/enumerable_spec.rb +0 -13
  47. data/spec/integration/mongoid/criteria_spec.rb +3 -50
  48. data/spec/integration/mongoid/document_spec.rb +5 -72
  49. data/spec/integration/mongoid/finders_spec.rb +28 -85
  50. data/spec/models/address.rb +3 -3
  51. data/spec/models/animal.rb +2 -2
  52. data/spec/models/country_code.rb +2 -2
  53. data/spec/models/game.rb +1 -2
  54. data/spec/models/inheritance.rb +5 -5
  55. data/spec/models/location.rb +2 -2
  56. data/spec/models/name.rb +3 -3
  57. data/spec/models/namespacing.rb +2 -2
  58. data/spec/models/patient.rb +0 -2
  59. data/spec/models/person.rb +4 -6
  60. data/spec/models/pet.rb +3 -3
  61. data/spec/models/pet_owner.rb +3 -3
  62. data/spec/models/phone.rb +3 -3
  63. data/spec/models/post.rb +1 -1
  64. data/spec/models/translation.rb +2 -2
  65. data/spec/models/vet_visit.rb +2 -2
  66. data/spec/spec_helper.rb +2 -2
  67. data/spec/unit/mongoid/associations/belongs_to_related_spec.rb +0 -4
  68. data/spec/unit/mongoid/associations/{embedded_in_spec.rb → belongs_to_spec.rb} +11 -11
  69. data/spec/unit/mongoid/associations/has_many_related_spec.rb +14 -45
  70. data/spec/unit/mongoid/associations/{embeds_many_spec.rb → has_many_spec.rb} +34 -79
  71. data/spec/unit/mongoid/associations/{embeds_one_spec.rb → has_one_spec.rb} +18 -18
  72. data/spec/unit/mongoid/associations/options_spec.rb +19 -20
  73. data/spec/unit/mongoid/associations_spec.rb +12 -74
  74. data/spec/unit/mongoid/attributes_spec.rb +51 -83
  75. data/spec/unit/mongoid/collection_spec.rb +0 -46
  76. data/spec/unit/mongoid/commands/delete_all_spec.rb +8 -9
  77. data/spec/unit/mongoid/commands/delete_spec.rb +3 -6
  78. data/spec/unit/mongoid/commands/destroy_all_spec.rb +2 -0
  79. data/spec/unit/mongoid/commands/destroy_spec.rb +3 -10
  80. data/spec/unit/mongoid/commands_spec.rb +11 -20
  81. data/spec/unit/mongoid/config_spec.rb +0 -18
  82. data/spec/unit/mongoid/contexts/enumerable_spec.rb +11 -198
  83. data/spec/unit/mongoid/contexts/mongo_spec.rb +54 -357
  84. data/spec/unit/mongoid/criteria_spec.rb +78 -107
  85. data/spec/unit/mongoid/criterion/exclusion_spec.rb +13 -3
  86. data/spec/unit/mongoid/criterion/inclusion_spec.rb +19 -25
  87. data/spec/unit/mongoid/criterion/optional_spec.rb +18 -25
  88. data/spec/unit/mongoid/document_spec.rb +34 -71
  89. data/spec/unit/mongoid/extensions/array/conversions_spec.rb +2 -2
  90. data/spec/unit/mongoid/extensions/datetime/conversions_spec.rb +3 -0
  91. data/spec/unit/mongoid/extensions/object/conversions_spec.rb +2 -16
  92. data/spec/unit/mongoid/extensions/proc/scoping_spec.rb +1 -1
  93. data/spec/unit/mongoid/field_spec.rb +0 -62
  94. data/spec/unit/mongoid/fields_spec.rb +0 -33
  95. data/spec/unit/mongoid/finders_spec.rb +1 -37
  96. data/spec/unit/mongoid/identity_spec.rb +6 -47
  97. data/spec/unit/mongoid/named_scope_spec.rb +2 -15
  98. data/spec/unit/mongoid/scope_spec.rb +1 -1
  99. data/spec/unit/mongoid/validations/uniqueness_spec.rb +3 -3
  100. data/spec/unit/mongoid_spec.rb +2 -11
  101. metadata +14 -64
  102. data/lib/mongoid/associations/meta_data.rb +0 -28
  103. data/lib/mongoid/contexts/ids.rb +0 -25
  104. data/lib/mongoid/deprecation.rb +0 -22
  105. data/lib/mongoid/dirty.rb +0 -203
  106. data/lib/mongoid/extensions/big_decimal/conversions.rb +0 -19
  107. data/lib/mongoid/extensions/binary/conversions.rb +0 -17
  108. data/lib/mongoid/extensions/objectid/conversions.rb +0 -15
  109. data/lib/mongoid/extras.rb +0 -61
  110. data/lib/mongoid/javascript/functions.yml +0 -37
  111. data/lib/mongoid/javascript.rb +0 -21
  112. data/lib/mongoid/observable.rb +0 -30
  113. data/lib/mongoid/paths.rb +0 -54
  114. data/lib/mongoid/persistence/command.rb +0 -20
  115. data/lib/mongoid/persistence/insert.rb +0 -71
  116. data/lib/mongoid/persistence/update.rb +0 -78
  117. data/lib/mongoid/persistence.rb +0 -27
  118. data/lib/mongoid/state.rb +0 -32
  119. data/spec/integration/mongoid/dirty_spec.rb +0 -70
  120. data/spec/integration/mongoid/persistence/update_spec.rb +0 -46
  121. data/spec/models/callbacks.rb +0 -18
  122. data/spec/unit/mongoid/associations/meta_data_spec.rb +0 -88
  123. data/spec/unit/mongoid/contexts_spec.rb +0 -25
  124. data/spec/unit/mongoid/deprecation_spec.rb +0 -24
  125. data/spec/unit/mongoid/dirty_spec.rb +0 -286
  126. data/spec/unit/mongoid/extensions/big_decimal/conversions_spec.rb +0 -22
  127. data/spec/unit/mongoid/extensions/binary/conversions_spec.rb +0 -22
  128. data/spec/unit/mongoid/extras_spec.rb +0 -102
  129. data/spec/unit/mongoid/javascript_spec.rb +0 -48
  130. data/spec/unit/mongoid/observable_spec.rb +0 -46
  131. data/spec/unit/mongoid/paths_spec.rb +0 -124
  132. data/spec/unit/mongoid/persistence/insert_spec.rb +0 -175
  133. data/spec/unit/mongoid/persistence/update_spec.rb +0 -148
  134. data/spec/unit/mongoid/persistence_spec.rb +0 -40
  135. data/spec/unit/mongoid/state_spec.rb +0 -83
@@ -120,19 +120,6 @@ describe Mongoid::Criteria do
120
120
 
121
121
  end
122
122
 
123
- describe "#avg" do
124
-
125
- before do
126
- @context = stub.quacks_like(Mongoid::Contexts::Mongo.allocate)
127
- @criteria.instance_variable_set(:@context, @context)
128
- end
129
-
130
- it "delegates to the context" do
131
- @context.expects(:avg).with(:age)
132
- @criteria.avg(:age)
133
- end
134
- end
135
-
136
123
  describe "#blank?" do
137
124
 
138
125
  before do
@@ -140,10 +127,10 @@ describe Mongoid::Criteria do
140
127
  @criteria.instance_variable_set(:@context, @context)
141
128
  end
142
129
 
143
- context "when the context is blank" do
130
+ context "when the count is 0" do
144
131
 
145
132
  before do
146
- @context.expects(:blank?).returns(true)
133
+ @context.expects(:count).returns(0)
147
134
  end
148
135
 
149
136
  it "returns true" do
@@ -151,10 +138,10 @@ describe Mongoid::Criteria do
151
138
  end
152
139
  end
153
140
 
154
- context "when the context is not blank" do
141
+ context "when the count is greater than 0" do
155
142
 
156
143
  before do
157
- @context.expects(:blank?).returns(false)
144
+ @context.expects(:count).returns(10)
158
145
  end
159
146
 
160
147
  it "returns false" do
@@ -185,7 +172,9 @@ describe Mongoid::Criteria do
185
172
  end
186
173
 
187
174
  it "creates a new context" do
188
- Mongoid::Contexts::Mongo.expects(:new).with(@criteria).returns(@context)
175
+ Mongoid::Contexts::Mongo.expects(:new).with(
176
+ @criteria.selector, @criteria.options, @criteria.klass
177
+ ).returns(@context)
189
178
  @criteria.context.should == @context
190
179
  end
191
180
 
@@ -213,19 +202,6 @@ describe Mongoid::Criteria do
213
202
 
214
203
  end
215
204
 
216
- describe "#distinct" do
217
-
218
- before do
219
- @context = stub.quacks_like(Mongoid::Contexts::Mongo.allocate)
220
- @criteria.instance_variable_set(:@context, @context)
221
- end
222
-
223
- it "delegates to the context" do
224
- @context.expects(:distinct).with(:title)
225
- @criteria.distinct(:title)
226
- end
227
- end
228
-
229
205
  describe "#entries" do
230
206
 
231
207
  context "filtering" do
@@ -269,7 +245,7 @@ describe Mongoid::Criteria do
269
245
  criteria = Mongoid::Criteria.new(Person)
270
246
  collection = mock
271
247
  Person.expects(:collection).returns(collection)
272
- collection.expects(:find).with(criteria.selector, criteria.options).returns([])
248
+ collection.expects(:find).with(@criteria.selector, @criteria.options).returns([])
273
249
  criteria.entries.should == []
274
250
  end
275
251
 
@@ -300,13 +276,6 @@ describe Mongoid::Criteria do
300
276
  @cursor = stub(:count => 10)
301
277
  end
302
278
 
303
- it "delegates to the context#iterate" do
304
- @context = stub('context')
305
- @criteria.stubs(:context).returns(@context)
306
- @context.expects(:iterate)
307
- @criteria.each
308
- end
309
-
310
279
  context "when the criteria has not been executed" do
311
280
 
312
281
  before do
@@ -351,12 +320,7 @@ describe Mongoid::Criteria do
351
320
 
352
321
  before do
353
322
  Person.expects(:collection).returns(@collection)
354
- @collection.expects(:find).with(
355
- { :_type => { "$in" => ["Doctor", "Person"] },
356
- :title => "Sir"
357
- },
358
- { :cache => true }
359
- ).returns(@cursor)
323
+ @collection.expects(:find).with({ :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir" }, {}).returns(@cursor)
360
324
  @cursor.expects(:each).yields(@person)
361
325
  @criteria.cache
362
326
  @criteria.each do |doc|
@@ -408,22 +372,22 @@ describe Mongoid::Criteria do
408
372
 
409
373
  describe "#initialize" do
410
374
 
411
- let(:criteria) { Mongoid::Criteria.new(Person) }
375
+ context "when class is hereditary" do
412
376
 
413
- it "sets the selector to an empty hash" do
414
- criteria.selector.should == {}
415
- end
377
+ it "sets the _type value on the selector" do
378
+ criteria = Mongoid::Criteria.new(Person)
379
+ criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] } }
380
+ end
416
381
 
417
- it "sets the options to an empty hash" do
418
- criteria.options.should == {}
419
382
  end
420
383
 
421
- it "sets the documents to an empty array" do
422
- criteria.documents.should == []
423
- end
384
+ context "when class is not hereditary" do
385
+
386
+ it "sets no _type value on the selector" do
387
+ criteria = Mongoid::Criteria.new(Game)
388
+ criteria.selector.should == {}
389
+ end
424
390
 
425
- it "sets the klass to the given class" do
426
- criteria.klass.should == Person
427
391
  end
428
392
 
429
393
  end
@@ -469,7 +433,7 @@ describe Mongoid::Criteria do
469
433
  before do
470
434
  @other = Mongoid::Criteria.new(Person)
471
435
  @other.where(:name => "Chloe").order_by([[:name, :asc]])
472
- @selector = { :title => "Sir", :age => 30, :name => "Chloe" }
436
+ @selector = { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir", :age => 30, :name => "Chloe" }
473
437
  @options = { :skip => 40, :limit => 20, :sort => [[:name, :asc]] }
474
438
  end
475
439
 
@@ -485,7 +449,7 @@ describe Mongoid::Criteria do
485
449
 
486
450
  before do
487
451
  @other = Mongoid::Criteria.new(Person)
488
- @selector = { :title => "Sir", :age => 30 }
452
+ @selector = { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir", :age => 30 }
489
453
  @options = { :skip => 40, :limit => 20 }
490
454
  end
491
455
 
@@ -525,7 +489,7 @@ describe Mongoid::Criteria do
525
489
 
526
490
  it "merges the criteria with the next one" do
527
491
  @new_criteria = @criteria.accepted
528
- @new_criteria.selector.should == { :title => "Sir", :terms => true }
492
+ @new_criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir", :terms => true }
529
493
  end
530
494
 
531
495
  context "chaining more than one scope" do
@@ -535,7 +499,8 @@ describe Mongoid::Criteria do
535
499
  end
536
500
 
537
501
  it "returns the final merged criteria" do
538
- @criteria.selector.should == { :title => "Sir", :terms => true, :age => { "$gt" => 50 } }
502
+ @criteria.selector.should ==
503
+ { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Sir", :terms => true, :age => { "$gt" => 50 } }
539
504
  end
540
505
 
541
506
  end
@@ -651,7 +616,8 @@ describe Mongoid::Criteria do
651
616
  end
652
617
 
653
618
  it "returns the selector plus the options" do
654
- @criteria.scoped.should == { :where => { :title => "Sir" }, :skip => 20 }
619
+ @criteria.scoped.should ==
620
+ { :where => { :title => "Sir", :_type=>{ "$in" => [ "Doctor", "Person" ] } }, :skip => 20 }
655
621
  end
656
622
 
657
623
  end
@@ -674,35 +640,29 @@ describe Mongoid::Criteria do
674
640
 
675
641
  context "with a single argument" do
676
642
 
677
- context "when the arg is a string" do
678
-
679
- before do
680
- @id = Mongo::ObjectID.new.to_s
681
- @document = stub
682
- @criteria = mock
683
- Mongoid::Criteria.expects(:new).returns(@criteria)
684
- end
643
+ before do
644
+ @id = Mongo::ObjectID.new.to_s
645
+ @document = stub
646
+ @criteria = mock
647
+ Mongoid::Criteria.expects(:new).returns(@criteria)
648
+ @criteria.expects(:id).with(@id).returns(@criteria)
649
+ end
685
650
 
686
- it "delegates to #id_criteria" do
687
- @criteria.expects(:id_criteria).with(@id).returns(@document)
688
- Mongoid::Criteria.translate(Person, @id).should == @document
689
- end
651
+ it "creates a criteria for a string" do
652
+ @criteria.expects(:one).returns(@document)
653
+ @document.expects(:blank? => false)
654
+ Mongoid::Criteria.translate(Person, @id)
690
655
  end
691
656
 
692
- context "when the arg is an object id" do
657
+ context "when the document is not found" do
693
658
 
694
- before do
695
- @id = Mongo::ObjectID.new
696
- @document = stub
697
- @criteria = mock
698
- Mongoid::Criteria.expects(:new).returns(@criteria)
659
+ it "raises an error" do
660
+ @criteria.expects(:one).returns(nil)
661
+ lambda { Mongoid::Criteria.translate(Person, @id) }.should raise_error
699
662
  end
700
663
 
701
- it "delegates to #id_criteria" do
702
- @criteria.expects(:id_criteria).with(@id).returns(@document)
703
- Mongoid::Criteria.translate(Person, @id).should == @document
704
- end
705
664
  end
665
+
706
666
  end
707
667
 
708
668
  context "multiple arguments" do
@@ -716,41 +676,52 @@ describe Mongoid::Criteria do
716
676
  @ids << Mongo::ObjectID.new.to_s
717
677
  @documents << stub
718
678
  end
719
- @criteria = mock
720
- Mongoid::Criteria.expects(:new).returns(@criteria)
721
- end
679
+ @collection = stub
680
+ Person.expects(:collection).returns(@collection)
681
+ end
682
+
683
+ context "when documents are found" do
684
+
685
+ it "returns an ids criteria" do
686
+ @collection.expects(:find).with(
687
+ { :_type =>
688
+ { "$in" =>
689
+ ["Doctor", "Person"]
690
+ },
691
+ :_id =>
692
+ { "$in" => @ids }
693
+ }, {}).returns([{ "_id" => "4", "title" => "Sir", "_type" => "Person" }])
694
+ @criteria = Mongoid::Criteria.translate(Person, @ids)
695
+ end
722
696
 
723
- it "delegates to #id_criteria" do
724
- @criteria.expects(:id_criteria).with(@ids).returns(@documents)
725
- Mongoid::Criteria.translate(Person, @ids).should == @documents
726
697
  end
727
698
 
728
- end
699
+ context "when documents are not found" do
729
700
 
730
- context "when Person, :conditions => {}" do
731
-
732
- before do
733
- @criteria = Mongoid::Criteria.translate(Person, :conditions => { :title => "Test" })
734
- end
735
-
736
- it "returns a criteria with a selector from the conditions" do
737
- @criteria.selector.should == { :title => "Test" }
738
- end
701
+ it "returns an ids criteria" do
702
+ @collection.expects(:find).with(
703
+ { :_type =>
704
+ { "$in" =>
705
+ ["Doctor", "Person"]
706
+ },
707
+ :_id =>
708
+ { "$in" => @ids }
709
+ }, {}).returns([])
710
+ lambda { Mongoid::Criteria.translate(Person, @ids) }.should raise_error
711
+ end
739
712
 
740
- it "returns a criteria with klass Person" do
741
- @criteria.klass.should == Person
742
713
  end
743
714
 
744
715
  end
745
716
 
746
- context "when Person, :conditions => {:id => id}" do
717
+ context "when Person, :conditions => {}" do
747
718
 
748
719
  before do
749
- @criteria = Mongoid::Criteria.translate(Person, :conditions => { :id => "1234e567" })
720
+ @criteria = Mongoid::Criteria.translate(Person, :conditions => { :title => "Test" })
750
721
  end
751
722
 
752
723
  it "returns a criteria with a selector from the conditions" do
753
- @criteria.selector.should == { :_id => "1234e567" }
724
+ @criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Test" }
754
725
  end
755
726
 
756
727
  it "returns a criteria with klass Person" do
@@ -766,7 +737,7 @@ describe Mongoid::Criteria do
766
737
  end
767
738
 
768
739
  it "returns a criteria with a selector from the conditions" do
769
- @criteria.selector.should == { :title => "Test" }
740
+ @criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Test" }
770
741
  end
771
742
 
772
743
  it "returns a criteria with klass Person" do
@@ -782,7 +753,7 @@ describe Mongoid::Criteria do
782
753
  end
783
754
 
784
755
  it "returns a criteria with a selector from the conditions" do
785
- @criteria.selector.should == { :title => "Test" }
756
+ @criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Test" }
786
757
  end
787
758
 
788
759
  it "returns a criteria with klass Person" do
@@ -797,7 +768,7 @@ describe Mongoid::Criteria do
797
768
  end
798
769
 
799
770
  it "adds the criteria and the options" do
800
- @criteria.selector.should == { :title => "Test" }
771
+ @criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Test" }
801
772
  @criteria.options.should == { :skip => 10 }
802
773
  end
803
774
 
@@ -12,7 +12,10 @@ describe Mongoid::Criterion::Exclusion do
12
12
  it "adds the $ne query to the selector" do
13
13
  @criteria.excludes(:title => "Bad Title", :text => "Bad Text")
14
14
  @criteria.selector.should ==
15
- {
15
+ { :_type =>
16
+ { "$in" =>
17
+ ["Doctor", "Person"]
18
+ },
16
19
  :title =>
17
20
  { "$ne" => "Bad Title"},
18
21
  :text =>
@@ -29,7 +32,10 @@ describe Mongoid::Criterion::Exclusion do
29
32
  it "accepts id" do
30
33
  @criteria.excludes(:id => "1")
31
34
  @criteria.selector.should ==
32
- {
35
+ { :_type =>
36
+ { "$in" =>
37
+ ["Doctor", "Person"]
38
+ },
33
39
  :_id => { "$ne" => "1" }
34
40
  }
35
41
  end
@@ -37,7 +43,10 @@ describe Mongoid::Criterion::Exclusion do
37
43
  it "accepts _id" do
38
44
  @criteria.excludes(:_id => "1")
39
45
  @criteria.selector.should ==
40
- {
46
+ { :_type =>
47
+ { "$in" =>
48
+ ["Doctor", "Person"]
49
+ },
41
50
  :_id => { "$ne" => "1" }
42
51
  }
43
52
  end
@@ -51,6 +60,7 @@ describe Mongoid::Criterion::Exclusion do
51
60
  it "adds the exclusion to the selector" do
52
61
  @criteria.not_in(:title => ["title1", "title2"], :text => ["test"])
53
62
  @criteria.selector.should == {
63
+ :_type => { "$in" => ["Doctor", "Person"] },
54
64
  :title => { "$nin" => ["title1", "title2"] },
55
65
  :text => { "$nin" => ["test"] }
56
66
  }
@@ -12,7 +12,7 @@ describe Mongoid::Criterion::Inclusion do
12
12
  it "adds the $all query to the selector" do
13
13
  @criteria.all(:title => ["title1", "title2"])
14
14
  @criteria.selector.should ==
15
- {
15
+ { :_type => { "$in" => ["Doctor", "Person"] },
16
16
  :title => { "$all" => ["title1", "title2"] }
17
17
  }
18
18
  end
@@ -30,7 +30,7 @@ describe Mongoid::Criterion::Inclusion do
30
30
  it "adds the clause to the selector" do
31
31
  @criteria.and(:title => "Title", :text => "Text")
32
32
  @criteria.selector.should ==
33
- {
33
+ { :_type => { "$in" => ["Doctor", "Person"] },
34
34
  :title => "Title",
35
35
  :text => "Text"
36
36
  }
@@ -43,7 +43,7 @@ describe Mongoid::Criterion::Inclusion do
43
43
  it "adds the $where clause to the selector" do
44
44
  @criteria.and("this.date < new Date()")
45
45
  @criteria.selector.should ==
46
- {
46
+ { :_type => { "$in" => ["Doctor", "Person"] },
47
47
  "$where" => "this.date < new Date()"
48
48
  }
49
49
  end
@@ -61,16 +61,10 @@ describe Mongoid::Criterion::Inclusion do
61
61
  it "adds the $in clause to the selector" do
62
62
  @criteria.in(:title => ["title1", "title2"], :text => ["test"])
63
63
  @criteria.selector.should ==
64
- {
65
- :title => { "$in" => ["title1", "title2"] }, :text => { "$in" => ["test"] }
66
- }
67
- end
68
-
69
- it "#any_in is aliased to #in" do
70
- @criteria.any_in(:title => ["title1", "title2"], :text => ["test"])
71
- @criteria.selector.should ==
72
- {
73
- :title => { "$in" => ["title1", "title2"] }, :text => { "$in" => ["test"] }
64
+ { :_type =>
65
+ { "$in" =>
66
+ ["Doctor", "Person"]
67
+ }, :title => { "$in" => ["title1", "title2"] }, :text => { "$in" => ["test"] }
74
68
  }
75
69
  end
76
70
 
@@ -89,7 +83,7 @@ describe Mongoid::Criterion::Inclusion do
89
83
  it "adds the clause to the selector" do
90
84
  @criteria.where(:title => "Title", :text => "Text")
91
85
  @criteria.selector.should ==
92
- { :title => "Title", :text => "Text" }
86
+ { :_type => { "$in" => ["Doctor", "Person"] }, :title => "Title", :text => "Text" }
93
87
  end
94
88
 
95
89
  end
@@ -101,7 +95,7 @@ describe Mongoid::Criterion::Inclusion do
101
95
  it "returns those matching an all clause" do
102
96
  @criteria.where(:title.all => ["Sir"])
103
97
  @criteria.selector.should ==
104
- { :title => { "$all" => ["Sir"] } }
98
+ { :_type => { "$in" => ["Doctor", "Person"] }, :title => { "$all" => ["Sir"] } }
105
99
  end
106
100
 
107
101
  end
@@ -111,7 +105,7 @@ describe Mongoid::Criterion::Inclusion do
111
105
  it "returns those matching an exists clause" do
112
106
  @criteria.where(:title.exists => true)
113
107
  @criteria.selector.should ==
114
- { :title => { "$exists" => true } }
108
+ { :_type => { "$in" => ["Doctor", "Person"] }, :title => { "$exists" => true } }
115
109
  end
116
110
 
117
111
  end
@@ -121,7 +115,7 @@ describe Mongoid::Criterion::Inclusion do
121
115
  it "returns those matching a gt clause" do
122
116
  @criteria.where(:age.gt => 30)
123
117
  @criteria.selector.should ==
124
- { :age => { "$gt" => 30 } }
118
+ { :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$gt" => 30 } }
125
119
  end
126
120
 
127
121
  end
@@ -131,7 +125,7 @@ describe Mongoid::Criterion::Inclusion do
131
125
  it "returns those matching a gte clause" do
132
126
  @criteria.where(:age.gte => 33)
133
127
  @criteria.selector.should ==
134
- { :age => { "$gte" => 33 } }
128
+ { :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$gte" => 33 } }
135
129
  end
136
130
 
137
131
  end
@@ -141,7 +135,7 @@ describe Mongoid::Criterion::Inclusion do
141
135
  it "returns those matching an in clause" do
142
136
  @criteria.where(:title.in => ["Sir", "Madam"])
143
137
  @criteria.selector.should ==
144
- { :title => { "$in" => ["Sir", "Madam"] } }
138
+ { :_type => { "$in" => ["Doctor", "Person"] }, :title => { "$in" => ["Sir", "Madam"] } }
145
139
  end
146
140
 
147
141
  end
@@ -151,7 +145,7 @@ describe Mongoid::Criterion::Inclusion do
151
145
  it "returns those matching a lt clause" do
152
146
  @criteria.where(:age.lt => 34)
153
147
  @criteria.selector.should ==
154
- { :age => { "$lt" => 34 } }
148
+ { :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$lt" => 34 } }
155
149
  end
156
150
 
157
151
  end
@@ -161,7 +155,7 @@ describe Mongoid::Criterion::Inclusion do
161
155
  it "returns those matching a lte clause" do
162
156
  @criteria.where(:age.lte => 33)
163
157
  @criteria.selector.should ==
164
- { :age => { "$lte" => 33 } }
158
+ { :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$lte" => 33 } }
165
159
  end
166
160
 
167
161
  end
@@ -171,7 +165,7 @@ describe Mongoid::Criterion::Inclusion do
171
165
  it "returns those matching a ne clause" do
172
166
  @criteria.where(:age.ne => 50)
173
167
  @criteria.selector.should ==
174
- { :age => { "$ne" => 50 } }
168
+ { :_type => { "$in" => ["Doctor", "Person"] }, :age => { "$ne" => 50 } }
175
169
  end
176
170
 
177
171
  end
@@ -181,7 +175,7 @@ describe Mongoid::Criterion::Inclusion do
181
175
  it "returns those matching a nin clause" do
182
176
  @criteria.where(:title.nin => ["Esquire", "Congressman"])
183
177
  @criteria.selector.should ==
184
- { :title => { "$nin" => ["Esquire", "Congressman"] } }
178
+ { :_type => { "$in" => ["Doctor", "Person"] }, :title => { "$nin" => ["Esquire", "Congressman"] } }
185
179
  end
186
180
 
187
181
  end
@@ -191,7 +185,7 @@ describe Mongoid::Criterion::Inclusion do
191
185
  it "returns those matching a size clause" do
192
186
  @criteria.where(:aliases.size => 2)
193
187
  @criteria.selector.should ==
194
- { :aliases => { "$size" => 2 } }
188
+ { :_type => { "$in" => ["Doctor", "Person"] }, :aliases => { "$size" => 2 } }
195
189
  end
196
190
 
197
191
  end
@@ -205,7 +199,7 @@ describe Mongoid::Criterion::Inclusion do
205
199
  it "adds the $where clause to the selector" do
206
200
  @criteria.where("this.date < new Date()")
207
201
  @criteria.selector.should ==
208
- { "$where" => "this.date < new Date()" }
202
+ { :_type => { "$in" => ["Doctor", "Person"] }, "$where" => "this.date < new Date()" }
209
203
  end
210
204
 
211
205
  end
@@ -30,6 +30,16 @@ describe Mongoid::Criterion::Optional do
30
30
  it "returns true" do
31
31
  @criteria.cached?.should be_true
32
32
  end
33
+
34
+ it "removes cache from the options" do
35
+ @criteria.cached?
36
+ @criteria.options[:cache].should be_nil
37
+ end
38
+
39
+ it "sets the cache instance variable" do
40
+ @criteria.cached?
41
+ @criteria.instance_variable_get(:@cached).should be_true
42
+ end
33
43
  end
34
44
 
35
45
  context "when the criteria has no cache option" do
@@ -102,32 +112,15 @@ describe Mongoid::Criterion::Optional do
102
112
 
103
113
  context "when passing a single id" do
104
114
 
105
- context "when the id is a string" do
106
-
107
- it "adds the _id query to the selector" do
108
- id = Mongo::ObjectID.new.to_s
109
- @criteria.id(id)
110
- @criteria.selector.should == { :_id => id }
111
- end
112
-
113
- it "returns self" do
114
- id = Mongo::ObjectID.new.to_s
115
- @criteria.id(id).should == @criteria
116
- end
115
+ it "adds the _id query to the selector" do
116
+ id = Mongo::ObjectID.new.to_s
117
+ @criteria.id(id)
118
+ @criteria.selector.should == { :_type => { "$in" => ["Doctor", "Person"] }, :_id => id }
117
119
  end
118
120
 
119
- context "when the id is an object id" do
120
-
121
- it "adds the _id query to the selector" do
122
- id = Mongo::ObjectID.new
123
- @criteria.id(id)
124
- @criteria.selector.should == { :_id => id }
125
- end
126
-
127
- it "returns self" do
128
- id = Mongo::ObjectID.new
129
- @criteria.id(id).should == @criteria
130
- end
121
+ it "returns self" do
122
+ id = Mongo::ObjectID.new.to_s
123
+ @criteria.id(id.to_s).should == @criteria
131
124
  end
132
125
 
133
126
  end
@@ -142,7 +135,7 @@ describe Mongoid::Criterion::Optional do
142
135
  it "adds the _id query to the selector" do
143
136
  @criteria.id(@ids)
144
137
  @criteria.selector.should ==
145
- { :_id => { "$in" => @ids } }
138
+ { :_type => { "$in" => ["Doctor", "Person"] }, :_id => { "$in" => @ids } }
146
139
  end
147
140
 
148
141
  end