mongoid 4.0.0.alpha1 → 4.0.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (100) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/README.md +1 -1
  4. data/lib/config/locales/en.yml +4 -6
  5. data/lib/mongoid/attributes/processing.rb +1 -1
  6. data/lib/mongoid/contextual.rb +2 -0
  7. data/lib/mongoid/contextual/map_reduce.rb +3 -1
  8. data/lib/mongoid/contextual/none.rb +90 -0
  9. data/lib/mongoid/criteria.rb +25 -0
  10. data/lib/mongoid/extensions/array.rb +1 -1
  11. data/lib/mongoid/extensions/big_decimal.rb +1 -1
  12. data/lib/mongoid/extensions/date.rb +1 -1
  13. data/lib/mongoid/extensions/date_time.rb +3 -3
  14. data/lib/mongoid/extensions/float.rb +1 -1
  15. data/lib/mongoid/extensions/hash.rb +1 -1
  16. data/lib/mongoid/extensions/integer.rb +1 -1
  17. data/lib/mongoid/extensions/object.rb +3 -3
  18. data/lib/mongoid/extensions/object_id.rb +1 -1
  19. data/lib/mongoid/extensions/range.rb +1 -1
  20. data/lib/mongoid/extensions/regexp.rb +1 -1
  21. data/lib/mongoid/extensions/set.rb +1 -1
  22. data/lib/mongoid/extensions/string.rb +1 -1
  23. data/lib/mongoid/extensions/symbol.rb +1 -1
  24. data/lib/mongoid/extensions/time.rb +1 -1
  25. data/lib/mongoid/extensions/time_with_zone.rb +1 -1
  26. data/lib/mongoid/findable.rb +1 -0
  27. data/lib/mongoid/interceptable.rb +4 -2
  28. data/lib/mongoid/persistable.rb +4 -3
  29. data/lib/mongoid/relations/embedded/many.rb +1 -1
  30. data/lib/mongoid/relations/metadata.rb +8 -4
  31. data/lib/mongoid/relations/referenced/many.rb +1 -1
  32. data/lib/mongoid/relations/touchable.rb +1 -1
  33. data/lib/mongoid/scopable.rb +2 -26
  34. data/lib/mongoid/validatable/uniqueness.rb +13 -0
  35. data/lib/mongoid/version.rb +1 -1
  36. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +0 -4
  37. data/spec/app/models/acolyte.rb +1 -1
  38. data/spec/app/models/address.rb +2 -2
  39. data/spec/app/models/appointment.rb +1 -1
  40. data/spec/app/models/audio.rb +1 -1
  41. data/spec/app/models/dog.rb +1 -1
  42. data/spec/app/models/event.rb +1 -1
  43. data/spec/app/models/house.rb +1 -1
  44. data/spec/app/models/message.rb +8 -0
  45. data/spec/app/models/person.rb +3 -2
  46. data/spec/app/models/player.rb +2 -2
  47. data/spec/app/models/post.rb +3 -2
  48. data/spec/app/models/preference.rb +1 -1
  49. data/spec/app/models/sound.rb +1 -1
  50. data/spec/app/models/symptom.rb +1 -1
  51. data/spec/app/models/tree.rb +2 -2
  52. data/spec/app/models/video.rb +1 -1
  53. data/spec/mongoid/attributes_spec.rb +3 -3
  54. data/spec/mongoid/changeable_spec.rb +3 -3
  55. data/spec/mongoid/contextual/mongo_spec.rb +14 -14
  56. data/spec/mongoid/contextual/none_spec.rb +127 -0
  57. data/spec/mongoid/criteria/scopable_spec.rb +6 -6
  58. data/spec/mongoid/criteria_spec.rb +52 -96
  59. data/spec/mongoid/errors/invalid_scope_spec.rb +2 -2
  60. data/spec/mongoid/errors/mongoid_error_spec.rb +1 -1
  61. data/spec/mongoid/extensions/date_time_spec.rb +1 -1
  62. data/spec/mongoid/fields_spec.rb +6 -7
  63. data/spec/mongoid/findable_spec.rb +65 -0
  64. data/spec/mongoid/interceptable_spec.rb +11 -11
  65. data/spec/mongoid/persistable_spec.rb +4 -4
  66. data/spec/mongoid/relations/auto_save_spec.rb +1 -1
  67. data/spec/mongoid/relations/bindings/embedded/in_spec.rb +5 -5
  68. data/spec/mongoid/relations/bindings/embedded/many_spec.rb +1 -1
  69. data/spec/mongoid/relations/bindings/embedded/one_spec.rb +2 -2
  70. data/spec/mongoid/relations/bindings/referenced/in_spec.rb +14 -14
  71. data/spec/mongoid/relations/bindings/referenced/many_spec.rb +4 -4
  72. data/spec/mongoid/relations/bindings/referenced/many_to_many_spec.rb +10 -10
  73. data/spec/mongoid/relations/bindings/referenced/one_spec.rb +6 -6
  74. data/spec/mongoid/relations/builders/referenced/in_spec.rb +2 -2
  75. data/spec/mongoid/relations/builders/referenced/one_spec.rb +1 -1
  76. data/spec/mongoid/relations/cascading/delete_spec.rb +2 -2
  77. data/spec/mongoid/relations/cascading/destroy_spec.rb +4 -4
  78. data/spec/mongoid/relations/cascading/nullify_spec.rb +2 -2
  79. data/spec/mongoid/relations/cascading/restrict_spec.rb +4 -4
  80. data/spec/mongoid/relations/eager_spec.rb +6 -6
  81. data/spec/mongoid/relations/embedded/many_spec.rb +36 -5
  82. data/spec/mongoid/relations/metadata_spec.rb +34 -15
  83. data/spec/mongoid/relations/referenced/many_spec.rb +19 -4
  84. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +7 -7
  85. data/spec/mongoid/relations/referenced/one_spec.rb +1 -1
  86. data/spec/mongoid/relations/targets/enumerable_spec.rb +2 -2
  87. data/spec/mongoid/relations/touchable_spec.rb +17 -0
  88. data/spec/mongoid/scopable_spec.rb +17 -40
  89. data/spec/mongoid/sessions/options_spec.rb +1 -1
  90. data/spec/mongoid/timestamps/timeless_spec.rb +6 -6
  91. data/spec/mongoid/timestamps/updated/short_spec.rb +1 -1
  92. data/spec/mongoid/timestamps/updated_spec.rb +1 -1
  93. data/spec/mongoid/timestamps_spec.rb +2 -2
  94. data/spec/mongoid/validatable/associated_spec.rb +4 -4
  95. data/spec/mongoid/validatable/length_spec.rb +7 -7
  96. data/spec/mongoid/validatable/presence_spec.rb +6 -6
  97. data/spec/mongoid/validatable/uniqueness_spec.rb +44 -3
  98. data/spec/rails/mongoid_spec.rb +20 -20
  99. data/spec/spec_helper.rb +1 -0
  100. metadata +55 -50
@@ -8,7 +8,7 @@ describe Mongoid::Errors::MongoidError do
8
8
 
9
9
  before do
10
10
  ["message", "summary", "resolution"].each do |name|
11
- ::I18n.should_receive(:translate).
11
+ expect(::I18n).to receive(:translate).
12
12
  with("mongoid.errors.messages.#{key}.#{name}", {}).
13
13
  and_return(name)
14
14
  end
@@ -14,7 +14,7 @@ describe Mongoid::Extensions::DateTime do
14
14
  date_time.__mongoize_time__
15
15
  end
16
16
 
17
- pending "does not drop the precision" do
17
+ it "does not drop the precision" do
18
18
  expect(mongoized.to_f.to_s).to match(/\.123/)
19
19
  end
20
20
  end
@@ -61,7 +61,7 @@ describe Mongoid::Fields do
61
61
  end
62
62
 
63
63
  it "should have alias method #\{field}_t" do
64
- product.method(:name_t).should eq product.method(:name_translations)
64
+ expect(product.method(:name_t)).to eq product.method(:name_translations)
65
65
  end
66
66
  end
67
67
 
@@ -72,7 +72,7 @@ describe Mongoid::Fields do
72
72
  end
73
73
 
74
74
  it "does not respond to the alias method" do
75
- product.should_not respond_to(:price_t)
75
+ expect(product).to_not respond_to(:price_t)
76
76
  end
77
77
  end
78
78
  end
@@ -174,7 +174,7 @@ describe Mongoid::Fields do
174
174
  end
175
175
 
176
176
  it "should have alias method #\{field}_t=" do
177
- product.method(:name_t=).should eq product.method(:name_translations=)
177
+ expect(product.method(:name_t=)).to eq product.method(:name_translations=)
178
178
  end
179
179
  end
180
180
 
@@ -185,7 +185,7 @@ describe Mongoid::Fields do
185
185
  end
186
186
 
187
187
  it "does not respond to the alias method" do
188
- product.should_not respond_to(:price_t=)
188
+ expect(product).to_not respond_to(:price_t=)
189
189
  end
190
190
  end
191
191
  end
@@ -956,7 +956,7 @@ describe Mongoid::Fields do
956
956
  context "when option is nil" do
957
957
 
958
958
  it "calls the handler" do
959
- handler.should_receive(:call)
959
+ expect(handler).to receive(:call)
960
960
  User.field :custom, option: nil, overwrite: true
961
961
  end
962
962
  end
@@ -964,8 +964,7 @@ describe Mongoid::Fields do
964
964
  context "when option is not provided" do
965
965
 
966
966
  it "does not call the handler" do
967
- handler.should_receive(:call).never
968
-
967
+ expect(handler).to receive(:call).never
969
968
  User.field :custom, overwrite: true
970
969
  end
971
970
  end
@@ -286,6 +286,71 @@ describe Mongoid::Findable do
286
286
  end
287
287
  end
288
288
 
289
+ describe ".none" do
290
+
291
+ let!(:depeche) do
292
+ Band.create(name: "Depeche Mode", likes: 3)
293
+ end
294
+
295
+ context "when not chaining any criteria" do
296
+
297
+ it "returns no records" do
298
+ expect(Band.none).to be_empty
299
+ end
300
+
301
+ it "has an empty count" do
302
+ expect(Band.none.count).to eq(0)
303
+ end
304
+
305
+ it "returns nil for first" do
306
+ expect(Band.none.first).to be_nil
307
+ end
308
+
309
+ it "returns nil for last" do
310
+ expect(Band.none.last).to be_nil
311
+ end
312
+
313
+ it "returns zero for length" do
314
+ expect(Band.none.length).to eq(0)
315
+ end
316
+
317
+ it "returns zero for size" do
318
+ expect(Band.none.size).to eq(0)
319
+ end
320
+ end
321
+
322
+ context "when chaining criteria after the none" do
323
+
324
+ let(:criteria) do
325
+ Band.none.where(name: "Depeche Mode")
326
+ end
327
+
328
+ it "returns no records" do
329
+ expect(criteria).to be_empty
330
+ end
331
+
332
+ it "has an empty count" do
333
+ expect(criteria.count).to eq(0)
334
+ end
335
+
336
+ it "returns nil for first" do
337
+ expect(criteria.first).to be_nil
338
+ end
339
+
340
+ it "returns nil for last" do
341
+ expect(criteria.last).to be_nil
342
+ end
343
+
344
+ it "returns zero for length" do
345
+ expect(criteria.length).to eq(0)
346
+ end
347
+
348
+ it "returns zero for size" do
349
+ expect(criteria.size).to eq(0)
350
+ end
351
+ end
352
+ end
353
+
289
354
  describe ".pluck" do
290
355
 
291
356
  let!(:depeche) do
@@ -216,7 +216,7 @@ describe Mongoid::Interceptable do
216
216
  context "callback returns true" do
217
217
 
218
218
  before do
219
- artist.should_receive(:before_create_stub).once.and_return(true)
219
+ expect(artist).to receive(:before_create_stub).once.and_return(true)
220
220
  artist.save
221
221
  end
222
222
 
@@ -228,7 +228,7 @@ describe Mongoid::Interceptable do
228
228
  context "callback returns false" do
229
229
 
230
230
  before do
231
- artist.should_receive(:before_create_stub).once.and_return(false)
231
+ expect(artist).to receive(:before_create_stub).once.and_return(false)
232
232
  artist.save
233
233
  end
234
234
 
@@ -253,7 +253,7 @@ describe Mongoid::Interceptable do
253
253
  context "when the callback returns true" do
254
254
 
255
255
  before do
256
- artist.should_receive(:before_save_stub).once.and_return(true)
256
+ expect(artist).to receive(:before_save_stub).once.and_return(true)
257
257
  end
258
258
 
259
259
  it "the save returns true" do
@@ -264,7 +264,7 @@ describe Mongoid::Interceptable do
264
264
  context "when callback returns false" do
265
265
 
266
266
  before do
267
- artist.should_receive(:before_save_stub).once.and_return(false)
267
+ expect(artist).to receive(:before_save_stub).once.and_return(false)
268
268
  end
269
269
 
270
270
  it "the save returns false" do
@@ -288,7 +288,7 @@ describe Mongoid::Interceptable do
288
288
  context "when the callback returns true" do
289
289
 
290
290
  before do
291
- artist.should_receive(:before_save_stub).once.and_return(true)
291
+ expect(artist).to receive(:before_save_stub).once.and_return(true)
292
292
  end
293
293
 
294
294
  it "the save returns true" do
@@ -299,7 +299,7 @@ describe Mongoid::Interceptable do
299
299
  context "when the callback returns false" do
300
300
 
301
301
  before do
302
- artist.should_receive(:before_save_stub).once.and_return(false)
302
+ expect(artist).to receive(:before_save_stub).once.and_return(false)
303
303
  end
304
304
 
305
305
  it "the save returns false" do
@@ -326,7 +326,7 @@ describe Mongoid::Interceptable do
326
326
  context "when the callback returns true" do
327
327
 
328
328
  before do
329
- artist.should_receive(:before_destroy_stub).once.and_return(true)
329
+ expect(artist).to receive(:before_destroy_stub).once.and_return(true)
330
330
  end
331
331
 
332
332
  it "the destroy returns true" do
@@ -337,7 +337,7 @@ describe Mongoid::Interceptable do
337
337
  context "when the callback returns false" do
338
338
 
339
339
  before do
340
- artist.should_receive(:before_destroy_stub).once.and_return(false)
340
+ expect(artist).to receive(:before_destroy_stub).once.and_return(false)
341
341
  end
342
342
 
343
343
  it "the destroy returns false" do
@@ -485,7 +485,7 @@ describe Mongoid::Interceptable do
485
485
  context "when saving the root" do
486
486
 
487
487
  it "only executes the callbacks once for each embed" do
488
- note.should_receive(:update_saved).twice
488
+ expect(note).to receive(:update_saved).twice
489
489
  band.save
490
490
  end
491
491
  end
@@ -503,7 +503,7 @@ describe Mongoid::Interceptable do
503
503
  end
504
504
 
505
505
  it "doesn't cascade the initialize" do
506
- Service.any_instance.should_receive(:after_initialize_called=).never
506
+ expect_any_instance_of(Service).to receive(:after_initialize_called=).never
507
507
  expect(Person.find(person.id)).to eq(person)
508
508
  end
509
509
  end
@@ -1556,7 +1556,7 @@ describe Mongoid::Interceptable do
1556
1556
  context "when saving the document" do
1557
1557
 
1558
1558
  it "only executes the callbacks once" do
1559
- callback.should_receive(:execute).once
1559
+ expect(callback).to receive(:execute).once
1560
1560
  callback.save
1561
1561
  end
1562
1562
  end
@@ -61,7 +61,7 @@ describe Mongoid::Persistable do
61
61
  end
62
62
 
63
63
  before do
64
- Moped::Query.any_instance.should_receive(:update).with(operations).and_call_original
64
+ expect_any_instance_of(Moped::Query).to receive(:update).with(operations).and_call_original
65
65
  end
66
66
 
67
67
  let!(:update) do
@@ -88,7 +88,7 @@ describe Mongoid::Persistable do
88
88
  end
89
89
 
90
90
  before do
91
- Moped::Query.any_instance.should_receive(:update).with(operations).and_call_original
91
+ expect_any_instance_of(Moped::Query).to receive(:update).with(operations).and_call_original
92
92
  end
93
93
 
94
94
  let!(:update) do
@@ -116,7 +116,7 @@ describe Mongoid::Persistable do
116
116
  end
117
117
 
118
118
  before do
119
- Moped::Query.any_instance.should_receive(:update).with(operations).and_call_original
119
+ expect_any_instance_of(Moped::Query).to receive(:update).with(operations).and_call_original
120
120
  end
121
121
 
122
122
  let!(:update) do
@@ -153,7 +153,7 @@ describe Mongoid::Persistable do
153
153
  end
154
154
 
155
155
  before do
156
- Moped::Query.any_instance.should_receive(:update).with(operations).and_call_original
156
+ expect_any_instance_of(Moped::Query).to receive(:update).with(operations).and_call_original
157
157
  end
158
158
 
159
159
  let!(:update) do
@@ -56,7 +56,7 @@ describe Mongoid::Relations::AutoSave do
56
56
  end
57
57
 
58
58
  it "does not add the autosave callback twice" do
59
- drug.should_receive(:save).once
59
+ expect(drug).to receive(:save).once
60
60
  person.drugs.push(drug)
61
61
  person.save
62
62
  end
@@ -56,7 +56,7 @@ describe Mongoid::Relations::Bindings::Embedded::In do
56
56
  end
57
57
 
58
58
  it "does nothing" do
59
- name.should_receive(:namable=).never
59
+ expect(name).to receive(:namable=).never
60
60
  binding.bind_one
61
61
  end
62
62
  end
@@ -92,7 +92,7 @@ describe Mongoid::Relations::Bindings::Embedded::In do
92
92
  end
93
93
 
94
94
  it "does not overwrite the existing metadata" do
95
- address.should_receive(:metadata=).never
95
+ expect(address).to receive(:metadata=).never
96
96
  binding.bind_one
97
97
  end
98
98
  end
@@ -105,7 +105,7 @@ describe Mongoid::Relations::Bindings::Embedded::In do
105
105
  end
106
106
 
107
107
  it "does nothing" do
108
- address.should_receive(:addressable=).never
108
+ expect(address).to receive(:addressable=).never
109
109
  binding.bind_one
110
110
  end
111
111
  end
@@ -135,7 +135,7 @@ describe Mongoid::Relations::Bindings::Embedded::In do
135
135
  context "when the document is not unbindable" do
136
136
 
137
137
  it "does nothing" do
138
- name.should_receive(:namable=).never
138
+ expect(name).to receive(:namable=).never
139
139
  binding.unbind_one
140
140
  end
141
141
  end
@@ -162,7 +162,7 @@ describe Mongoid::Relations::Bindings::Embedded::In do
162
162
  context "when the document is not unbindable" do
163
163
 
164
164
  it "does nothing" do
165
- address.should_receive(:addressable=).never
165
+ expect(address).to receive(:addressable=).never
166
166
  binding.unbind_one
167
167
  end
168
168
  end
@@ -46,7 +46,7 @@ describe Mongoid::Relations::Bindings::Embedded::Many do
46
46
  context "when the document is not bindable" do
47
47
 
48
48
  it "does nothing" do
49
- person.addresses.should_receive(:<<).never
49
+ expect(person.addresses).to receive(:<<).never
50
50
  binding.bind_one(address)
51
51
  end
52
52
  end
@@ -42,7 +42,7 @@ describe Mongoid::Relations::Bindings::Embedded::One do
42
42
  end
43
43
 
44
44
  it "does nothing" do
45
- person.should_receive(:name=).never
45
+ expect(person).to receive(:name=).never
46
46
  binding.bind_one
47
47
  end
48
48
  end
@@ -69,7 +69,7 @@ describe Mongoid::Relations::Bindings::Embedded::One do
69
69
  context "when the document is not unbindable" do
70
70
 
71
71
  it "does nothing" do
72
- person.should_receive(:name=).never
72
+ expect(person).to receive(:name=).never
73
73
  binding.unbind_one
74
74
  end
75
75
  end
@@ -33,8 +33,8 @@ describe Mongoid::Relations::Bindings::Referenced::In do
33
33
  context "when the document is bindable with default pk" do
34
34
 
35
35
  before do
36
- person.should_receive(:save).never
37
- game.should_receive(:save).never
36
+ expect(person).to receive(:save).never
37
+ expect(game).to receive(:save).never
38
38
  binding.bind_one
39
39
  end
40
40
 
@@ -52,8 +52,8 @@ describe Mongoid::Relations::Bindings::Referenced::In do
52
52
  before do
53
53
  Game.belongs_to :person, index: true, validate: true, primary_key: :username
54
54
 
55
- person.should_receive(:save).never
56
- game.should_receive(:save).never
55
+ expect(person).to receive(:save).never
56
+ expect(game).to receive(:save).never
57
57
  binding.bind_one
58
58
  end
59
59
 
@@ -81,7 +81,7 @@ describe Mongoid::Relations::Bindings::Referenced::In do
81
81
  end
82
82
 
83
83
  it "does nothing" do
84
- game.should_receive(:person=).never
84
+ expect(game).to receive(:person=).never
85
85
  binding.bind_one
86
86
  end
87
87
  end
@@ -96,8 +96,8 @@ describe Mongoid::Relations::Bindings::Referenced::In do
96
96
  context "when the document is bindable" do
97
97
 
98
98
  before do
99
- person.should_receive(:save).never
100
- post.should_receive(:save).never
99
+ expect(person).to receive(:save).never
100
+ expect(post).to receive(:save).never
101
101
  binding.bind_one
102
102
  end
103
103
 
@@ -117,7 +117,7 @@ describe Mongoid::Relations::Bindings::Referenced::In do
117
117
  end
118
118
 
119
119
  it "does nothing" do
120
- post.should_receive(:person=).never
120
+ expect(post).to receive(:person=).never
121
121
  binding.bind_one
122
122
  end
123
123
  end
@@ -136,8 +136,8 @@ describe Mongoid::Relations::Bindings::Referenced::In do
136
136
 
137
137
  before do
138
138
  binding.bind_one
139
- person.should_receive(:delete).never
140
- game.should_receive(:delete).never
139
+ expect(person).to receive(:delete).never
140
+ expect(game).to receive(:delete).never
141
141
  binding.unbind_one
142
142
  end
143
143
 
@@ -153,7 +153,7 @@ describe Mongoid::Relations::Bindings::Referenced::In do
153
153
  context "when the document is not unbindable" do
154
154
 
155
155
  it "does nothing" do
156
- game.should_receive(:person=).never
156
+ expect(game).to receive(:person=).never
157
157
  binding.unbind_one
158
158
  end
159
159
  end
@@ -169,8 +169,8 @@ describe Mongoid::Relations::Bindings::Referenced::In do
169
169
 
170
170
  before do
171
171
  binding.bind_one
172
- person.should_receive(:delete).never
173
- post.should_receive(:delete).never
172
+ expect(person).to receive(:delete).never
173
+ expect(post).to receive(:delete).never
174
174
  binding.unbind_one
175
175
  end
176
176
 
@@ -186,7 +186,7 @@ describe Mongoid::Relations::Bindings::Referenced::In do
186
186
  context "when the document is not unbindable" do
187
187
 
188
188
  it "does nothing" do
189
- post.should_receive(:person=).never
189
+ expect(post).to receive(:person=).never
190
190
  binding.unbind_one
191
191
  end
192
192
  end
@@ -46,7 +46,7 @@ describe Mongoid::Relations::Bindings::Referenced::Many do
46
46
  context "when the document is not bindable" do
47
47
 
48
48
  it "does nothing" do
49
- person.posts.should_receive(:<<).never
49
+ expect(person.posts).to receive(:<<).never
50
50
  binding.bind_one(post)
51
51
  end
52
52
  end
@@ -62,8 +62,8 @@ describe Mongoid::Relations::Bindings::Referenced::Many do
62
62
 
63
63
  before do
64
64
  binding.bind_one(target.first)
65
- person.should_receive(:delete).never
66
- post.should_receive(:delete).never
65
+ expect(person).to receive(:delete).never
66
+ expect(post).to receive(:delete).never
67
67
  binding.unbind_one(target.first)
68
68
  end
69
69
 
@@ -79,7 +79,7 @@ describe Mongoid::Relations::Bindings::Referenced::Many do
79
79
  context "when the documents are not unbindable" do
80
80
 
81
81
  it "does nothing" do
82
- person.should_receive(:posts=).never
82
+ expect(person).to receive(:posts=).never
83
83
  binding.unbind_one(target.first)
84
84
  end
85
85
  end