mongoid 8.0.12 → 8.0.14

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 (57) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/locales/en.yml +26 -0
  3. data/lib/mongoid/association/depending.rb +8 -4
  4. data/lib/mongoid/association/nested/many.rb +38 -3
  5. data/lib/mongoid/association/nested/nested_buildable.rb +14 -0
  6. data/lib/mongoid/association/referenced/has_many/proxy.rb +103 -2
  7. data/lib/mongoid/attributes/readonly.rb +19 -1
  8. data/lib/mongoid/changeable.rb +22 -4
  9. data/lib/mongoid/config.rb +62 -0
  10. data/lib/mongoid/contextual/aggregable/memory.rb +5 -2
  11. data/lib/mongoid/contextual/memory.rb +19 -8
  12. data/lib/mongoid/contextual/none.rb +51 -0
  13. data/lib/mongoid/criteria/queryable/mergeable.rb +12 -0
  14. data/lib/mongoid/criteria/queryable/selectable.rb +109 -0
  15. data/lib/mongoid/criteria/scopable.rb +7 -1
  16. data/lib/mongoid/criteria.rb +45 -1
  17. data/lib/mongoid/document.rb +10 -0
  18. data/lib/mongoid/errors/in_memory_regexp_timeout.rb +26 -0
  19. data/lib/mongoid/errors.rb +1 -0
  20. data/lib/mongoid/field_readable.rb +70 -0
  21. data/lib/mongoid/matchable.rb +6 -1
  22. data/lib/mongoid/matcher/eq_impl_with_regexp.rb +3 -5
  23. data/lib/mongoid/matcher/regex.rb +8 -9
  24. data/lib/mongoid/matcher/regexp_budget.rb +389 -0
  25. data/lib/mongoid/matcher.rb +1 -0
  26. data/lib/mongoid/reloadable.rb +1 -0
  27. data/lib/mongoid/threaded.rb +3 -0
  28. data/lib/mongoid/version.rb +1 -1
  29. data/lib/mongoid/warnings.rb +1 -0
  30. data/spec/integration/app_spec.rb +8 -0
  31. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +17 -2
  32. data/spec/integration/dots_and_dollars_spec.rb +12 -2
  33. data/spec/integration/matcher_operator_data/regex.yml +21 -0
  34. data/spec/integration/matcher_regexp_timeout_spec.rb +215 -0
  35. data/spec/integration/query_operator_guard_spec.rb +89 -0
  36. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +96 -0
  37. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +228 -0
  38. data/spec/mongoid/attributes/nested_spec.rb +204 -10
  39. data/spec/mongoid/attributes/readonly_dotted_spec.rb +332 -0
  40. data/spec/mongoid/attributes/readonly_spec.rb +387 -0
  41. data/spec/mongoid/changeable_spec.rb +18 -0
  42. data/spec/mongoid/config_spec.rb +7 -0
  43. data/spec/mongoid/contextual/aggregable/memory_spec.rb +91 -0
  44. data/spec/mongoid/contextual/memory_spec.rb +202 -0
  45. data/spec/mongoid/contextual/none_spec.rb +18 -0
  46. data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +2 -0
  47. data/spec/mongoid/criteria/queryable/selectable_where_spec.rb +235 -0
  48. data/spec/mongoid/criteria_spec.rb +19 -0
  49. data/spec/mongoid/findable_spec.rb +14 -0
  50. data/spec/mongoid/matcher/regexp_budget_spec.rb +576 -0
  51. data/spec/mongoid/scopable_spec.rb +116 -2
  52. data/spec/spec_helper.rb +8 -0
  53. data/spec/support/models/delete_all_account.rb +10 -0
  54. data/spec/support/models/delete_all_item.rb +9 -0
  55. data/spec/support/models/delete_all_scoped_item.rb +11 -0
  56. data/spec/support/models/delete_all_tag.rb +7 -0
  57. metadata +1862 -1826
@@ -83,6 +83,28 @@ describe Mongoid::Attributes::Readonly do
83
83
  it "persists the third readonly value" do
84
84
  expect(person.reload.aliased_timestamp).to eq(Time.at(42))
85
85
  end
86
+
87
+ it 'reports the readonly value as a saved change' do
88
+ expect(person.previous_changes['title']).to eq([ nil, 'sir' ])
89
+ expect(person.title_previously_changed?).to be true
90
+ end
91
+
92
+ it 'reports the saved change for an aliased readonly field' do
93
+ expect(person.previous_changes['at']).to eq([ nil, Time.at(42) ])
94
+ expect(person.aliased_timestamp_previously_changed?).to be true
95
+ end
96
+
97
+ it 'does not persist a later change to the readonly field' do
98
+ person.title = 'mr'
99
+ person.score = 1
100
+ person.save!
101
+
102
+ expect(person.previous_changes).not_to have_key('title')
103
+ expect(person.previous_changes).to have_key('score')
104
+
105
+ expect(person.reload.title).to eq('sir')
106
+ expect(person.reload.score).to eq(1)
107
+ end
86
108
  end
87
109
 
88
110
  context "when updating an existing readonly field" do
@@ -233,6 +255,371 @@ describe Mongoid::Attributes::Readonly do
233
255
  end
234
256
  end
235
257
 
258
+ context 'when modifying a readonly field in place' do
259
+ let(:attributes) do
260
+ %i[map aliases array]
261
+ end
262
+
263
+ let(:person) do
264
+ Person.create!(map: { 'enabled' => false }, aliases: [ 'original' ], array: [ 'original' ], title: 'old')
265
+ end
266
+
267
+ it 'does not persist changes to a readonly hash' do
268
+ person.map['enabled'] = true
269
+ person.save!
270
+
271
+ expect(person.reload.map).to eq('enabled' => false)
272
+ end
273
+
274
+ it 'does not mark the document as changed' do
275
+ person.map['enabled'] = true
276
+
277
+ expect(person).not_to be_changed
278
+ end
279
+
280
+ it 'does not mark an aliased readonly field as changed' do
281
+ person.array << 'added'
282
+
283
+ expect(person).not_to be_changed
284
+ end
285
+
286
+ it 'allows in-place changes before the document is created' do
287
+ new_person = Person.new(map: { 'enabled' => false })
288
+ new_person.map['enabled'] = true
289
+ new_person.save!
290
+
291
+ expect(new_person.reload.map).to eq('enabled' => true)
292
+ end
293
+
294
+ it 'does not report the ignored change as saved' do
295
+ person.map['enabled'] = true
296
+ person.title = 'new'
297
+ person.save!
298
+
299
+ expect(person.previous_changes).not_to have_key('map')
300
+ expect(person.previous_changes).to have_key('title')
301
+ end
302
+
303
+ it 'does not update timestamps for an ignored change' do
304
+ model = test_model do
305
+ include Mongoid::Timestamps
306
+
307
+ field :settings, type: Hash
308
+ attr_readonly :settings
309
+ end
310
+ document = Timecop.freeze(Time.utc(2020, 1, 1)) do
311
+ model.create!(settings: { 'enabled' => false })
312
+ end
313
+
314
+ document.settings['enabled'] = true
315
+ Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
316
+
317
+ expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
318
+ end
319
+
320
+ it 'does not persist changes made through attributes' do
321
+ person.map
322
+ person.attributes['map']['enabled'] = true
323
+ person.save!
324
+
325
+ expect(person.reload.map).to eq('enabled' => false)
326
+ end
327
+
328
+ it 'resets an in-place change to a readonly hash' do
329
+ person.map['enabled'] = true
330
+ person.reset_map!
331
+
332
+ expect(person.map).to eq('enabled' => false)
333
+ expect(person).not_to be_changed
334
+ end
335
+
336
+ context 'when a tracked field becomes readonly' do
337
+ let(:model) do
338
+ test_model do
339
+ include Mongoid::Timestamps
340
+
341
+ field :settings, type: Hash
342
+ field :title
343
+ end
344
+ end
345
+
346
+ let(:document) do
347
+ Timecop.freeze(Time.utc(2020, 1, 1)) do
348
+ model.create!(settings: { 'enabled' => false }, title: 'old')
349
+ end
350
+ end
351
+
352
+ before do
353
+ document.settings['enabled'] = true
354
+ model.attr_readonly :settings
355
+ end
356
+
357
+ it 'does not persist the ignored change' do
358
+ document.save!
359
+
360
+ expect(document.reload.settings).to eq('enabled' => false)
361
+ end
362
+
363
+ it 'does not report the ignored change as dirty' do
364
+ expect(document).not_to be_changed
365
+ expect(document.changes).not_to have_key('settings')
366
+ end
367
+
368
+ it 'does not report the readonly field as changed' do
369
+ expect(document.settings_changed?).to be false
370
+ end
371
+
372
+ it 'does not report the ignored change as saved' do
373
+ document.title = 'new'
374
+ document.save!
375
+
376
+ expect(document.previous_changes).not_to have_key('settings')
377
+ expect(document.settings_previously_changed?).to be false
378
+ expect(document.previous_changes).to have_key('title')
379
+ end
380
+
381
+ it 'does not update timestamps for the ignored change' do
382
+ Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
383
+
384
+ expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
385
+ end
386
+ end
387
+
388
+ it 'does not persist changes to a readonly array' do
389
+ person.aliases << 'added'
390
+ person.save!
391
+
392
+ expect(person.reload.aliases).to eq([ 'original' ])
393
+ end
394
+
395
+ it 'does not persist changes to an aliased readonly field' do
396
+ person.array << 'added'
397
+ person.save!
398
+
399
+ expect(person.reload.array).to eq([ 'original' ])
400
+ end
401
+
402
+ it 'persists changes to other fields' do
403
+ person.map['enabled'] = true
404
+ person.title = 'new'
405
+ person.save!
406
+
407
+ expect(person.reload.map).to eq('enabled' => false)
408
+ expect(person.title).to eq('new')
409
+ end
410
+
411
+ it 'still allows atomic set on a readonly field' do
412
+ person.set('map.enabled' => true)
413
+
414
+ expect(person.reload.map).to eq('enabled' => true)
415
+ end
416
+
417
+ it 'still allows atomic unset on a readonly field' do
418
+ person.unset('map.enabled')
419
+
420
+ expect(person.reload.map).to eq({})
421
+ end
422
+
423
+ context 'when an atomic array update fails' do
424
+ it 'restores the readonly array in memory and in the database' do
425
+ expect do
426
+ person.atomically do
427
+ person.push(aliases: 'added')
428
+ raise 'abort'
429
+ end
430
+ end.to raise_error('abort')
431
+
432
+ expect(Person.collection.find(_id: person.id).first['aliases']).to eq([ 'original' ])
433
+ expect(person.aliases).to eq([ 'original' ])
434
+ end
435
+
436
+ it 'still allows a successful atomic update' do
437
+ person.atomically { person.push(aliases: 'added') }
438
+
439
+ expect(person.reload.aliases).to eq(%w[original added])
440
+ end
441
+ end
442
+ end
443
+
444
+ context 'when an embedded document has a readonly hash' do
445
+ let(:attributes) do
446
+ :title
447
+ end
448
+
449
+ let(:person) do
450
+ Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
451
+ end
452
+
453
+ around do |example|
454
+ original = Address.readonly_attributes
455
+ Address.attr_readonly :map
456
+ example.run
457
+ ensure
458
+ Address.readonly_attributes = original
459
+ end
460
+
461
+ it 'allows the initial value to be saved' do
462
+ expect(person.reload.addresses.first.map).to eq('enabled' => false)
463
+ end
464
+
465
+ it 'ignores in-place changes when saving the parent' do
466
+ person.addresses.first.map['enabled'] = true
467
+ person.save!
468
+
469
+ expect(person.reload.addresses.first.map).to eq('enabled' => false)
470
+ end
471
+
472
+ it 'ignores in-place changes when saving the embedded document' do
473
+ address = person.addresses.first
474
+ address.map['enabled'] = true
475
+ address.save!
476
+
477
+ expect(person.reload.addresses.first.map).to eq('enabled' => false)
478
+ end
479
+
480
+ it 'allows an initial value when adding a new embedded document' do
481
+ person.addresses.build(street: 'Second', map: { 'enabled' => true })
482
+ person.save!
483
+
484
+ expect(person.reload.addresses.last.map).to eq('enabled' => true)
485
+ end
486
+
487
+ it 'reports the initial value as a saved change' do
488
+ address = person.addresses.first
489
+
490
+ expect(address.previous_changes['map']).to eq([ nil, { 'enabled' => false } ])
491
+ expect(address.map_previously_changed?).to be true
492
+ end
493
+ end
494
+
495
+ context 'when a persisted root document is converted with becomes' do
496
+ let(:attributes) do
497
+ :title
498
+ end
499
+
500
+ let(:model) do
501
+ test_model do
502
+ field :settings, type: Hash
503
+ attr_readonly :settings
504
+ end
505
+ end
506
+
507
+ let(:original) do
508
+ model.create!(settings: { 'enabled' => false })
509
+ end
510
+
511
+ it 'does not persist an in-place change to the readonly field of the became root document' do
512
+ became = original.becomes(model)
513
+ became.settings['enabled'] = true
514
+ became.save!
515
+
516
+ expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
517
+ end
518
+ end
519
+
520
+ context 'when a persisted document is converted to another class with becomes' do
521
+ let(:attributes) do
522
+ :title
523
+ end
524
+
525
+ let(:model) do
526
+ test_model do
527
+ field :settings, type: Hash
528
+ attr_readonly :settings
529
+ end
530
+ end
531
+
532
+ let(:subclass) do
533
+ Class.new(model)
534
+ end
535
+
536
+ let(:original) do
537
+ model.create!(settings: { 'enabled' => false })
538
+ end
539
+
540
+ it 'does not persist an in-place change to the readonly field of the became document' do
541
+ became = original.becomes(subclass)
542
+ became.settings['enabled'] = true
543
+ became.save!
544
+
545
+ expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
546
+ end
547
+ end
548
+
549
+ context 'when a persisted document with embedded documents is converted with becomes' do
550
+ let(:attributes) do
551
+ :title
552
+ end
553
+
554
+ let(:person) do
555
+ Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
556
+ end
557
+
558
+ around do |example|
559
+ original = Address.readonly_attributes
560
+ Address.attr_readonly :map
561
+ example.run
562
+ ensure
563
+ Address.readonly_attributes = original
564
+ end
565
+
566
+ it 'does not persist an in-place change to the readonly field of a became embedded document' do
567
+ became = person.becomes(Person)
568
+ became.addresses.first.map['enabled'] = true
569
+ became.save!
570
+
571
+ stored = Person.collection.find(_id: person.id).first['addresses'].first['map']
572
+ expect(stored).to eq('enabled' => false)
573
+ end
574
+ end
575
+
576
+ context 'when a new document is converted with becomes' do
577
+ let(:attributes) do
578
+ :title
579
+ end
580
+
581
+ let(:model) do
582
+ test_model do
583
+ field :settings, type: Hash
584
+ attr_readonly :settings
585
+ end
586
+ end
587
+
588
+ it 'reports the readonly value as a saved change when the became document is created' do
589
+ became = model.new(settings: { 'enabled' => false }).becomes(model)
590
+ became.save!
591
+
592
+ expect(became.previous_changes['settings']).to eq([ nil, { 'enabled' => false } ])
593
+ expect(model.collection.find(_id: became.id).first['settings']).to eq('enabled' => false)
594
+ end
595
+ end
596
+
597
+ context 'when a field is named like an internal dirty tracking method' do
598
+ let(:attributes) do
599
+ :title
600
+ end
601
+
602
+ let(:model) do
603
+ test_model do
604
+ field :name, type: String
605
+ field :readonly_change_ignored, type: String
606
+ end
607
+ end
608
+
609
+ it 'tracks changes and creates the document' do
610
+ document = model.new(name: 'original', readonly_change_ignored: 'value')
611
+
612
+ expect(document.name_changed?).to be true
613
+
614
+ document.save!
615
+
616
+ expect(document.reload.name).to eq('original')
617
+ expect(document.readonly_change_ignored).to eq('value')
618
+ expect(document.readonly_change_ignored?).to be true
619
+ expect(document).not_to be_changed
620
+ end
621
+ end
622
+
236
623
  context 'when a foreign_key is defined as readonly' do
237
624
 
238
625
  let(:attributes) do
@@ -1342,6 +1342,24 @@ describe Mongoid::Changeable do
1342
1342
  expect(person.title).to be_nil
1343
1343
  end
1344
1344
  end
1345
+
1346
+ context 'when a resizable attribute was read but its value has not changed' do
1347
+ let(:person) do
1348
+ Person.create!(map: { 'enabled' => false })
1349
+ end
1350
+
1351
+ it 'keeps the current value object so held references stay attached' do
1352
+ map = person.map
1353
+ person.reset_map!
1354
+
1355
+ expect(person.attributes['map']).to equal(map)
1356
+
1357
+ map['enabled'] = true
1358
+ person.save!
1359
+
1360
+ expect(person.reload.map).to eq('enabled' => true)
1361
+ end
1362
+ end
1345
1363
  end
1346
1364
 
1347
1365
  describe "#reset_attribute_to_default!" do
@@ -233,6 +233,13 @@ describe Mongoid::Config do
233
233
  end
234
234
  end
235
235
 
236
+ # Config.reset does not invoke the option's on_change callback, so use the
237
+ # setter to restore any global state the callback modifies (e.g. the BSON
238
+ # registry for allow_bson5_decimal128).
239
+ after do
240
+ Mongoid.send("#{option}=", default)
241
+ end
242
+
236
243
  context 'when the value is false' do
237
244
 
238
245
  let(:conf) do
@@ -570,4 +570,95 @@ describe Mongoid::Contextual::Aggregable::Memory do
570
570
  end
571
571
  end
572
572
  end
573
+
574
+ context 'when the field name is a method name' do
575
+ let!(:depeche) do
576
+ Band.create!(name: 'Depeche Mode', likes: 1000)
577
+ end
578
+
579
+ let(:criteria) do
580
+ Band.all.tap do |crit|
581
+ crit.documents = [ depeche ]
582
+ end
583
+ end
584
+
585
+ it 'does not call the method for sum' do
586
+ expect(depeche).not_to receive(:delete)
587
+ expect(context.sum(:delete)).to eq(0)
588
+ end
589
+
590
+ it 'does not call the method for min' do
591
+ expect(depeche).not_to receive(:delete)
592
+ expect(context.min(:delete)).to be_nil
593
+ end
594
+
595
+ it 'does not call the method for max' do
596
+ expect(depeche).not_to receive(:delete)
597
+ expect(context.max(:delete)).to be_nil
598
+ end
599
+
600
+ it 'does not call the method for avg' do
601
+ expect(depeche).not_to receive(:delete)
602
+ expect(context.avg(:delete)).to be_nil
603
+ end
604
+
605
+ it 'does not call the method for aggregates' do
606
+ expect(depeche).not_to receive(:delete)
607
+ expect(context.aggregates(:delete))
608
+ .to eq('count' => 0, 'avg' => nil, 'max' => nil, 'min' => nil, 'sum' => 0)
609
+ end
610
+
611
+ it 'does not call a private method for avg' do
612
+ expect(depeche).not_to receive(:exit)
613
+ expect(context.avg(:exit)).to be_nil
614
+ end
615
+ end
616
+
617
+ context 'when the field is not defined' do
618
+ let!(:depeche) do
619
+ Band.create!(name: 'Depeche Mode', likes: 1000)
620
+ end
621
+
622
+ let(:criteria) do
623
+ Band.all.tap do |crit|
624
+ crit.documents = [ depeche ]
625
+ end
626
+ end
627
+
628
+ it 'returns zero for sum' do
629
+ expect(context.sum(:not_a_field)).to eq(0)
630
+ end
631
+
632
+ it 'returns nil for min' do
633
+ expect(context.min(:not_a_field)).to be_nil
634
+ end
635
+
636
+ it 'returns nil for max' do
637
+ expect(context.max(:not_a_field)).to be_nil
638
+ end
639
+
640
+ it 'returns nil for avg' do
641
+ expect(context.avg(:not_a_field)).to be_nil
642
+ end
643
+ end
644
+
645
+ context 'when the field is the foreign key of a many to many association' do
646
+ let!(:preference) do
647
+ Preference.create!(name: 'nature')
648
+ end
649
+
650
+ let!(:person) do
651
+ Person.new(preferences: [ preference ])
652
+ end
653
+
654
+ let(:criteria) do
655
+ Person.all.tap do |crit|
656
+ crit.documents = [ person ]
657
+ end
658
+ end
659
+
660
+ it 'aggregates the stored ids' do
661
+ expect(context.max(:preference_ids)).to eq([ preference.id ])
662
+ end
663
+ end
573
664
  end