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
@@ -5,9 +5,9 @@ describe Mongoid::Contexts::Mongo do
5
5
  describe "#aggregate" do
6
6
 
7
7
  before do
8
- @criteria = Mongoid::Criteria.new(Person)
9
- @criteria.only(:field1)
10
- @context = Mongoid::Contexts::Mongo.new(@criteria)
8
+ @selector = {}
9
+ @options = { :fields => [:field1] }
10
+ @context = Mongoid::Contexts::Mongo.new(@selector, @options, Person)
11
11
  end
12
12
 
13
13
  context "when klass not provided" do
@@ -19,78 +19,20 @@ describe Mongoid::Contexts::Mongo do
19
19
  end
20
20
 
21
21
  it "calls group on the collection with the aggregate js" do
22
- @collection.expects(:group).with([:field1], {:_type => {'$in' => ['Doctor', 'Person']}}, {:count => 0}, @reduce, true)
22
+ @collection.expects(:group).with([:field1], {}, {:count => 0}, @reduce, true)
23
23
  @context.aggregate
24
24
  end
25
- end
26
- end
27
-
28
- describe "#avg" do
29
-
30
- before do
31
- @reduce = Mongoid::Javascript.sum.gsub("[field]", "age")
32
- @collection = mock
33
- Person.expects(:collection).twice.returns(@collection)
34
- @criteria = Mongoid::Criteria.new(Person)
35
- @context = Mongoid::Contexts::Mongo.new(@criteria)
36
- end
37
-
38
- it "calls group on the collection with the aggregate js" do
39
- @collection.expects(:group).with(
40
- nil,
41
- {:_type => {'$in' => ['Doctor', 'Person']}},
42
- {:sum => "start"},
43
- @reduce
44
- ).returns([{"sum" => 100.0}])
45
- @cursor = mock(:count => 10)
46
- @collection.expects(:find).returns(@cursor)
47
- @context.avg(:age).should == 10
48
- end
49
- end
50
-
51
- describe "blank?" do
52
-
53
- before do
54
- @criteria = Mongoid::Criteria.new(Game)
55
- @criteria.where(:test => 'Testing')
56
- @context = Mongoid::Contexts::Mongo.new(@criteria)
57
- end
58
-
59
- context "when a document exists" do
60
-
61
- before do
62
- @doc = mock
63
- @collection = mock
64
- Game.expects(:collection).returns(@collection)
65
- @collection.expects(:find_one).with({ :test => "Testing" }, { :fields => [ :_id ] }).returns(@doc)
66
- end
67
25
 
68
- it "returns false" do
69
- @context.blank?.should be_false
70
- end
71
26
  end
72
27
 
73
- context "when a document does not exist" do
74
-
75
- before do
76
- @doc = mock
77
- @collection = mock
78
- Game.expects(:collection).returns(@collection)
79
- @collection.expects(:find_one).with({ :test => "Testing" }, { :fields => [ :_id ] }).returns(nil)
80
- end
81
-
82
- it "returns true" do
83
- @context.blank?.should be_true
84
- end
85
- end
86
28
  end
87
29
 
88
30
  describe "#count" do
89
31
 
90
32
  before do
91
- @criteria = Mongoid::Criteria.new(Person)
92
- @criteria.where(:test => 'Testing')
93
- @context = Mongoid::Contexts::Mongo.new(@criteria)
33
+ @selector = { :_type => { "$in" => ["Doctor", "Person"] }, :test => "Testing" }
34
+ @options = {}
35
+ @context = Mongoid::Contexts::Mongo.new(@selector, @options, Person)
94
36
  end
95
37
 
96
38
  context "when criteria has not been executed" do
@@ -114,7 +56,7 @@ describe Mongoid::Contexts::Mongo do
114
56
  end
115
57
 
116
58
  it "returns the count from the cursor without creating the documents" do
117
- @collection.expects(:find).with(@criteria.selector, {}).returns(@cursor)
59
+ @collection.expects(:find).with(@selector, {}).returns(@cursor)
118
60
  @cursor.expects(:count).returns(10)
119
61
  @context.count.should == 10
120
62
  end
@@ -123,22 +65,6 @@ describe Mongoid::Contexts::Mongo do
123
65
 
124
66
  end
125
67
 
126
- describe "#distinct" do
127
-
128
- before do
129
- @criteria = Mongoid::Criteria.new(Person)
130
- @criteria.where(:test => 'Testing')
131
- @context = Mongoid::Contexts::Mongo.new(@criteria)
132
- @collection = mock
133
- Person.expects(:collection).returns(@collection)
134
- end
135
-
136
- it "returns delegates to distinct on the collection" do
137
- @collection.expects(:distinct).with(:title, @criteria.selector).returns(["Sir"])
138
- @context.distinct(:title).should == ["Sir"]
139
- end
140
- end
141
-
142
68
  describe "#execute" do
143
69
 
144
70
  let(:selector) { { :field => "value" } }
@@ -147,16 +73,8 @@ describe Mongoid::Contexts::Mongo do
147
73
  before do
148
74
  @cursor = stub(:count => 500)
149
75
  @collection = mock
150
- @klass = stub(
151
- :collection => @collection,
152
- :hereditary => false,
153
- :instantiate => @person,
154
- :enslaved? => false,
155
- :cached? => false
156
- )
157
- @criteria = Mongoid::Criteria.new(@klass)
158
- @criteria.where(selector).skip(20)
159
- @context = Mongoid::Contexts::Mongo.new(@criteria)
76
+ @klass = stub(:collection => @collection, :hereditary => false, :instantiate => @person)
77
+ @context = Mongoid::Contexts::Mongo.new(selector, options, @klass)
160
78
  end
161
79
 
162
80
  it "calls find on the collection" do
@@ -179,7 +97,7 @@ describe Mongoid::Contexts::Mongo do
179
97
  context "when _type not in the field list" do
180
98
 
181
99
  before do
182
- @criteria.only(:title)
100
+ options[:fields] = [ :title ]
183
101
  @expected_options = { :skip => 20, :fields => [ :title, :_type ] }
184
102
  end
185
103
 
@@ -197,10 +115,10 @@ describe Mongoid::Contexts::Mongo do
197
115
  describe "#group" do
198
116
 
199
117
  before do
200
- @criteria = Mongoid::Criteria.new(Person)
201
- @criteria.only(:field1)
118
+ @selector = { :_type => { "$in" => ["Doctor", "Person"] } }
119
+ @options = { :fields => [ :field1 ] }
202
120
  @grouping = [{ "title" => "Sir", "group" => [{ "title" => "Sir", "age" => 30, "_type" => "Person" }] }]
203
- @context = Mongoid::Contexts::Mongo.new(@criteria)
121
+ @context = Mongoid::Contexts::Mongo.new(@selector, @options, Person)
204
122
  end
205
123
 
206
124
  context "when klass provided" do
@@ -212,12 +130,7 @@ describe Mongoid::Contexts::Mongo do
212
130
  end
213
131
 
214
132
  it "calls group on the collection with the aggregate js" do
215
- @collection.expects(:group).with(
216
- [:field1],
217
- {:_type => { "$in" => ["Doctor", "Person"] }},
218
- {:group => []},
219
- @reduce
220
- ).returns(@grouping)
133
+ @collection.expects(:group).with([:field1], {:_type => { "$in" => ["Doctor", "Person"] }}, {:group => []}, @reduce, true).returns(@grouping)
221
134
  @context.group
222
135
  end
223
136
 
@@ -230,118 +143,21 @@ describe Mongoid::Contexts::Mongo do
230
143
  let(:selector) { { :field => "value" } }
231
144
  let(:options) { { :skip => 20 } }
232
145
  let(:klass) { Person }
233
- let(:criteria) { Mongoid::Criteria.new(klass) }
234
- let(:context) { Mongoid::Contexts::Mongo.new(criteria) }
235
146
 
236
147
  before do
237
- criteria.where(selector).skip(20)
148
+ @context = Mongoid::Contexts::Mongo.new(selector, options, klass)
238
149
  end
239
150
 
240
151
  it "sets the selector" do
241
- context.selector.should == criteria.selector
152
+ @context.selector.should == selector
242
153
  end
243
154
 
244
155
  it "sets the options" do
245
- context.options.should == options
156
+ @context.options.should == options
246
157
  end
247
158
 
248
159
  it "sets the klass" do
249
- context.klass.should == klass
250
- end
251
-
252
- context "when persisting type" do
253
-
254
- it "set the selector to query across the _type when it is hereditary" do
255
- context.selector[:_type].should == {'$in' => Person._types}
256
- end
257
-
258
- end
259
-
260
- context "when not persisting type" do
261
-
262
- before do
263
- Mongoid.persist_types = false
264
- end
265
-
266
- after do
267
- Mongoid.persist_types = true
268
- end
269
-
270
- it "does not add the type to the selector" do
271
- context.selector[:_type].should be_nil
272
- end
273
-
274
- end
275
-
276
- context "enslaved and cached classes" do
277
-
278
- let(:klass) { Game }
279
-
280
- it "enslaves the criteria" do
281
- context.criteria.should be_enslaved
282
- end
283
-
284
- it "caches the criteria" do
285
- context.criteria.should be_cached
286
- end
287
- end
288
- end
289
-
290
- describe "#iterate" do
291
- before do
292
- @criteria = Mongoid::Criteria.new(Person)
293
- @context = Mongoid::Contexts::Mongo.new(@criteria)
294
- @person = Person.new(:title => "Sir")
295
- @cursor = stub('cursor')
296
- @cursor.stubs(:each).yields(@person)
297
- end
298
-
299
- context "when not caching" do
300
-
301
- it "executes the criteria" do
302
- @context.expects(:execute).returns(@cursor)
303
- @context.iterate do |person|
304
- person.should == @person
305
- end
306
-
307
- end
308
-
309
- end
310
-
311
- context "when caching" do
312
- before do
313
- @criteria.cache
314
- end
315
-
316
- it "executes the criteria" do
317
- @context.expects(:execute).returns(@cursor)
318
- @context.iterate do |person|
319
- person.should == @person
320
- end
321
- end
322
-
323
- it "executes only once and it caches the result" do
324
- @context.expects(:execute).once.returns(@cursor)
325
- @context.iterate do |person|
326
- person.should == @person
327
- end
328
- @context.iterate do |person|
329
- person.should == @person
330
- end
331
- end
332
-
333
- it "executes even if there is no block" do
334
- @context.expects(:execute).once.returns(@cursor)
335
- @context.iterate
336
- end
337
-
338
- it "caches even if there is no block" do
339
- @context.expects(:execute).once.returns(@cursor)
340
- @context.iterate
341
- @context.iterate do |person|
342
- person.should == @person
343
- end
344
- end
160
+ @context.klass.should == klass
345
161
  end
346
162
 
347
163
  end
@@ -356,13 +172,10 @@ describe Mongoid::Contexts::Mongo do
356
172
  context "when documents exist" do
357
173
 
358
174
  before do
359
- @criteria = Mongoid::Criteria.new(Person)
360
- @criteria.order_by([[:title, :asc]])
361
- @context = Mongoid::Contexts::Mongo.new(@criteria)
362
- @collection.expects(:find_one).with(
363
- {:_type => {'$in' => ['Doctor', 'Person']}},
364
- { :sort => [[:title, :desc]] }
365
- ).returns(
175
+ @selector = {}
176
+ @options = { :sort => [[:title, :asc]] }
177
+ @context = Mongoid::Contexts::Mongo.new(@selector, @options, Person)
178
+ @collection.expects(:find_one).with(@selector, { :sort => [[:title, :desc]] }).returns(
366
179
  { "title" => "Sir", "_type" => "Person" }
367
180
  )
368
181
  end
@@ -376,13 +189,10 @@ describe Mongoid::Contexts::Mongo do
376
189
  context "when no documents exist" do
377
190
 
378
191
  before do
379
- @criteria = Mongoid::Criteria.new(Person)
380
- @criteria.order_by([[:_id, :asc]])
381
- @context = Mongoid::Contexts::Mongo.new(@criteria)
382
- @collection.expects(:find_one).with(
383
- {:_type => {'$in' => ['Doctor', 'Person']}},
384
- { :sort => [[:_id, :desc]] }
385
- ).returns(nil)
192
+ @selector = {}
193
+ @options = { :sort => [[:_id, :asc]] }
194
+ @context = Mongoid::Contexts::Mongo.new(@selector, @options, Person)
195
+ @collection.expects(:find_one).with(@selector, { :sort => [[:_id, :desc]] }).returns(nil)
386
196
  end
387
197
 
388
198
  it "returns nil" do
@@ -394,13 +204,10 @@ describe Mongoid::Contexts::Mongo do
394
204
  context "when no sorting options provided" do
395
205
 
396
206
  before do
397
- @criteria = Mongoid::Criteria.new(Person)
398
- @criteria.order_by([[:_id, :asc]])
399
- @context = Mongoid::Contexts::Mongo.new(@criteria)
400
- @collection.expects(:find_one).with(
401
- {:_type => {'$in' => ['Doctor', 'Person']}},
402
- { :sort => [[:_id, :desc]] }
403
- ).returns(
207
+ @selector = {}
208
+ @options = { :sort => [[:_id, :asc]] }
209
+ @context = Mongoid::Contexts::Mongo.new(@selector, @options, Person)
210
+ @collection.expects(:find_one).with(@selector, { :sort => [[:_id, :desc]] }).returns(
404
211
  { "title" => "Sir", "_type" => "Person" }
405
212
  )
406
213
  end
@@ -416,19 +223,19 @@ describe Mongoid::Contexts::Mongo do
416
223
  describe "#max" do
417
224
 
418
225
  before do
419
- @reduce = Mongoid::Javascript.max.gsub("[field]", "age")
226
+ @reduce = Mongoid::Contexts::Mongo::MAX_REDUCE.gsub("[field]", "age")
420
227
  @collection = mock
421
228
  Person.expects(:collection).returns(@collection)
422
- @criteria = Mongoid::Criteria.new(Person)
423
- @context = Mongoid::Contexts::Mongo.new(@criteria)
229
+ @context = Mongoid::Contexts::Mongo.new({}, {}, Person)
424
230
  end
425
231
 
426
232
  it "calls group on the collection with the aggregate js" do
427
233
  @collection.expects(:group).with(
428
234
  nil,
429
- {:_type => {'$in' => ['Doctor', 'Person']}},
235
+ {},
430
236
  {:max => "start"},
431
- @reduce
237
+ @reduce,
238
+ true
432
239
  ).returns([{"max" => 200.0}])
433
240
  @context.max(:age).should == 200.0
434
241
  end
@@ -438,19 +245,19 @@ describe Mongoid::Contexts::Mongo do
438
245
  describe "#min" do
439
246
 
440
247
  before do
441
- @reduce = Mongoid::Javascript.min.gsub("[field]", "age")
248
+ @reduce = Mongoid::Contexts::Mongo::MIN_REDUCE.gsub("[field]", "age")
442
249
  @collection = mock
443
250
  Person.expects(:collection).returns(@collection)
444
- @criteria = Mongoid::Criteria.new(Person)
445
- @context = Mongoid::Contexts::Mongo.new(@criteria)
251
+ @context = Mongoid::Contexts::Mongo.new({}, {}, Person)
446
252
  end
447
253
 
448
254
  it "calls group on the collection with the aggregate js" do
449
255
  @collection.expects(:group).with(
450
256
  nil,
451
- {:_type => {'$in' => ['Doctor', 'Person']}},
257
+ {},
452
258
  {:min => "start"},
453
- @reduce
259
+ @reduce,
260
+ true
454
261
  ).returns([{"min" => 4.0}])
455
262
  @context.min(:age).should == 4.0
456
263
  end
@@ -462,10 +269,10 @@ describe Mongoid::Contexts::Mongo do
462
269
  context "when documents exist" do
463
270
 
464
271
  before do
272
+ @collection = mock
465
273
  Person.expects(:collection).returns(@collection)
466
- @criteria = Mongoid::Criteria.new(Person)
467
- @context = Mongoid::Contexts::Mongo.new(@criteria)
468
- @collection.expects(:find_one).with({:_type => {'$in' => ['Doctor', 'Person']}}, {}).returns(
274
+ @context = Mongoid::Contexts::Mongo.new({}, {}, Person)
275
+ @collection.expects(:find_one).with({}, {}).returns(
469
276
  { "title"=> "Sir", "_type" => "Person" }
470
277
  )
471
278
  end
@@ -481,9 +288,8 @@ describe Mongoid::Contexts::Mongo do
481
288
  before do
482
289
  @collection = mock
483
290
  Person.expects(:collection).returns(@collection)
484
- @criteria = Mongoid::Criteria.new(Person)
485
- @context = Mongoid::Contexts::Mongo.new(@criteria)
486
- @collection.expects(:find_one).with({:_type => {'$in' => ['Doctor', 'Person']}}, {}).returns(nil)
291
+ @context = Mongoid::Contexts::Mongo.new({}, {}, Person)
292
+ @collection.expects(:find_one).with({}, {}).returns(nil)
487
293
  end
488
294
 
489
295
  it "returns nil" do
@@ -500,7 +306,7 @@ describe Mongoid::Contexts::Mongo do
500
306
 
501
307
  before do
502
308
  @criteria = Mongoid::Criteria.new(Person).extras({ :page => 5 })
503
- @context = Mongoid::Contexts::Mongo.new(@criteria)
309
+ @context = Mongoid::Contexts::Mongo.new({}, @criteria.options, Person)
504
310
  end
505
311
 
506
312
  it "returns the page option" do
@@ -513,7 +319,7 @@ describe Mongoid::Contexts::Mongo do
513
319
 
514
320
  before do
515
321
  @criteria = Mongoid::Criteria.new(Person)
516
- @context = Mongoid::Contexts::Mongo.new(@criteria)
322
+ @context = Mongoid::Contexts::Mongo.new({}, @criteria.options, Person)
517
323
  end
518
324
 
519
325
  it "returns 1" do
@@ -530,7 +336,7 @@ describe Mongoid::Contexts::Mongo do
530
336
  @collection = mock
531
337
  Person.expects(:collection).returns(@collection)
532
338
  @criteria = Person.where(:_id => "1").skip(60).limit(20)
533
- @context = Mongoid::Contexts::Mongo.new(@criteria)
339
+ @context = Mongoid::Contexts::Mongo.new(@criteria.selector, @criteria.options, Person)
534
340
  @collection.expects(:find).with(
535
341
  {:_type => { "$in" => ["Doctor", "Person"] }, :_id => "1"}, :skip => 60, :limit => 20
536
342
  ).returns([])
@@ -549,19 +355,19 @@ describe Mongoid::Contexts::Mongo do
549
355
  context "when klass not provided" do
550
356
 
551
357
  before do
552
- @reduce = Mongoid::Javascript.sum.gsub("[field]", "age")
358
+ @reduce = Mongoid::Contexts::Mongo::SUM_REDUCE.gsub("[field]", "age")
553
359
  @collection = mock
554
- @criteria = Mongoid::Criteria.new(Person)
555
- @context = Mongoid::Contexts::Mongo.new(@criteria)
360
+ @context = Mongoid::Contexts::Mongo.new({}, {}, Person)
556
361
  Person.expects(:collection).returns(@collection)
557
362
  end
558
363
 
559
364
  it "calls group on the collection with the aggregate js" do
560
365
  @collection.expects(:group).with(
561
366
  nil,
562
- {:_type => {'$in' => ['Doctor', 'Person']}},
367
+ {},
563
368
  {:sum => "start"},
564
- @reduce
369
+ @reduce,
370
+ true
565
371
  ).returns([{"sum" => 50.0}])
566
372
  @context.sum(:age).should == 50.0
567
373
  end
@@ -570,113 +376,4 @@ describe Mongoid::Contexts::Mongo do
570
376
 
571
377
  end
572
378
 
573
- context "#id_criteria" do
574
-
575
- let(:criteria) { Mongoid::Criteria.new(Person) }
576
- let(:context) { criteria.context }
577
-
578
- context "with a single argument" do
579
-
580
- let(:id) { Mongo::ObjectID.new.to_s }
581
-
582
- before do
583
- criteria.expects(:id).with(id).returns(criteria)
584
- end
585
-
586
- context "when the document is found" do
587
-
588
- let(:document) { stub }
589
-
590
- it "returns a matching document" do
591
- context.expects(:one).returns(document)
592
- document.expects(:blank? => false)
593
- context.id_criteria(id).should == document
594
- end
595
-
596
- end
597
-
598
- context "when the document is not found" do
599
-
600
- it "raises an error" do
601
- context.expects(:one).returns(nil)
602
- lambda { context.id_criteria(id) }.should raise_error
603
- end
604
-
605
- end
606
-
607
- end
608
-
609
- context "multiple arguments" do
610
-
611
- context "when an array of ids" do
612
-
613
- let(:ids) do
614
- (0..2).inject([]) { |ary, i| ary << Mongo::ObjectID.new.to_s }
615
- end
616
-
617
- context "when documents are found" do
618
-
619
- let(:docs) do
620
- (0..2).inject([]) { |ary, i| ary << stub }
621
- end
622
-
623
- before do
624
- criteria.expects(:id).with(ids).returns(criteria)
625
- end
626
-
627
- it "returns matching documents" do
628
- context.expects(:execute).returns(docs)
629
- context.id_criteria(ids).should == docs
630
- end
631
-
632
- end
633
-
634
- context "when documents are not found" do
635
-
636
- it "raises an error" do
637
- context.expects(:execute).returns([])
638
- lambda { context.id_criteria(ids) }.should raise_error
639
- end
640
-
641
- end
642
-
643
- end
644
-
645
- context "when an array of object ids" do
646
-
647
- let(:ids) do
648
- (0..2).inject([]) { |ary, i| ary << Mongo::ObjectID.new }
649
- end
650
-
651
- context "when documents are found" do
652
-
653
- let(:docs) do
654
- (0..2).inject([]) { |ary, i| ary << stub }
655
- end
656
-
657
- before do
658
- criteria.expects(:id).with(ids).returns(criteria)
659
- end
660
-
661
- it "returns matching documents" do
662
- context.expects(:execute).returns(docs)
663
- context.id_criteria(ids).should == docs
664
- end
665
-
666
- end
667
-
668
- context "when documents are not found" do
669
-
670
- it "raises an error" do
671
- context.expects(:execute).returns([])
672
- lambda { context.id_criteria(ids) }.should raise_error
673
- end
674
-
675
- end
676
-
677
- end
678
- end
679
-
680
- end
681
-
682
379
  end