mongoid 9.1.1 → 9.1.3

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 (85) hide show
  1. checksums.yaml +4 -4
  2. data/lib/config/locales/en.yml +44 -11
  3. data/lib/mongoid/association/accessors.rb +21 -4
  4. data/lib/mongoid/association/eager_load/discriminated_inclusion.rb +10 -10
  5. data/lib/mongoid/association/eager_load/embedded_distributor.rb +6 -6
  6. data/lib/mongoid/association/eager_load/lookup_pipeline.rb +1 -1
  7. data/lib/mongoid/association/eager_loadable.rb +8 -1
  8. data/lib/mongoid/association/embedded/batchable.rb +17 -1
  9. data/lib/mongoid/association/nested/many.rb +1 -1
  10. data/lib/mongoid/association/referenced/belongs_to/buildable.rb +19 -15
  11. data/lib/mongoid/association/referenced/belongs_to.rb +11 -3
  12. data/lib/mongoid/association/referenced/has_and_belongs_to_many/buildable.rb +25 -5
  13. data/lib/mongoid/association/referenced/has_and_belongs_to_many.rb +12 -0
  14. data/lib/mongoid/association/referenced/has_many/buildable.rb +17 -7
  15. data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -2
  16. data/lib/mongoid/association/referenced/has_one/buildable.rb +20 -6
  17. data/lib/mongoid/association/relatable.rb +18 -0
  18. data/lib/mongoid/association.rb +23 -0
  19. data/lib/mongoid/atomic/modifiers.rb +100 -16
  20. data/lib/mongoid/attributes/readonly.rb +19 -1
  21. data/lib/mongoid/changeable.rb +19 -1
  22. data/lib/mongoid/clients/factory.rb +2 -0
  23. data/lib/mongoid/config/encryption.rb +152 -19
  24. data/lib/mongoid/contextual/none.rb +51 -0
  25. data/lib/mongoid/criteria/scopable.rb +5 -0
  26. data/lib/mongoid/criteria.rb +45 -1
  27. data/lib/mongoid/document.rb +13 -0
  28. data/lib/mongoid/encryptable.rb +43 -16
  29. data/lib/mongoid/errors/invalid_encrypted_association.rb +28 -0
  30. data/lib/mongoid/errors/invalid_encryption_schema.rb +27 -0
  31. data/lib/mongoid/errors/no_encryption_schema.rb +5 -3
  32. data/lib/mongoid/errors.rb +2 -0
  33. data/lib/mongoid/factory.rb +1 -1
  34. data/lib/mongoid/fields/encrypted.rb +14 -3
  35. data/lib/mongoid/fields/foreign_key.rb +9 -0
  36. data/lib/mongoid/fields/standard.rb +10 -0
  37. data/lib/mongoid/fields/validators/macro.rb +29 -0
  38. data/lib/mongoid/fields.rb +49 -2
  39. data/lib/mongoid/persistable/creatable.rb +120 -14
  40. data/lib/mongoid/persistable.rb +15 -15
  41. data/lib/mongoid/persistence_context.rb +46 -4
  42. data/lib/mongoid/reloadable.rb +1 -0
  43. data/lib/mongoid/search_indexable.rb +5 -1
  44. data/lib/mongoid/tasks/database.rb +10 -7
  45. data/lib/mongoid/touchable.rb +28 -1
  46. data/lib/mongoid/version.rb +1 -1
  47. data/lib/mongoid/warnings.rb +4 -0
  48. data/spec/integration/associations/embedded_delayed_atomic_sets_spec.rb +216 -0
  49. data/spec/integration/associations/has_and_belongs_to_many_spec.rb +14 -0
  50. data/spec/integration/atomic/modifiers_spec.rb +283 -0
  51. data/spec/integration/encryption_spec.rb +418 -0
  52. data/spec/mongoid/association/accessors_spec.rb +35 -0
  53. data/spec/mongoid/association/referenced/belongs_to/buildable_spec.rb +35 -6
  54. data/spec/mongoid/association/referenced/encrypted_foreign_key_spec.rb +142 -0
  55. data/spec/mongoid/association/referenced/has_and_belongs_to_many/buildable_spec.rb +36 -5
  56. data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
  57. data/spec/mongoid/association/referenced/has_many/buildable_spec.rb +24 -6
  58. data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +81 -0
  59. data/spec/mongoid/association/referenced/has_one/buildable_spec.rb +18 -3
  60. data/spec/mongoid/atomic/modifiers_spec.rb +347 -0
  61. data/spec/mongoid/attributes/nested_spec.rb +29 -0
  62. data/spec/mongoid/attributes/readonly_dotted_spec.rb +334 -0
  63. data/spec/mongoid/attributes/readonly_spec.rb +389 -0
  64. data/spec/mongoid/changeable_spec.rb +18 -0
  65. data/spec/mongoid/clients/factory_spec.rb +36 -0
  66. data/spec/mongoid/config/encryption_spec.rb +450 -0
  67. data/spec/mongoid/contextual/none_spec.rb +18 -0
  68. data/spec/mongoid/criteria/includable_spec.rb +346 -0
  69. data/spec/mongoid/criteria_spec.rb +17 -0
  70. data/spec/mongoid/encryptable_spec.rb +54 -0
  71. data/spec/mongoid/factory_spec.rb +8 -0
  72. data/spec/mongoid/fields_spec.rb +37 -0
  73. data/spec/mongoid/findable_spec.rb +14 -0
  74. data/spec/mongoid/scopable_spec.rb +102 -0
  75. data/spec/mongoid/search_indexable_spec.rb +26 -0
  76. data/spec/mongoid/tasks/database_spec.rb +13 -0
  77. data/spec/mongoid/touchable_spec.rb +217 -0
  78. data/spec/mongoid/touchable_spec_models.rb +37 -0
  79. data/spec/support/crypt/models.rb +138 -0
  80. data/spec/support/models/delete_all_account.rb +10 -0
  81. data/spec/support/models/delete_all_item.rb +9 -0
  82. data/spec/support/models/delete_all_scoped_item.rb +11 -0
  83. data/spec/support/models/delete_all_tag.rb +7 -0
  84. data/spec/support/models/modifier_models.rb +65 -0
  85. metadata +2045 -2008
@@ -77,6 +77,30 @@ describe Mongoid::Attributes::Readonly do
77
77
  it 'persists the third readonly value' do
78
78
  expect(person.reload.aliased_timestamp).to eq(Time.at(42))
79
79
  end
80
+
81
+ it 'reports the readonly value as a saved change' do
82
+ expect(person.previous_changes['title']).to eq([ nil, 'sir' ])
83
+ expect(person.saved_change_to_attribute('title')).to eq([ nil, 'sir' ])
84
+ expect(person.saved_change_to_attribute?(:title)).to be true
85
+ expect(person.title_previously_changed?).to be true
86
+ end
87
+
88
+ it 'reports the saved change for an aliased readonly field' do
89
+ expect(person.previous_changes['at']).to eq([ nil, Time.at(42) ])
90
+ expect(person.saved_change_to_attribute?(:aliased_timestamp)).to be true
91
+ end
92
+
93
+ it 'does not persist a later change to the readonly field' do
94
+ person.title = 'mr'
95
+ person.score = 1
96
+ person.save!
97
+
98
+ expect(person.previous_changes).not_to have_key('title')
99
+ expect(person.previous_changes).to have_key('score')
100
+
101
+ expect(person.reload.title).to eq('sir')
102
+ expect(person.reload.score).to eq(1)
103
+ end
80
104
  end
81
105
 
82
106
  context 'when updating an existing readonly field' do
@@ -213,6 +237,371 @@ describe Mongoid::Attributes::Readonly do
213
237
  end
214
238
  end
215
239
 
240
+ context 'when modifying a readonly field in place' do
241
+ let(:attributes) do
242
+ %i[map aliases array]
243
+ end
244
+
245
+ let(:person) do
246
+ Person.create!(map: { 'enabled' => false }, aliases: [ 'original' ], array: [ 'original' ], title: 'old')
247
+ end
248
+
249
+ it 'does not persist changes to a readonly hash' do
250
+ person.map['enabled'] = true
251
+ person.save!
252
+
253
+ expect(person.reload.map).to eq('enabled' => false)
254
+ end
255
+
256
+ it 'does not mark the document as changed' do
257
+ person.map['enabled'] = true
258
+
259
+ expect(person).not_to be_changed
260
+ end
261
+
262
+ it 'does not mark an aliased readonly field as changed' do
263
+ person.array << 'added'
264
+
265
+ expect(person).not_to be_changed
266
+ end
267
+
268
+ it 'allows in-place changes before the document is created' do
269
+ new_person = Person.new(map: { 'enabled' => false })
270
+ new_person.map['enabled'] = true
271
+ new_person.save!
272
+
273
+ expect(new_person.reload.map).to eq('enabled' => true)
274
+ end
275
+
276
+ it 'does not report the ignored change as saved' do
277
+ person.map['enabled'] = true
278
+ person.title = 'new'
279
+ person.save!
280
+
281
+ expect(person.previous_changes).not_to have_key('map')
282
+ expect(person.previous_changes).to have_key('title')
283
+ end
284
+
285
+ it 'does not update timestamps for an ignored change' do
286
+ model = test_model do
287
+ include Mongoid::Timestamps
288
+
289
+ field :settings, type: Hash
290
+ attr_readonly :settings
291
+ end
292
+ document = Timecop.freeze(Time.utc(2020, 1, 1)) do
293
+ model.create!(settings: { 'enabled' => false })
294
+ end
295
+
296
+ document.settings['enabled'] = true
297
+ Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
298
+
299
+ expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
300
+ end
301
+
302
+ it 'does not persist changes made through attributes' do
303
+ person.map
304
+ person.attributes['map']['enabled'] = true
305
+ person.save!
306
+
307
+ expect(person.reload.map).to eq('enabled' => false)
308
+ end
309
+
310
+ it 'resets an in-place change to a readonly hash' do
311
+ person.map['enabled'] = true
312
+ person.reset_map!
313
+
314
+ expect(person.map).to eq('enabled' => false)
315
+ expect(person).not_to be_changed
316
+ end
317
+
318
+ context 'when a tracked field becomes readonly' do
319
+ let(:model) do
320
+ test_model do
321
+ include Mongoid::Timestamps
322
+
323
+ field :settings, type: Hash
324
+ field :title
325
+ end
326
+ end
327
+
328
+ let(:document) do
329
+ Timecop.freeze(Time.utc(2020, 1, 1)) do
330
+ model.create!(settings: { 'enabled' => false }, title: 'old')
331
+ end
332
+ end
333
+
334
+ before do
335
+ document.settings['enabled'] = true
336
+ model.attr_readonly :settings
337
+ end
338
+
339
+ it 'does not persist the ignored change' do
340
+ document.save!
341
+
342
+ expect(document.reload.settings).to eq('enabled' => false)
343
+ end
344
+
345
+ it 'does not report the ignored change as dirty' do
346
+ expect(document).not_to be_changed
347
+ expect(document.changes).not_to have_key('settings')
348
+ end
349
+
350
+ it 'does not report the readonly field as changed' do
351
+ expect(document.settings_changed?).to be false
352
+ end
353
+
354
+ it 'does not report the ignored change as saved' do
355
+ document.title = 'new'
356
+ document.save!
357
+
358
+ expect(document.previous_changes).not_to have_key('settings')
359
+ expect(document.saved_change_to_attribute?(:settings)).to be false
360
+ expect(document.previous_changes).to have_key('title')
361
+ end
362
+
363
+ it 'does not update timestamps for the ignored change' do
364
+ Timecop.freeze(Time.utc(2020, 1, 2)) { document.save! }
365
+
366
+ expect(document.reload.updated_at).to eq(Time.utc(2020, 1, 1))
367
+ end
368
+ end
369
+
370
+ it 'does not persist changes to a readonly array' do
371
+ person.aliases << 'added'
372
+ person.save!
373
+
374
+ expect(person.reload.aliases).to eq([ 'original' ])
375
+ end
376
+
377
+ it 'does not persist changes to an aliased readonly field' do
378
+ person.array << 'added'
379
+ person.save!
380
+
381
+ expect(person.reload.array).to eq([ 'original' ])
382
+ end
383
+
384
+ it 'persists changes to other fields' do
385
+ person.map['enabled'] = true
386
+ person.title = 'new'
387
+ person.save!
388
+
389
+ expect(person.reload.map).to eq('enabled' => false)
390
+ expect(person.title).to eq('new')
391
+ end
392
+
393
+ it 'still allows atomic set on a readonly field' do
394
+ person.set('map.enabled' => true)
395
+
396
+ expect(person.reload.map).to eq('enabled' => true)
397
+ end
398
+
399
+ it 'still allows atomic unset on a readonly field' do
400
+ person.unset('map.enabled')
401
+
402
+ expect(person.reload.map).to eq({})
403
+ end
404
+
405
+ context 'when an atomic array update fails' do
406
+ it 'restores the readonly array in memory and in the database' do
407
+ expect do
408
+ person.atomically do
409
+ person.push(aliases: 'added')
410
+ raise 'abort'
411
+ end
412
+ end.to raise_error('abort')
413
+
414
+ expect(Person.collection.find(_id: person.id).first['aliases']).to eq([ 'original' ])
415
+ expect(person.aliases).to eq([ 'original' ])
416
+ end
417
+
418
+ it 'still allows a successful atomic update' do
419
+ person.atomically { person.push(aliases: 'added') }
420
+
421
+ expect(person.reload.aliases).to eq(%w[original added])
422
+ end
423
+ end
424
+ end
425
+
426
+ context 'when an embedded document has a readonly hash' do
427
+ let(:attributes) do
428
+ :title
429
+ end
430
+
431
+ let(:person) do
432
+ Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
433
+ end
434
+
435
+ around do |example|
436
+ original = Address.readonly_attributes
437
+ Address.attr_readonly :map
438
+ example.run
439
+ ensure
440
+ Address.readonly_attributes = original
441
+ end
442
+
443
+ it 'allows the initial value to be saved' do
444
+ expect(person.reload.addresses.first.map).to eq('enabled' => false)
445
+ end
446
+
447
+ it 'ignores in-place changes when saving the parent' do
448
+ person.addresses.first.map['enabled'] = true
449
+ person.save!
450
+
451
+ expect(person.reload.addresses.first.map).to eq('enabled' => false)
452
+ end
453
+
454
+ it 'ignores in-place changes when saving the embedded document' do
455
+ address = person.addresses.first
456
+ address.map['enabled'] = true
457
+ address.save!
458
+
459
+ expect(person.reload.addresses.first.map).to eq('enabled' => false)
460
+ end
461
+
462
+ it 'allows an initial value when adding a new embedded document' do
463
+ person.addresses.build(street: 'Second', map: { 'enabled' => true })
464
+ person.save!
465
+
466
+ expect(person.reload.addresses.last.map).to eq('enabled' => true)
467
+ end
468
+
469
+ it 'reports the initial value as a saved change' do
470
+ address = person.addresses.first
471
+
472
+ expect(address.previous_changes['map']).to eq([ nil, { 'enabled' => false } ])
473
+ expect(address.saved_change_to_attribute?(:map)).to be true
474
+ end
475
+ end
476
+
477
+ context 'when a persisted root document is converted with becomes' do
478
+ let(:attributes) do
479
+ :title
480
+ end
481
+
482
+ let(:model) do
483
+ test_model do
484
+ field :settings, type: Hash
485
+ attr_readonly :settings
486
+ end
487
+ end
488
+
489
+ let(:original) do
490
+ model.create!(settings: { 'enabled' => false })
491
+ end
492
+
493
+ it 'does not persist an in-place change to the readonly field of the became root document' do
494
+ became = original.becomes(model)
495
+ became.settings['enabled'] = true
496
+ became.save!
497
+
498
+ expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
499
+ end
500
+ end
501
+
502
+ context 'when a persisted document is converted to another class with becomes' do
503
+ let(:attributes) do
504
+ :title
505
+ end
506
+
507
+ let(:model) do
508
+ test_model do
509
+ field :settings, type: Hash
510
+ attr_readonly :settings
511
+ end
512
+ end
513
+
514
+ let(:subclass) do
515
+ Class.new(model)
516
+ end
517
+
518
+ let(:original) do
519
+ model.create!(settings: { 'enabled' => false })
520
+ end
521
+
522
+ it 'does not persist an in-place change to the readonly field of the became document' do
523
+ became = original.becomes(subclass)
524
+ became.settings['enabled'] = true
525
+ became.save!
526
+
527
+ expect(model.collection.find(_id: original.id).first['settings']).to eq('enabled' => false)
528
+ end
529
+ end
530
+
531
+ context 'when a persisted document with embedded documents is converted with becomes' do
532
+ let(:attributes) do
533
+ :title
534
+ end
535
+
536
+ let(:person) do
537
+ Person.create!(addresses: [ { street: 'Main', map: { 'enabled' => false } } ])
538
+ end
539
+
540
+ around do |example|
541
+ original = Address.readonly_attributes
542
+ Address.attr_readonly :map
543
+ example.run
544
+ ensure
545
+ Address.readonly_attributes = original
546
+ end
547
+
548
+ it 'does not persist an in-place change to the readonly field of a became embedded document' do
549
+ became = person.becomes(Person)
550
+ became.addresses.first.map['enabled'] = true
551
+ became.save!
552
+
553
+ stored = Person.collection.find(_id: person.id).first['addresses'].first['map']
554
+ expect(stored).to eq('enabled' => false)
555
+ end
556
+ end
557
+
558
+ context 'when a new document is converted with becomes' do
559
+ let(:attributes) do
560
+ :title
561
+ end
562
+
563
+ let(:model) do
564
+ test_model do
565
+ field :settings, type: Hash
566
+ attr_readonly :settings
567
+ end
568
+ end
569
+
570
+ it 'reports the readonly value as a saved change when the became document is created' do
571
+ became = model.new(settings: { 'enabled' => false }).becomes(model)
572
+ became.save!
573
+
574
+ expect(became.previous_changes['settings']).to eq([ nil, { 'enabled' => false } ])
575
+ expect(model.collection.find(_id: became.id).first['settings']).to eq('enabled' => false)
576
+ end
577
+ end
578
+
579
+ context 'when a field is named like an internal dirty tracking method' do
580
+ let(:attributes) do
581
+ :title
582
+ end
583
+
584
+ let(:model) do
585
+ test_model do
586
+ field :name, type: String
587
+ field :readonly_change_ignored, type: String
588
+ end
589
+ end
590
+
591
+ it 'tracks changes and creates the document' do
592
+ document = model.new(name: 'original', readonly_change_ignored: 'value')
593
+
594
+ expect(document.name_changed?).to be true
595
+
596
+ document.save!
597
+
598
+ expect(document.reload.name).to eq('original')
599
+ expect(document.readonly_change_ignored).to eq('value')
600
+ expect(document.readonly_change_ignored?).to be true
601
+ expect(document).not_to be_changed
602
+ end
603
+ end
604
+
216
605
  context 'when a foreign_key is defined as readonly' do
217
606
  let(:attributes) do
218
607
  :mother_id
@@ -1410,6 +1410,24 @@ describe Mongoid::Changeable do
1410
1410
  expect(person.title).to be_nil
1411
1411
  end
1412
1412
  end
1413
+
1414
+ context 'when a resizable attribute was read but its value has not changed' do
1415
+ let(:person) do
1416
+ Person.create!(map: { 'enabled' => false })
1417
+ end
1418
+
1419
+ it 'keeps the current value object so held references stay attached' do
1420
+ map = person.map
1421
+ person.reset_map!
1422
+
1423
+ expect(person.attributes['map']).to equal(map)
1424
+
1425
+ map['enabled'] = true
1426
+ person.save!
1427
+
1428
+ expect(person.reload.map).to eq('enabled' => true)
1429
+ end
1430
+ end
1413
1431
  end
1414
1432
 
1415
1433
  describe '#reset_attribute_to_default!' do
@@ -331,6 +331,42 @@ describe Mongoid::Clients::Factory do
331
331
  end
332
332
  end
333
333
  end
334
+
335
+ context 'when bypass_auto_encryption is set' do
336
+ it 'warns that encryption is bypassed' do
337
+ expect(Mongoid::Warnings).to receive(:warn_bypass_auto_encryption)
338
+
339
+ described_class.send(
340
+ :build_auto_encryption_options,
341
+ { auto_encryption_options: { bypass_auto_encryption: true, schema_map: {} } },
342
+ database_id
343
+ )
344
+ end
345
+ end
346
+
347
+ context 'when bypass_auto_encryption is not set' do
348
+ it 'does not warn' do
349
+ expect(Mongoid::Warnings).not_to receive(:warn_bypass_auto_encryption)
350
+
351
+ described_class.send(
352
+ :build_auto_encryption_options,
353
+ { auto_encryption_options: { schema_map: {} } },
354
+ database_id
355
+ )
356
+ end
357
+ end
358
+
359
+ context 'when bypass_auto_encryption is false' do
360
+ it 'does not warn' do
361
+ expect(Mongoid::Warnings).not_to receive(:warn_bypass_auto_encryption)
362
+
363
+ described_class.send(
364
+ :build_auto_encryption_options,
365
+ { auto_encryption_options: { bypass_auto_encryption: false, schema_map: {} } },
366
+ database_id
367
+ )
368
+ end
369
+ end
334
370
  end
335
371
 
336
372
  context 'when no name is provided' do