mongoid 6.4.0 → 6.4.7

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 (76) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/Rakefile +26 -0
  5. data/lib/mongoid.rb +1 -1
  6. data/lib/mongoid/clients/sessions.rb +2 -2
  7. data/lib/mongoid/contextual/aggregable/mongo.rb +1 -1
  8. data/lib/mongoid/contextual/map_reduce.rb +4 -4
  9. data/lib/mongoid/contextual/memory.rb +4 -4
  10. data/lib/mongoid/contextual/mongo.rb +3 -3
  11. data/lib/mongoid/criteria/modifiable.rb +12 -2
  12. data/lib/mongoid/criteria/queryable/selectable.rb +34 -7
  13. data/lib/mongoid/document.rb +4 -4
  14. data/lib/mongoid/extensions/big_decimal.rb +1 -1
  15. data/lib/mongoid/extensions/regexp.rb +1 -0
  16. data/lib/mongoid/extensions/string.rb +3 -1
  17. data/lib/mongoid/indexable.rb +4 -4
  18. data/lib/mongoid/matchable.rb +3 -0
  19. data/lib/mongoid/matchable/nor.rb +37 -0
  20. data/lib/mongoid/persistable.rb +1 -1
  21. data/lib/mongoid/persistable/creatable.rb +2 -2
  22. data/lib/mongoid/persistable/deletable.rb +2 -2
  23. data/lib/mongoid/persistable/settable.rb +5 -5
  24. data/lib/mongoid/persistable/updatable.rb +2 -2
  25. data/lib/mongoid/persistable/upsertable.rb +1 -1
  26. data/lib/mongoid/persistence_context.rb +4 -0
  27. data/lib/mongoid/query_cache.rb +21 -10
  28. data/lib/mongoid/railtie.rb +17 -0
  29. data/lib/mongoid/railties/controller_runtime.rb +86 -0
  30. data/lib/mongoid/relations/embedded/batchable.rb +4 -4
  31. data/lib/mongoid/relations/embedded/many.rb +23 -0
  32. data/lib/mongoid/relations/many.rb +2 -2
  33. data/lib/mongoid/relations/referenced/many.rb +1 -1
  34. data/lib/mongoid/relations/touchable.rb +1 -1
  35. data/lib/mongoid/reloadable.rb +1 -1
  36. data/lib/mongoid/scopable.rb +3 -3
  37. data/lib/mongoid/tasks/database.rb +2 -2
  38. data/lib/mongoid/threaded.rb +36 -0
  39. data/lib/mongoid/version.rb +1 -1
  40. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +4 -0
  41. data/spec/app/models/array_field.rb +7 -0
  42. data/spec/app/models/delegating_patient.rb +16 -0
  43. data/spec/integration/document_spec.rb +22 -0
  44. data/spec/mongoid/clients/factory_spec.rb +52 -28
  45. data/spec/mongoid/clients/options_spec.rb +30 -15
  46. data/spec/mongoid/clients/sessions_spec.rb +12 -3
  47. data/spec/mongoid/contextual/geo_near_spec.rb +1 -0
  48. data/spec/mongoid/contextual/mongo_spec.rb +2 -2
  49. data/spec/mongoid/criteria/modifiable_spec.rb +59 -10
  50. data/spec/mongoid/criteria/queryable/extensions/big_decimal_spec.rb +3 -3
  51. data/spec/mongoid/criteria/queryable/selectable_spec.rb +42 -3
  52. data/spec/mongoid/criteria/queryable/selector_spec.rb +2 -2
  53. data/spec/mongoid/criteria/scopable_spec.rb +81 -0
  54. data/spec/mongoid/criteria_spec.rb +4 -1
  55. data/spec/mongoid/document_spec.rb +54 -0
  56. data/spec/mongoid/extensions/big_decimal_spec.rb +9 -9
  57. data/spec/mongoid/extensions/regexp_spec.rb +23 -0
  58. data/spec/mongoid/extensions/string_spec.rb +35 -7
  59. data/spec/mongoid/fields_spec.rb +1 -1
  60. data/spec/mongoid/findable_spec.rb +1 -1
  61. data/spec/mongoid/matchable/nor_spec.rb +209 -0
  62. data/spec/mongoid/matchable_spec.rb +26 -1
  63. data/spec/mongoid/persistable/incrementable_spec.rb +6 -6
  64. data/spec/mongoid/persistable/settable_spec.rb +35 -1
  65. data/spec/mongoid/query_cache_spec.rb +73 -18
  66. data/spec/mongoid/relations/embedded/many_spec.rb +246 -16
  67. data/spec/mongoid/scopable_spec.rb +13 -0
  68. data/spec/mongoid/threaded_spec.rb +68 -0
  69. data/spec/rails/controller_extension/controller_runtime_spec.rb +110 -0
  70. data/spec/spec_helper.rb +9 -0
  71. data/spec/support/cluster_config.rb +158 -0
  72. data/spec/support/constraints.rb +101 -0
  73. data/spec/support/macros.rb +20 -0
  74. data/spec/support/spec_config.rb +42 -0
  75. metadata +41 -23
  76. metadata.gz.sig +0 -0
@@ -7,7 +7,7 @@ describe BigDecimal do
7
7
  context "when provided a big decimal" do
8
8
 
9
9
  let(:big_decimal) do
10
- BigDecimal.new("123456.789")
10
+ BigDecimal("123456.789")
11
11
  end
12
12
 
13
13
  it "returns the decimal as a string" do
@@ -25,11 +25,11 @@ describe BigDecimal do
25
25
  context "when provided an array of big decimals" do
26
26
 
27
27
  let(:bd_one) do
28
- BigDecimal.new("123456.789")
28
+ BigDecimal("123456.789")
29
29
  end
30
30
 
31
31
  let(:bd_two) do
32
- BigDecimal.new("123333.789")
32
+ BigDecimal("123333.789")
33
33
  end
34
34
 
35
35
  let(:array) do
@@ -1073,6 +1073,26 @@ describe Mongoid::Criteria::Queryable::Selectable do
1073
1073
  })
1074
1074
  end
1075
1075
 
1076
+ context "when used with the $box operator ($geoWithin query) " do
1077
+ let(:selection) do
1078
+ query.geo_spacial(
1079
+ :location.within_box => [[ 1, 10 ], [ 2, 10 ]]
1080
+ )
1081
+ end
1082
+
1083
+ it "adds the $geoIntersects expression" do
1084
+ expect(selection.selector).to eq({
1085
+ "location" => {
1086
+ "$geoWithin" => {
1087
+ "$box" => [
1088
+ [ 1, 10 ], [ 2, 10 ]
1089
+ ]
1090
+ }
1091
+ }
1092
+ })
1093
+ end
1094
+ end
1095
+
1076
1096
  it_behaves_like "a cloning selection"
1077
1097
  end
1078
1098
  end
@@ -3369,7 +3389,7 @@ describe Mongoid::Criteria::Queryable::Selectable do
3369
3389
  end
3370
3390
 
3371
3391
  it "constructs a text search document" do
3372
- expect(selection.selector).to eq({ :$text => { :$search => "testing" }})
3392
+ expect(selection.selector).to eq({ '$text' => { '$search' => "testing" }})
3373
3393
  end
3374
3394
 
3375
3395
  it "returns the cloned selectable" do
@@ -3383,16 +3403,35 @@ describe Mongoid::Criteria::Queryable::Selectable do
3383
3403
  end
3384
3404
 
3385
3405
  it "constructs a text search document" do
3386
- expect(selection.selector[:$text][:$search]).to eq("essais")
3406
+ expect(selection.selector['$text']['$search']).to eq("essais")
3387
3407
  end
3388
3408
 
3389
3409
  it "add the options to the text search document" do
3390
- expect(selection.selector[:$text][:$language]).to eq("fr")
3410
+ expect(selection.selector['$text'][:$language]).to eq("fr")
3391
3411
  end
3392
3412
 
3393
3413
  it_behaves_like "a cloning selection"
3394
3414
  end
3395
3415
  end
3416
+
3417
+ context 'when given more than once' do
3418
+ let(:selection) do
3419
+ query.text_search("one").text_search('two')
3420
+ end
3421
+
3422
+ # MongoDB server can only handle one text expression at a time,
3423
+ # per https://docs.mongodb.com/manual/reference/operator/query/text/.
3424
+ # Nonetheless we test that the query is built correctly when
3425
+ # a user supplies more than one text condition.
3426
+ it 'merges conditions' do
3427
+ expect(Mongoid.logger).to receive(:warn)
3428
+ expect(selection.selector).to eq('$and' => [
3429
+ {'$text' => {'$search' => 'one'}}
3430
+ ],
3431
+ '$text' => {'$search' => 'two'},
3432
+ )
3433
+ end
3434
+ end
3396
3435
  end
3397
3436
 
3398
3437
  describe "#where" do
@@ -370,11 +370,11 @@ describe Mongoid::Criteria::Queryable::Selector do
370
370
  context "when providing an array" do
371
371
 
372
372
  let(:big_one) do
373
- BigDecimal.new("1.2321")
373
+ BigDecimal("1.2321")
374
374
  end
375
375
 
376
376
  let(:big_two) do
377
- BigDecimal.new("4.2222")
377
+ BigDecimal("4.2222")
378
378
  end
379
379
 
380
380
  let(:array) do
@@ -388,4 +388,85 @@ describe Mongoid::Criteria::Scopable do
388
388
  end
389
389
  end
390
390
  end
391
+
392
+ describe 'scope and where' do
393
+ class ScopeWhereModel
394
+ include Mongoid::Document
395
+
396
+ scope :foo, -> { where(foo: true) }
397
+ end
398
+
399
+ shared_examples_for 'restricts to both' do
400
+ it 'restricts to both' do
401
+ expect(result.selector['foo']).to eq(true)
402
+ expect(result.selector['hello']).to eq('world')
403
+ end
404
+ end
405
+
406
+ context 'scope is given first' do
407
+ let(:result) { ScopeWhereModel.foo.where(hello: 'world') }
408
+
409
+ it_behaves_like 'restricts to both'
410
+ end
411
+
412
+ context 'where is given first' do
413
+ let(:result) { ScopeWhereModel.where(hello: 'world').foo }
414
+
415
+ it_behaves_like 'restricts to both'
416
+ end
417
+ end
418
+
419
+ describe 'scope with argument and where' do
420
+ class ArgumentScopeWhereModel
421
+ include Mongoid::Document
422
+
423
+ scope :my_text_search, ->(search) { where('$text' => {'$search' => search}) }
424
+ end
425
+
426
+ shared_examples_for 'restricts to both' do
427
+ it 'restricts to both' do
428
+ expect(result.selector['$text']).to eq({'$search' => 'bar'})
429
+ expect(result.selector['hello']).to eq('world')
430
+ end
431
+ end
432
+
433
+ context 'scope is given first' do
434
+ let(:result) { ArgumentScopeWhereModel.my_text_search('bar').where(hello: 'world') }
435
+
436
+ it_behaves_like 'restricts to both'
437
+ end
438
+
439
+ context 'where is given first' do
440
+ let(:result) { ArgumentScopeWhereModel.where(hello: 'world').my_text_search('bar') }
441
+
442
+ it_behaves_like 'restricts to both'
443
+ end
444
+ end
445
+
446
+ describe 'built in text search scope and where' do
447
+ class TextSearchScopeWhereModel
448
+ include Mongoid::Document
449
+
450
+ # using the default text_search scope
451
+ end
452
+
453
+ shared_examples_for 'restricts to both' do
454
+ it 'restricts to both' do
455
+ expect(result.selector['$text']).to eq({'$search' => 'bar'})
456
+ expect(result.selector['hello']).to eq('world')
457
+ end
458
+ end
459
+
460
+ context 'scope is given first' do
461
+ let(:result) { TextSearchScopeWhereModel.text_search('bar').where(hello: 'world') }
462
+
463
+ it_behaves_like 'restricts to both'
464
+ end
465
+
466
+ context 'where is given first' do
467
+ let(:result) { TextSearchScopeWhereModel.where(hello: 'world').text_search('bar') }
468
+
469
+ it_behaves_like 'restricts to both'
470
+ end
471
+ end
391
472
  end
@@ -975,6 +975,7 @@ describe Mongoid::Criteria do
975
975
  end
976
976
 
977
977
  describe "#geo_near" do
978
+ max_server_version '4.0'
978
979
 
979
980
  before do
980
981
  Bar.create_indexes
@@ -3321,6 +3322,8 @@ describe Mongoid::Criteria do
3321
3322
  end
3322
3323
 
3323
3324
  describe "#max_scan" do
3325
+ max_server_version '4.0'
3326
+
3324
3327
  let!(:band) do
3325
3328
  Band.create(name: "Depeche Mode")
3326
3329
  end
@@ -3465,7 +3468,7 @@ describe Mongoid::Criteria do
3465
3468
  context "when querying on a big decimal" do
3466
3469
 
3467
3470
  let(:sales) do
3468
- BigDecimal.new('0.1')
3471
+ BigDecimal('0.1')
3469
3472
  end
3470
3473
 
3471
3474
  let!(:band) do
@@ -433,6 +433,60 @@ describe Mongoid::Document do
433
433
  end
434
434
  end
435
435
  end
436
+
437
+ context ':compact option' do
438
+
439
+ before do
440
+ # These tests require a specific set of defined attributes
441
+ # on the model
442
+ expect(church.as_json.keys.sort).to eq(%w(_id location name))
443
+ end
444
+
445
+ context 'there is a nil valued attribute' do
446
+ let(:church) do
447
+ Church.create!(name: 'St. Basil')
448
+ end
449
+
450
+ it 'returns non-nil fields and _id only' do
451
+ actual = church.as_json(compact: true)
452
+ expect(actual).to eq('_id' => church.id, 'name' => 'St. Basil')
453
+ end
454
+ end
455
+
456
+ context 'all attrbutes are nil valued' do
457
+ let(:church) do
458
+ Church.create!
459
+ end
460
+
461
+ it 'returns a hash with _id only' do
462
+ actual = church.as_json(compact: true)
463
+ expect(actual).to eq('_id' => church.id)
464
+ end
465
+ end
466
+
467
+ context 'there are no nil valued attributes' do
468
+ let(:church) do
469
+ Church.create!(name: 'St. Basil', location: {})
470
+ end
471
+
472
+ it 'returns all fields and _id' do
473
+ actual = church.as_json(compact: true)
474
+ expect(actual).to eq('_id' => church.id, 'name' => 'St. Basil',
475
+ 'location' => {})
476
+ end
477
+ end
478
+
479
+ context 'when option is specified as a truthy value' do
480
+ let(:church) do
481
+ Church.create!(name: 'St. Basil')
482
+ end
483
+
484
+ it 'returns non-nil fields and _id only' do
485
+ actual = church.as_json(compact: 1)
486
+ expect(actual).to eq('_id' => church.id, 'name' => 'St. Basil')
487
+ end
488
+ end
489
+ end
436
490
  end
437
491
 
438
492
  describe "#as_document" do
@@ -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