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
@@ -0,0 +1,127 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Contextual::None do
4
+
5
+ describe "#==" do
6
+
7
+ let!(:band) do
8
+ Band.create(name: "Depeche Mode")
9
+ end
10
+
11
+ let(:context) do
12
+ described_class.new(Band.where(name: "Depeche Mode"))
13
+ end
14
+
15
+ context "when the other is a none" do
16
+
17
+ let(:other) do
18
+ described_class.new(Band.where(name: "Depeche Mode"))
19
+ end
20
+
21
+ it "returns true" do
22
+ expect(context).to eq(other)
23
+ end
24
+ end
25
+
26
+ context "when the other is not a none" do
27
+
28
+ let(:other) do
29
+ Mongoid::Contextual::Memory.new(Band.where(name: "Depeche Mode"))
30
+ end
31
+
32
+ it "returns false" do
33
+ expect(context).to_not eq(other)
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "#each" do
39
+
40
+ let!(:band) do
41
+ Band.create(name: "Depeche Mode")
42
+ end
43
+
44
+ let(:context) do
45
+ described_class.new(Band.where(name: "Depeche Mode"))
46
+ end
47
+
48
+ it "iterates over no documents" do
49
+ expect(context.each.entries).to be_empty
50
+ end
51
+ end
52
+
53
+ describe "#exists?" do
54
+
55
+ let!(:band) do
56
+ Band.create(name: "Depeche Mode")
57
+ end
58
+
59
+ let(:context) do
60
+ described_class.new(Band.where(name: "Depeche Mode"))
61
+ end
62
+
63
+ it "returns false" do
64
+ expect(context).to_not be_exists
65
+ end
66
+ end
67
+
68
+ describe "#first" do
69
+
70
+ let!(:band) do
71
+ Band.create(name: "Depeche Mode")
72
+ end
73
+
74
+ let(:context) do
75
+ described_class.new(Band.where(name: "Depeche Mode"))
76
+ end
77
+
78
+ it "returns nil" do
79
+ expect(context.first).to be_nil
80
+ end
81
+ end
82
+
83
+ describe "#last" do
84
+
85
+ let!(:band) do
86
+ Band.create(name: "Depeche Mode")
87
+ end
88
+
89
+ let(:context) do
90
+ described_class.new(Band.where(name: "Depeche Mode"))
91
+ end
92
+
93
+ it "returns nil" do
94
+ expect(context.last).to be_nil
95
+ end
96
+ end
97
+
98
+ describe "#length" do
99
+
100
+ let!(:band) do
101
+ Band.create(name: "Depeche Mode")
102
+ end
103
+
104
+ let(:context) do
105
+ described_class.new(Band.where(name: "Depeche Mode"))
106
+ end
107
+
108
+ it "returns zero" do
109
+ expect(context.length).to eq(0)
110
+ end
111
+ end
112
+
113
+ describe "#size" do
114
+
115
+ let!(:band) do
116
+ Band.create(name: "Depeche Mode")
117
+ end
118
+
119
+ let(:context) do
120
+ described_class.new(Band.where(name: "Depeche Mode"))
121
+ end
122
+
123
+ it "returns zero" do
124
+ expect(context.size).to eq(0)
125
+ end
126
+ end
127
+ end
@@ -11,7 +11,7 @@ describe Mongoid::Criteria::Scopable do
11
11
  end
12
12
 
13
13
  before do
14
- Band.default_scope scope
14
+ Band.default_scope ->{ scope }
15
15
  end
16
16
 
17
17
  after do
@@ -40,7 +40,7 @@ describe Mongoid::Criteria::Scopable do
40
40
  end
41
41
 
42
42
  before do
43
- Band.default_scope scope
43
+ Band.default_scope ->{ scope }
44
44
  end
45
45
 
46
46
  after do
@@ -69,7 +69,7 @@ describe Mongoid::Criteria::Scopable do
69
69
  end
70
70
 
71
71
  before do
72
- Band.default_scope scope
72
+ Band.default_scope ->{ scope }
73
73
  end
74
74
 
75
75
  after do
@@ -241,7 +241,7 @@ describe Mongoid::Criteria::Scopable do
241
241
  end
242
242
 
243
243
  before do
244
- Band.default_scope criteria
244
+ Band.default_scope ->{ criteria }
245
245
  end
246
246
 
247
247
  after do
@@ -318,7 +318,7 @@ describe Mongoid::Criteria::Scopable do
318
318
  end
319
319
 
320
320
  before do
321
- Band.default_scope criteria
321
+ Band.default_scope ->{ criteria }
322
322
  end
323
323
 
324
324
  after do
@@ -370,7 +370,7 @@ describe Mongoid::Criteria::Scopable do
370
370
  context "when a named scope is called in the block" do
371
371
 
372
372
  before do
373
- Band.scope(:skipped, Band.skip(10))
373
+ Band.scope(:skipped, ->{ Band.skip(10) })
374
374
  end
375
375
 
376
376
  after do
@@ -362,7 +362,7 @@ describe Mongoid::Criteria do
362
362
  end
363
363
 
364
364
  it "does not hit the database after first iteration" do
365
- criteria.context.query.should_receive(:each).never
365
+ expect(criteria.context.query).to receive(:each).never
366
366
  criteria.each do |doc|
367
367
  expect(doc).to eq(person)
368
368
  end
@@ -380,7 +380,7 @@ describe Mongoid::Criteria do
380
380
  end
381
381
 
382
382
  it "does not hit the database after first iteration" do
383
- criteria.context.query.should_receive(:each).never
383
+ expect(criteria.context.query).to receive(:each).never
384
384
  criteria.each do |doc|
385
385
  expect(doc).to eq(person)
386
386
  end
@@ -1571,7 +1571,7 @@ describe Mongoid::Criteria do
1571
1571
  context "when providing inclusions to the default scope" do
1572
1572
 
1573
1573
  before do
1574
- Person.default_scope(Person.includes(:posts))
1574
+ Person.default_scope(->{ Person.includes(:posts) })
1575
1575
  end
1576
1576
 
1577
1577
  after do
@@ -1623,7 +1623,7 @@ describe Mongoid::Criteria do
1623
1623
  end
1624
1624
 
1625
1625
  before do
1626
- new_context.should_receive(:eager_load_one).with(person).once.and_call_original
1626
+ expect(new_context).to receive(:eager_load_one).with(person).once.and_call_original
1627
1627
  end
1628
1628
 
1629
1629
  let!(:from_db) do
@@ -1674,7 +1674,7 @@ describe Mongoid::Criteria do
1674
1674
  end
1675
1675
 
1676
1676
  before do
1677
- context.should_receive(:eager_load_one).with(person).once.and_call_original
1677
+ expect(context).to receive(:eager_load_one).with(person).once.and_call_original
1678
1678
  end
1679
1679
 
1680
1680
  let!(:from_db) do
@@ -1934,7 +1934,7 @@ describe Mongoid::Criteria do
1934
1934
  end
1935
1935
 
1936
1936
  before do
1937
- context.should_receive(:eager_load).with([ person ]).once.and_call_original
1937
+ expect(context).to receive(:eager_load).with([ person ]).once.and_call_original
1938
1938
  end
1939
1939
 
1940
1940
  let!(:documents) do
@@ -1968,7 +1968,7 @@ describe Mongoid::Criteria do
1968
1968
  end
1969
1969
 
1970
1970
  before do
1971
- context.should_receive(:eager_load).with([ person ]).once.and_call_original
1971
+ expect(context).to receive(:eager_load).with([ person ]).once.and_call_original
1972
1972
  end
1973
1973
 
1974
1974
  let!(:documents) do
@@ -1999,7 +1999,7 @@ describe Mongoid::Criteria do
1999
1999
  end
2000
2000
 
2001
2001
  before do
2002
- context.should_receive(:eager_load).with([ person ]).once.and_call_original
2002
+ expect(context).to receive(:eager_load).with([ person ]).once.and_call_original
2003
2003
  end
2004
2004
 
2005
2005
  let!(:documents) do
@@ -2037,11 +2037,7 @@ describe Mongoid::Criteria do
2037
2037
  end
2038
2038
 
2039
2039
  before do
2040
- context.
2041
- should_receive(:eager_load).
2042
- with([ game_one, game_two ]).
2043
- once.
2044
- and_call_original
2040
+ expect(context).to receive(:eager_load).with([ game_one, game_two ]).once.and_call_original
2045
2041
  end
2046
2042
 
2047
2043
  let!(:documents) do
@@ -2064,7 +2060,7 @@ describe Mongoid::Criteria do
2064
2060
  end
2065
2061
 
2066
2062
  before do
2067
- context.should_receive(:eager_load).with([ game_one ]).once.and_call_original
2063
+ expect(context).to receive(:eager_load).with([ game_one ]).once.and_call_original
2068
2064
  end
2069
2065
 
2070
2066
  let!(:documents) do
@@ -2104,7 +2100,7 @@ describe Mongoid::Criteria do
2104
2100
  end
2105
2101
 
2106
2102
  before do
2107
- context.should_receive(:eager_load).with([ person ]).once.and_call_original
2103
+ expect(context).to receive(:eager_load).with([ person ]).once.and_call_original
2108
2104
  end
2109
2105
 
2110
2106
  let!(:documents) do
@@ -2217,15 +2213,29 @@ describe Mongoid::Criteria do
2217
2213
  Band.create(name: "Tool", likes: 100)
2218
2214
  end
2219
2215
 
2220
- let(:map_reduce) do
2221
- Band.limit(2).map_reduce(map, reduce).out(inline: 1)
2216
+ context "when no timeout options are provided" do
2217
+
2218
+ let(:map_reduce) do
2219
+ Band.limit(2).map_reduce(map, reduce).out(inline: 1)
2220
+ end
2221
+
2222
+ it "returns the map/reduce results" do
2223
+ expect(map_reduce).to eq([
2224
+ { "_id" => "Depeche Mode", "value" => { "likes" => 200 }},
2225
+ { "_id" => "Tool", "value" => { "likes" => 100 }}
2226
+ ])
2227
+ end
2222
2228
  end
2223
2229
 
2224
- it "returns the map/reduce results" do
2225
- expect(map_reduce).to eq([
2226
- { "_id" => "Depeche Mode", "value" => { "likes" => 200 }},
2227
- { "_id" => "Tool", "value" => { "likes" => 100 }}
2228
- ])
2230
+ context "when timeout options are provided" do
2231
+
2232
+ let(:map_reduce) do
2233
+ Band.limit(2).no_timeout.map_reduce(map, reduce).out(replace: "test_bands")
2234
+ end
2235
+
2236
+ it "sets the timeout option on the query" do
2237
+ expect(map_reduce.send(:documents).operation.flags).to eq([ :no_cursor_timeout ])
2238
+ end
2229
2239
  end
2230
2240
  end
2231
2241
 
@@ -3254,7 +3264,7 @@ describe Mongoid::Criteria do
3254
3264
  context "when the method exists on the class" do
3255
3265
 
3256
3266
  before do
3257
- Person.should_receive(:minor).and_call_original
3267
+ expect(Person).to receive(:minor).and_call_original
3258
3268
  end
3259
3269
 
3260
3270
  it "calls the method on the class" do
@@ -3265,7 +3275,7 @@ describe Mongoid::Criteria do
3265
3275
  context "when the method exists on the criteria" do
3266
3276
 
3267
3277
  before do
3268
- criteria.should_receive(:to_criteria).and_call_original
3278
+ expect(criteria).to receive(:to_criteria).and_call_original
3269
3279
  end
3270
3280
 
3271
3281
  it "calls the method on the criteria" do
@@ -3276,7 +3286,7 @@ describe Mongoid::Criteria do
3276
3286
  context "when the method exists on array" do
3277
3287
 
3278
3288
  before do
3279
- criteria.should_receive(:entries).and_call_original
3289
+ expect(criteria).to receive(:entries).and_call_original
3280
3290
  end
3281
3291
 
3282
3292
  it "calls the method on the criteria" do
@@ -3287,7 +3297,7 @@ describe Mongoid::Criteria do
3287
3297
  context "when the method does not exist" do
3288
3298
 
3289
3299
  before do
3290
- criteria.should_receive(:entries).never
3300
+ expect(criteria).to receive(:entries).never
3291
3301
  end
3292
3302
 
3293
3303
  it "raises an error" do
@@ -3332,81 +3342,27 @@ describe Mongoid::Criteria do
3332
3342
  end
3333
3343
  end
3334
3344
 
3335
- describe "#within_box" do
3336
-
3337
- before do
3338
- Bar.create_indexes
3339
- end
3340
-
3341
- let!(:match) do
3342
- Bar.create(location: [ 52.30, 13.25 ])
3343
- end
3345
+ describe "#geo_spacial" do
3344
3346
 
3345
- let(:criteria) do
3346
- Bar.within_box(location: [[ 50, 10 ], [ 60, 20 ]])
3347
- end
3347
+ context "when checking within a polygon" do
3348
3348
 
3349
- it "returns the matching documents" do
3350
- expect(criteria).to eq([ match ])
3351
- end
3352
- end
3353
-
3354
- describe "#within_circle" do
3355
-
3356
- before do
3357
- Bar.create_indexes
3358
- end
3359
-
3360
- let!(:match) do
3361
- Bar.create(location: [ 52.30, 13.25 ])
3362
- end
3363
-
3364
- let(:criteria) do
3365
- Bar.within_circle(location: [[ 52, 13 ], 0.5 ])
3366
- end
3367
-
3368
- it "returns the matching documents" do
3369
- expect(criteria).to eq([ match ])
3370
- end
3371
- end
3372
-
3373
- describe "#within_polygon" do
3374
-
3375
- before do
3376
- Bar.create_indexes
3377
- end
3378
-
3379
- let!(:match) do
3380
- Bar.create(location: [ 52.30, 13.25 ])
3381
- end
3382
-
3383
- let(:criteria) do
3384
- Bar.within_polygon(
3385
- location: [[ 50, 10 ], [ 50, 20 ], [ 60, 20 ], [ 60, 10 ]]
3386
- )
3387
- end
3388
-
3389
- it "returns the matching documents" do
3390
- expect(criteria).to eq([ match ])
3391
- end
3392
- end
3393
-
3394
- describe "#within_spherical_circle" do
3395
-
3396
- before do
3397
- Bar.create_indexes
3398
- end
3349
+ before do
3350
+ Bar.create_indexes
3351
+ end
3399
3352
 
3400
- let!(:match) do
3401
- Bar.create(location: [ 52.30, 13.25 ])
3402
- end
3353
+ let!(:match) do
3354
+ Bar.create(location: [ 52.30, 13.25 ])
3355
+ end
3403
3356
 
3404
- let(:criteria) do
3405
- Bar.within_spherical_circle(location: [[ 52, 13 ], 0.5 ])
3406
- end
3357
+ let(:criteria) do
3358
+ Bar.geo_spacial(
3359
+ :location.within_polygon => [[[ 50, 10 ], [ 50, 20 ], [ 60, 20 ], [ 60, 10 ], [ 50, 10 ]]]
3360
+ )
3361
+ end
3407
3362
 
3408
- it "returns the matching documents" do
3409
- expect(criteria).to eq([ match ])
3363
+ it "returns the matching documents" do
3364
+ expect(criteria).to eq([ match ])
3365
+ end
3410
3366
  end
3411
3367
  end
3412
3368
 
@@ -16,13 +16,13 @@ describe Mongoid::Errors::InvalidScope do
16
16
 
17
17
  it "contains the summary in the message" do
18
18
  expect(error.message).to include(
19
- "Scopes in Mongoid must be either criteria objects or procs that wrap"
19
+ "Scopes in Mongoid must be procs that wrap"
20
20
  )
21
21
  end
22
22
 
23
23
  it "contains the resolution in the message" do
24
24
  expect(error.message).to include(
25
- "Change the scope to be a criteria or proc wrapped critera."
25
+ "Change the scope to be a proc wrapped critera."
26
26
  )
27
27
  end
28
28
  end