mongoid 6.3.0 → 6.4.5

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 (87) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +12 -0
  5. data/lib/config/locales/en.yml +21 -0
  6. data/lib/mongoid.rb +2 -2
  7. data/lib/mongoid/clients.rb +2 -0
  8. data/lib/mongoid/clients/sessions.rb +113 -0
  9. data/lib/mongoid/clients/storage_options.rb +1 -0
  10. data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
  11. data/lib/mongoid/contextual/map_reduce.rb +7 -3
  12. data/lib/mongoid/contextual/memory.rb +7 -2
  13. data/lib/mongoid/contextual/mongo.rb +11 -2
  14. data/lib/mongoid/criteria.rb +1 -0
  15. data/lib/mongoid/criteria/modifiable.rb +12 -2
  16. data/lib/mongoid/criteria/queryable/mergeable.rb +3 -1
  17. data/lib/mongoid/criteria/queryable/selectable.rb +34 -7
  18. data/lib/mongoid/document.rb +4 -4
  19. data/lib/mongoid/errors.rb +1 -0
  20. data/lib/mongoid/errors/invalid_session_use.rb +24 -0
  21. data/lib/mongoid/extensions/big_decimal.rb +1 -1
  22. data/lib/mongoid/extensions/regexp.rb +1 -0
  23. data/lib/mongoid/extensions/string.rb +3 -1
  24. data/lib/mongoid/indexable.rb +4 -4
  25. data/lib/mongoid/matchable.rb +3 -0
  26. data/lib/mongoid/matchable/nor.rb +37 -0
  27. data/lib/mongoid/persistable.rb +1 -1
  28. data/lib/mongoid/persistable/creatable.rb +4 -2
  29. data/lib/mongoid/persistable/deletable.rb +4 -2
  30. data/lib/mongoid/persistable/destroyable.rb +1 -5
  31. data/lib/mongoid/persistable/settable.rb +5 -5
  32. data/lib/mongoid/persistable/updatable.rb +2 -2
  33. data/lib/mongoid/persistable/upsertable.rb +2 -1
  34. data/lib/mongoid/persistence_context.rb +4 -0
  35. data/lib/mongoid/railtie.rb +17 -0
  36. data/lib/mongoid/railties/controller_runtime.rb +86 -0
  37. data/lib/mongoid/relations/embedded/batchable.rb +10 -4
  38. data/lib/mongoid/relations/embedded/many.rb +23 -0
  39. data/lib/mongoid/relations/many.rb +4 -0
  40. data/lib/mongoid/relations/referenced/many.rb +1 -1
  41. data/lib/mongoid/relations/touchable.rb +1 -1
  42. data/lib/mongoid/reloadable.rb +1 -1
  43. data/lib/mongoid/scopable.rb +3 -3
  44. data/lib/mongoid/tasks/database.rb +3 -2
  45. data/lib/mongoid/threaded.rb +74 -0
  46. data/lib/mongoid/version.rb +1 -1
  47. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +4 -0
  48. data/spec/app/models/array_field.rb +7 -0
  49. data/spec/app/models/delegating_patient.rb +16 -0
  50. data/spec/integration/document_spec.rb +22 -0
  51. data/spec/mongoid/attributes/nested_spec.rb +4 -0
  52. data/spec/mongoid/clients/factory_spec.rb +52 -28
  53. data/spec/mongoid/clients/options_spec.rb +30 -15
  54. data/spec/mongoid/clients/sessions_spec.rb +334 -0
  55. data/spec/mongoid/contextual/geo_near_spec.rb +1 -0
  56. data/spec/mongoid/contextual/mongo_spec.rb +40 -2
  57. data/spec/mongoid/criteria/modifiable_spec.rb +59 -10
  58. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +3 -3
  59. data/spec/mongoid/criteria/queryable/selectable_spec.rb +74 -6
  60. data/spec/mongoid/criteria/queryable/selector_spec.rb +2 -2
  61. data/spec/mongoid/criteria/scopable_spec.rb +81 -0
  62. data/spec/mongoid/criteria_spec.rb +4 -1
  63. data/spec/mongoid/document_spec.rb +54 -0
  64. data/spec/mongoid/extensions/big_decimal_spec.rb +9 -9
  65. data/spec/mongoid/extensions/regexp_spec.rb +23 -0
  66. data/spec/mongoid/extensions/string_spec.rb +35 -7
  67. data/spec/mongoid/fields_spec.rb +1 -1
  68. data/spec/mongoid/findable_spec.rb +1 -1
  69. data/spec/mongoid/interceptable_spec.rb +1 -1
  70. data/spec/mongoid/matchable/nor_spec.rb +209 -0
  71. data/spec/mongoid/matchable_spec.rb +26 -1
  72. data/spec/mongoid/persistable/deletable_spec.rb +19 -0
  73. data/spec/mongoid/persistable/destroyable_spec.rb +19 -0
  74. data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
  75. data/spec/mongoid/persistable/settable_spec.rb +35 -1
  76. data/spec/mongoid/persistable_spec.rb +16 -16
  77. data/spec/mongoid/relations/embedded/many_spec.rb +246 -16
  78. data/spec/mongoid/scopable_spec.rb +13 -0
  79. data/spec/mongoid/threaded_spec.rb +68 -0
  80. data/spec/rails/controller_extension/controller_runtime_spec.rb +110 -0
  81. data/spec/spec_helper.rb +79 -0
  82. data/spec/support/cluster_config.rb +158 -0
  83. data/spec/support/constraints.rb +101 -0
  84. data/spec/support/macros.rb +20 -0
  85. data/spec/support/spec_config.rb +42 -0
  86. metadata +471 -443
  87. metadata.gz.sig +0 -0
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  describe Mongoid::Extensions::BigDecimal do
4
4
 
5
5
  let(:big_decimal) do
6
- BigDecimal.new("123456.789")
6
+ BigDecimal("123456.789")
7
7
  end
8
8
 
9
9
  describe ".demongoize" do
@@ -26,7 +26,7 @@ describe Mongoid::Extensions::BigDecimal do
26
26
  end
27
27
 
28
28
  it "returns a BigDecimal" do
29
- expect(BigDecimal.demongoize(string)).to eq(BigDecimal.new(string))
29
+ expect(BigDecimal.demongoize(string)).to eq(BigDecimal(string))
30
30
  end
31
31
  end
32
32
 
@@ -37,7 +37,7 @@ describe Mongoid::Extensions::BigDecimal do
37
37
  end
38
38
 
39
39
  it "returns a BigDecimal" do
40
- expect(BigDecimal.demongoize(string)).to eq(BigDecimal.new(string))
40
+ expect(BigDecimal.demongoize(string)).to eq(BigDecimal(string))
41
41
  end
42
42
  end
43
43
 
@@ -48,7 +48,7 @@ describe Mongoid::Extensions::BigDecimal do
48
48
  end
49
49
 
50
50
  it "returns a BigDecimal" do
51
- expect(BigDecimal.demongoize(string)).to eq(BigDecimal.new(string))
51
+ expect(BigDecimal.demongoize(string)).to eq(BigDecimal(string))
52
52
  end
53
53
  end
54
54
 
@@ -184,7 +184,7 @@ describe Mongoid::Extensions::BigDecimal do
184
184
  context "when the value is the BigDecimal zero" do
185
185
 
186
186
  let(:big_decimal) do
187
- BigDecimal.new("0.0")
187
+ BigDecimal("0.0")
188
188
  end
189
189
 
190
190
  it "returns a BigDecimal" do
@@ -195,7 +195,7 @@ describe Mongoid::Extensions::BigDecimal do
195
195
  context "when the value is the BigDecimal negative zero" do
196
196
 
197
197
  let(:big_decimal) do
198
- BigDecimal.new("-0.0")
198
+ BigDecimal("-0.0")
199
199
  end
200
200
 
201
201
  it "returns a BigDecimal" do
@@ -326,7 +326,7 @@ describe Mongoid::Extensions::BigDecimal do
326
326
  context "when the value is BigDecimal NaN" do
327
327
 
328
328
  let(:nan) do
329
- BigDecimal.new("NaN")
329
+ BigDecimal("NaN")
330
330
  end
331
331
 
332
332
  it "returns a String" do
@@ -337,7 +337,7 @@ describe Mongoid::Extensions::BigDecimal do
337
337
  context "when the value is BigDecimal Infinity" do
338
338
 
339
339
  let(:infinity) do
340
- BigDecimal.new("Infinity")
340
+ BigDecimal("Infinity")
341
341
  end
342
342
 
343
343
  it "returns a String" do
@@ -348,7 +348,7 @@ describe Mongoid::Extensions::BigDecimal do
348
348
  context "when the value is BigDecimal negative Infinity" do
349
349
 
350
350
  let(:neg_infinity) do
351
- BigDecimal.new("-Infinity")
351
+ BigDecimal("-Infinity")
352
352
  end
353
353
 
354
354
  it "returns a String" do
@@ -35,6 +35,29 @@ describe Mongoid::Extensions::Regexp do
35
35
  it "returns the provided value as a regex" do
36
36
  expect(value).to eq(/[^abc]/)
37
37
  end
38
+
39
+
40
+ context "when the string is empty" do
41
+
42
+ let(:value) do
43
+ Regexp.mongoize("")
44
+ end
45
+
46
+ it "returns an empty regex" do
47
+ expect(value).to eq(//)
48
+ end
49
+ end
50
+ end
51
+
52
+ context "when the value is nil" do
53
+
54
+ let(:value) do
55
+ Regexp.mongoize(nil)
56
+ end
57
+
58
+ it "returns the nil" do
59
+ expect(value).to be_nil
60
+ end
38
61
  end
39
62
  end
40
63
 
@@ -277,49 +277,77 @@ describe Mongoid::Extensions::String do
277
277
  context "when the string is an integer" do
278
278
 
279
279
  it "returns true" do
280
- expect("1234").to be_numeric
280
+ expect("1234".numeric?).to eq(true)
281
281
  end
282
282
  end
283
283
 
284
284
  context "when string is a float" do
285
285
 
286
286
  it "returns true" do
287
- expect("1234.123").to be_numeric
287
+ expect("1234.123".numeric?).to eq(true)
288
288
  end
289
289
  end
290
290
 
291
291
  context "when the string is has exponents" do
292
292
 
293
293
  it "returns true" do
294
- expect("1234.123123E4").to be_numeric
294
+ expect("1234.123123E4".numeric?).to eq(true)
295
295
  end
296
296
  end
297
297
 
298
298
  context "when the string is non numeric" do
299
299
 
300
300
  it "returns false" do
301
- expect("blah").to_not be_numeric
301
+ expect("blah".numeric?).to eq(false)
302
302
  end
303
303
  end
304
304
 
305
305
  context "when the string is NaN" do
306
306
 
307
307
  it "returns true" do
308
- expect("NaN").to be_numeric
308
+ expect("NaN".numeric?).to eq(true)
309
+ end
310
+ end
311
+
312
+ context "when the string is NaN and junk in front" do
313
+
314
+ it "returns false" do
315
+ expect("a\nNaN".numeric?).to eq(false)
316
+ end
317
+ end
318
+
319
+ context "when the string is NaN and whitespace at end" do
320
+
321
+ it "returns false" do
322
+ expect("NaN\n".numeric?).to eq(false)
309
323
  end
310
324
  end
311
325
 
312
326
  context "when the string is Infinity" do
313
327
 
314
328
  it "returns true" do
315
- expect("Infinity").to be_numeric
329
+ expect("Infinity".numeric?).to eq(true)
330
+ end
331
+ end
332
+
333
+ context "when the string contains Infinity and junk in front" do
334
+
335
+ it "returns false" do
336
+ expect("a\nInfinity".numeric?).to eq(false)
337
+ end
338
+ end
339
+
340
+ context "when the string contains Infinity and whitespace at end" do
341
+
342
+ it "returns false" do
343
+ expect("Infinity\n".numeric?).to eq(false)
316
344
  end
317
345
  end
318
346
 
319
347
  context "when the string is -Infinity" do
320
348
 
321
349
  it "returns true" do
322
- expect("-Infinity").to be_numeric
350
+ expect("-Infinity".numeric?).to eq(true)
323
351
  end
324
352
  end
325
353
  end
@@ -1171,7 +1171,7 @@ describe Mongoid::Fields do
1171
1171
  end
1172
1172
 
1173
1173
  let(:decimal) do
1174
- BigDecimal.new("1000000.00")
1174
+ BigDecimal("1000000.00")
1175
1175
  end
1176
1176
 
1177
1177
  context "when setting to a big decimal" do
@@ -10,7 +10,7 @@ describe Mongoid::Findable do
10
10
  end
11
11
 
12
12
  it "returns the distinct values for the field" do
13
- expect(Band.distinct(:name)).to eq([ "Tool", "Photek" ])
13
+ expect(Band.distinct(:name).sort).to eq([ "Photek", "Tool" ])
14
14
  end
15
15
  end
16
16
 
@@ -526,7 +526,7 @@ describe Mongoid::Interceptable do
526
526
  end
527
527
 
528
528
  after(:all) do
529
- Band.reset_callbacks(:rearrange)
529
+ begin; Band.reset_callbacks(:rearrange); rescue; end
530
530
  end
531
531
 
532
532
  let(:attributes) do
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+
5
+ describe Mongoid::Matchable::Nor do
6
+
7
+ let(:target) do
8
+ Person.new
9
+ end
10
+
11
+ let(:matcher) do
12
+ described_class.new("value", target)
13
+ end
14
+
15
+ describe "#_matches?" do
16
+
17
+ context "when provided a simple expression" do
18
+
19
+ context "when one of the hashes does not match model" do
20
+
21
+ let(:matches) do
22
+ matcher._matches?(
23
+ [ { title: "Sir" }, { title: "King" } ]
24
+ )
25
+ end
26
+
27
+ let(:target) do
28
+ Person.new(title: 'Queen')
29
+ end
30
+
31
+ it "returns true" do
32
+ expect(matches).to be true
33
+ end
34
+ end
35
+
36
+ context "when all of the hashes match different fields in model" do
37
+ let(:matches) do
38
+ matcher._matches?(
39
+ [ { age: 10 }, { title: "King" } ]
40
+ )
41
+ end
42
+
43
+ let(:target) do
44
+ Person.new(title: 'King', age: 10)
45
+ end
46
+
47
+ it "returns false" do
48
+ expect(matches).to be false
49
+ end
50
+ end
51
+
52
+ context "when one of the hashes matches an array field in model" do
53
+ let(:matches) do
54
+ matcher._matches?(
55
+ [ { af: "Sir" }, { af: "King" } ]
56
+ )
57
+ end
58
+
59
+ let(:target) do
60
+ ArrayField.new(af: ['King'])
61
+ end
62
+
63
+ it "returns false" do
64
+ expect(matches).to be false
65
+ end
66
+ end
67
+
68
+ context "when none of the hashes matches an array field in model" do
69
+ let(:matches) do
70
+ matcher._matches?(
71
+ [ { af: "Sir" }, { af: "King" } ]
72
+ )
73
+ end
74
+
75
+ let(:target) do
76
+ ArrayField.new(af: ['Boo'])
77
+ end
78
+
79
+ it "returns true" do
80
+ expect(matches).to be true
81
+ end
82
+ end
83
+
84
+ context "when there are no criteria" do
85
+
86
+ it "returns false" do
87
+ expect(matcher._matches?([])).to be false
88
+ end
89
+ end
90
+
91
+ # $nor with $not is a double negation.
92
+ # Whatever the argument of $not is is what the overall condition
93
+ # is looking for.
94
+ context "when the expression is a $not" do
95
+
96
+ let(:matches) do
97
+ matcher._matches?([ { title: {:$not => /Foobar/ } }])
98
+ end
99
+
100
+ context "when the value does not match $not argument" do
101
+
102
+ let(:target) do
103
+ Person.new(title: 'test')
104
+ end
105
+
106
+ it "returns false" do
107
+ expect(matches).to be false
108
+ end
109
+ end
110
+
111
+ context "when the value matches $not argument" do
112
+
113
+ let(:target) do
114
+ Person.new(title: 'Foobar baz')
115
+ end
116
+
117
+ it "returns true" do
118
+ expect(matches).to be true
119
+ end
120
+ end
121
+ end
122
+ end
123
+
124
+ context "when provided a complex expression" do
125
+
126
+ context "when none of the model values match criteria values" do
127
+
128
+ let(:matches) do
129
+ matcher._matches?(
130
+ [
131
+ { title: { "$in" => [ "Sir", "Madam" ] } },
132
+ { title: "King" }
133
+ ]
134
+ )
135
+ end
136
+
137
+ let(:target) do
138
+ Person.new(title: 'Queen')
139
+ end
140
+
141
+ it "returns true" do
142
+ expect(matches).to be true
143
+ end
144
+ end
145
+
146
+ context "when there is a matching value" do
147
+
148
+ let(:matches) do
149
+ matcher._matches?(
150
+ [
151
+ { title: { "$in" => [ "Prince", "Madam" ] } },
152
+ { title: "King" }
153
+ ]
154
+ )
155
+ end
156
+
157
+ let(:target) do
158
+ Person.new(title: 'Prince')
159
+ end
160
+
161
+ it "returns false" do
162
+ expect(matches).to be false
163
+ end
164
+ end
165
+
166
+ context "when expression contain multiple fields" do
167
+
168
+ let(:matches) do
169
+ matcher._matches?(
170
+ [
171
+ { title: "Sir", age: 23 },
172
+ { title: "King", age: 100 }
173
+ ]
174
+ )
175
+ end
176
+
177
+ context 'and model has different values in all of the fields' do
178
+ let(:target) do
179
+ Person.new(title: 'Queen', age: 10)
180
+ end
181
+
182
+ it "returns true" do
183
+ expect(matches).to be true
184
+ end
185
+ end
186
+
187
+ context 'and model has identical value in one of the fields' do
188
+ let(:target) do
189
+ Person.new(title: 'Queen', age: 23)
190
+ end
191
+
192
+ it "returns true" do
193
+ expect(matches).to be true
194
+ end
195
+ end
196
+
197
+ context 'and model has identical values in all of the fields' do
198
+ let(:target) do
199
+ Person.new(title: 'Sir', age: 23)
200
+ end
201
+
202
+ it "returns false" do
203
+ expect(matches).to be false
204
+ end
205
+ end
206
+ end
207
+ end
208
+ end
209
+ end
@@ -86,7 +86,7 @@ describe Mongoid::Matchable do
86
86
  let(:selector) do
87
87
  { "occupants.0" => "Tim" }
88
88
  end
89
-
89
+
90
90
  it "returns true" do
91
91
  expect(document.locations.first._matches?(selector)).to be true
92
92
  end
@@ -774,6 +774,31 @@ describe Mongoid::Matchable do
774
774
  end
775
775
  end
776
776
 
777
+ context "with an $nor selector" do
778
+
779
+ context "when the attributes match" do
780
+
781
+ let(:selector) do
782
+ { "$nor" => [ { number: 10 }, { number: { "$gt" => 199 } } ] }
783
+ end
784
+
785
+ it "returns true" do
786
+ expect(document._matches?(selector)).to be true
787
+ end
788
+ end
789
+
790
+ context "when the attributes do not match" do
791
+
792
+ let(:selector) do
793
+ { "$nor" => [ { number: 10 }, { number: { "$gt" => 99 } } ] }
794
+ end
795
+
796
+ it "returns false" do
797
+ expect(document._matches?(selector)).to be false
798
+ end
799
+ end
800
+ end
801
+
777
802
  context "with a $size selector" do
778
803
 
779
804
  context "when the attributes match" do