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
@@ -1208,6 +1208,35 @@ describe Mongoid::Fields do
1208
1208
  end
1209
1209
  end
1210
1210
 
1211
+ context 'when a subclass declares an encrypted field and the parent declares it as a plain field' do
1212
+ let(:parent) do
1213
+ klass = stub_const('PlainFieldParent', Class.new)
1214
+ klass.include(Mongoid::Document)
1215
+ klass
1216
+ end
1217
+
1218
+ let(:child) do
1219
+ parent
1220
+ klass = stub_const('PlainFieldChild', Class.new(parent))
1221
+ klass.field(:secret, type: String, encrypt: { deterministic: true })
1222
+ klass
1223
+ end
1224
+
1225
+ after do
1226
+ Mongoid.deregister_model(child)
1227
+ Mongoid.deregister_model(parent)
1228
+ end
1229
+
1230
+ it 'replaces the encrypted field on the subclass' do
1231
+ child
1232
+
1233
+ parent.field(:secret, type: String)
1234
+
1235
+ expect(parent.fields['secret']).not_to be_encrypted
1236
+ expect(child.fields['secret']).not_to be_encrypted
1237
+ end
1238
+ end
1239
+
1211
1240
  context 'custom options' do
1212
1241
  let(:handler) do
1213
1242
  proc {}
@@ -1814,6 +1843,14 @@ describe Mongoid::Fields do
1814
1843
  it { is_expected.to eq '' }
1815
1844
  end
1816
1845
 
1846
+ context 'when the returned name is mutated' do
1847
+ it 'does not corrupt the aliased field map' do
1848
+ Person.database_field_name('test') << '_mutated'
1849
+
1850
+ expect(Person.database_field_name('test')).to eq('t')
1851
+ end
1852
+ end
1853
+
1817
1854
  context 'given a String' do
1818
1855
  subject { Person.database_field_name(key.to_s) }
1819
1856
 
@@ -855,6 +855,20 @@ describe Mongoid::Findable do
855
855
  expect(criteria.size).to eq(0)
856
856
  end
857
857
  end
858
+
859
+ context 'when none is called on a criteria with a cached context' do
860
+ let(:criteria) do
861
+ Band.where(name: 'Depeche Mode')
862
+ end
863
+
864
+ it 'does not use the cached context' do
865
+ expect(criteria.count).to eq(1)
866
+
867
+ criteria.none
868
+
869
+ expect(criteria.count).to eq(0)
870
+ end
871
+ end
858
872
  end
859
873
 
860
874
  describe '.pluck' do
@@ -149,6 +149,90 @@ describe Mongoid::Scopable do
149
149
  end
150
150
  end
151
151
 
152
+ context 'when the default scope is none' do
153
+ before do
154
+ Band.create!(name: 'Depeche Mode', active: true)
155
+ Band.default_scope -> { Band.none }
156
+ end
157
+
158
+ after do
159
+ Band.default_scoping = nil
160
+ end
161
+
162
+ it 'does not return any documents' do
163
+ expect(Band.count).to eq(0)
164
+ expect(Band.all.to_a).to be_empty
165
+ expect(Band.where(name: 'Depeche Mode').first).to be_nil
166
+ end
167
+
168
+ it 'does not update documents' do
169
+ Band.update_all(active: false)
170
+ expect(Band.unscoped.first.active).to be(true)
171
+ end
172
+
173
+ it 'does not delete documents' do
174
+ expect(Band.delete_all).to eq(0)
175
+ expect(Band.unscoped.count).to eq(1)
176
+ end
177
+
178
+ it 'removes the none from the default scope when unscoping' do
179
+ crit = Band.all
180
+ expect(crit.count).to eq(0)
181
+ expect(crit.unscoped.count).to eq(1)
182
+ expect(Band.all.merge(Band.all).unscoped.count).to eq(1)
183
+ end
184
+
185
+ it 'keeps an explicit none when unscoping' do
186
+ expect(Band.none.unscoped.count).to eq(0)
187
+ expect(Band.all.none.unscoped.count).to eq(0)
188
+ expect(Band.all.merge(Band.none).unscoped.count).to eq(0)
189
+ end
190
+ end
191
+
192
+ context 'when the default scope is conditionally none' do
193
+ before do
194
+ Band.create!(name: 'Depeche Mode', active: true)
195
+ Band.default_scope -> { allow_all ? Band.where(active: true) : Band.none }
196
+ end
197
+
198
+ after do
199
+ Band.default_scoping = nil
200
+ end
201
+
202
+ context 'when the condition restricts the scope' do
203
+ let(:allow_all) { false }
204
+
205
+ it 'does not return any documents' do
206
+ expect(Band.count).to eq(0)
207
+ end
208
+ end
209
+
210
+ context 'when the condition allows all documents' do
211
+ let(:allow_all) { true }
212
+
213
+ it 'returns the documents' do
214
+ expect(Band.count).to eq(1)
215
+ end
216
+ end
217
+ end
218
+
219
+ context 'when multiple default scopes are combined with none last' do
220
+ before do
221
+ Band.create!(name: 'Depeche Mode', active: true)
222
+ Band.default_scope -> { Band.where(active: true) }
223
+ Band.default_scope -> { Band.none }
224
+ end
225
+
226
+ after do
227
+ Band.default_scoping = nil
228
+ end
229
+
230
+ it 'does not return any documents' do
231
+ expect(Band.count).to eq(0)
232
+ expect(Band.all.to_a).to be_empty
233
+ end
234
+ end
235
+
152
236
  context 'when parent class has default scope' do
153
237
  let(:selector) do
154
238
  AudibleSound.all.selector
@@ -872,6 +956,24 @@ describe Mongoid::Scopable do
872
956
  expect(Circle.visible.selector).to eq('radius' => 5)
873
957
  end
874
958
  end
959
+
960
+ context 'when the scope name matches an internal criteria helper' do
961
+ before do
962
+ Band.scope(:none_state, -> { where(active: true) })
963
+ Band.scope(:clear_default_scope_none, -> { where(origin: 'England') })
964
+ end
965
+
966
+ after do
967
+ remove_scope(Band, :none_state)
968
+ remove_scope(Band, :clear_default_scope_none)
969
+ end
970
+
971
+ it 'applies both scopes when chained on a criteria' do
972
+ selector = Band.where(name: 'Depeche Mode').none_state.clear_default_scope_none.selector
973
+
974
+ expect(selector).to eq('name' => 'Depeche Mode', 'active' => true, 'origin' => 'England')
975
+ end
976
+ end
875
977
  end
876
978
 
877
979
  describe '.scoped' do
@@ -118,6 +118,32 @@ describe Mongoid::SearchIndexable do
118
118
  end
119
119
  end
120
120
 
121
+ describe '.wait_for_search_indexes' do
122
+ let(:model) do
123
+ Class.new do
124
+ include Mongoid::Document
125
+
126
+ store_in collection: BSON::ObjectId.new.to_s
127
+ search_index :idx, mappings: { dynamic: false }
128
+ end
129
+ end
130
+
131
+ it 'reads the index status inside collection management' do
132
+ managing = nil
133
+ search_indexes = [ { 'name' => 'idx', 'queryable' => true, 'status' => 'READY' } ]
134
+ collection = instance_double(Mongo::Collection)
135
+ allow(collection).to receive(:search_indexes) do
136
+ managing = Mongoid::Threaded.managing_collection?
137
+ search_indexes
138
+ end
139
+ allow(model).to receive(:collection).and_return(collection)
140
+
141
+ model.wait_for_search_indexes([ 'idx' ])
142
+
143
+ expect(managing).to be true
144
+ end
145
+ end
146
+
121
147
  describe '.vector_search_index' do
122
148
  let(:vector_model) do
123
149
  Class.new do
@@ -254,6 +254,19 @@ describe Mongoid::Tasks::Database do
254
254
  described_class.create_search_indexes([ searchable_model_spy ], wait: false)
255
255
  end
256
256
  end
257
+
258
+ context 'when waiting for the indexes' do
259
+ it 'runs the wait inside collection management' do
260
+ managing = nil
261
+ allow(described_class).to receive(:wait_for_search_indexes) do
262
+ managing = Mongoid::Threaded.managing_collection?
263
+ end
264
+
265
+ described_class.create_search_indexes([ searchable_model_spy ], wait: true)
266
+
267
+ expect(managing).to be true
268
+ end
269
+ end
257
270
  end
258
271
 
259
272
  describe '.remove_search_indexes' do
@@ -1400,6 +1400,46 @@ describe Mongoid::Touchable do
1400
1400
  end
1401
1401
  end
1402
1402
 
1403
+ describe 'the parent chain touch paths' do
1404
+ context 'on a root document' do
1405
+ it 'contains the root updated_at path' do
1406
+ expect(TouchableParent.new._touchable_chain_paths).to eq(%w[updated_at])
1407
+ end
1408
+ end
1409
+
1410
+ context 'on an embedded document with a touchable parent' do
1411
+ let(:parent) do
1412
+ parent = TouchableParent.create!
1413
+ parent.child = TouchableChild.create!(parent: parent)
1414
+ parent
1415
+ end
1416
+
1417
+ it 'contains the paths of each node in the chain' do
1418
+ expect(parent.child._touchable_chain_paths)
1419
+ .to eq(%w[child.updated_at updated_at])
1420
+ end
1421
+ end
1422
+
1423
+ context 'with a custom touch field' do
1424
+ it 'contains the custom field path of the anchor document' do
1425
+ building = TouchableSpec::Embedded::Building.create!
1426
+
1427
+ expect(building._touchable_chain_paths(:last_used_at))
1428
+ .to eq(%w[updated_at last_used_at])
1429
+ end
1430
+ end
1431
+
1432
+ context 'when the models use Timestamps::Short' do
1433
+ it 'resolves the aliased timestamp fields' do
1434
+ building = TouchableSpec::Short::Building.create!
1435
+ building.floors.create!
1436
+
1437
+ expect(building.reload.floors.first._touchable_chain_paths)
1438
+ .to eq(%w[floors.0.u_at u_at])
1439
+ end
1440
+ end
1441
+ end
1442
+
1403
1443
  describe 'touch merged with embedded insert' do
1404
1444
  context 'when pushing an embedded document with touch: true' do
1405
1445
  let(:building) do
@@ -1424,6 +1464,21 @@ describe Mongoid::Touchable do
1424
1464
  end
1425
1465
  end
1426
1466
 
1467
+ it 'sends the merged touch updates rewritten to the positional operator' do
1468
+ subscriber = Mrss::EventSubscriber.new
1469
+ Mongoid.client(:default).subscribe(Mongo::Monitoring::COMMAND, subscriber)
1470
+ begin
1471
+ floor.sofas.push(TouchableSpec::Embedded::Sofa.new)
1472
+ ensure
1473
+ Mongoid.client(:default).unsubscribe(Mongo::Monitoring::COMMAND, subscriber)
1474
+ end
1475
+
1476
+ command = subscriber.single_command_started_event('update').command
1477
+ sets = command['updates'].first['u']['$set']
1478
+
1479
+ expect(sets.keys).to contain_exactly('floors.$.updated_at', 'updated_at')
1480
+ end
1481
+
1427
1482
  it 'updates updated_at on the parent after push' do
1428
1483
  original_floor_updated_at = floor.updated_at
1429
1484
  original_building_updated_at = building.updated_at
@@ -1536,5 +1591,167 @@ describe Mongoid::Touchable do
1536
1591
  expect(building.updated_at).to be > original_updated_at
1537
1592
  end
1538
1593
  end
1594
+
1595
+ context 'when an existing child of the same array has a pending touch' do
1596
+ let(:building) do
1597
+ TouchableSpec::Embedded::Building.create!(title: 'Tower')
1598
+ end
1599
+
1600
+ before do
1601
+ floor = building.floors.create!(level: 1)
1602
+ # Leave a pending touch on the existing sibling so merging it into
1603
+ # the insert would target a path inside the pushed array.
1604
+ floor.updated_at = Time.now + 60
1605
+ end
1606
+
1607
+ it 'does not raise a conflict error when creating a sibling' do
1608
+ expect { building.floors.create!(level: 2) }.not_to raise_error
1609
+ end
1610
+
1611
+ it 'persists the new sibling' do
1612
+ building.floors.create!(level: 2)
1613
+ expect(building.reload.floors.length).to eq(2)
1614
+ end
1615
+
1616
+ it 'touches the parent' do
1617
+ building.floors.create!(level: 2)
1618
+ building.reload
1619
+ expect(building.updated_at).to be_within(5).of(Time.now)
1620
+ end
1621
+ end
1622
+
1623
+ context 'when a sibling in a parent array has a pending touch' do
1624
+ let(:building) do
1625
+ TouchableSpec::Embedded::Building.create!(title: 'Tower')
1626
+ end
1627
+
1628
+ let(:first_floor) do
1629
+ building.floors.create!(level: 1)
1630
+ end
1631
+
1632
+ let(:second_floor) do
1633
+ building.floors.create!(level: 2)
1634
+ end
1635
+
1636
+ let(:pending_touch_time) do
1637
+ Time.now + 60
1638
+ end
1639
+
1640
+ before do
1641
+ first_floor
1642
+ # Leave a pending touch on the other floor, at a different index of
1643
+ # the same array. Rewriting both that touch and the insert's own
1644
+ # touch to the positional operator collapses them onto one path and
1645
+ # touches the wrong floor.
1646
+ second_floor.updated_at = pending_touch_time
1647
+ end
1648
+
1649
+ it 'does not raise when creating a grandchild under the other floor' do
1650
+ expect { first_floor.sofas.create! }.not_to raise_error
1651
+ end
1652
+
1653
+ it 'touches the floor the grandchild was inserted under' do
1654
+ first_floor.sofas.create!
1655
+ building.reload
1656
+ expect(building.floors[0].updated_at).to be_within(5).of(Time.now)
1657
+ end
1658
+
1659
+ it 'preserves the pending touch on the sibling floor' do
1660
+ first_floor.sofas.create!
1661
+ building.reload
1662
+ expect(building.floors[1].updated_at.to_f)
1663
+ .to be_within(1).of(pending_touch_time.to_f)
1664
+ end
1665
+ end
1666
+
1667
+ context 'when the parent touch option names a custom field' do
1668
+ let(:building) do
1669
+ TouchableSpec::Embedded::Building.create!(title: 'Tower')
1670
+ end
1671
+
1672
+ it 'writes the custom touch field when creating an embedded child' do
1673
+ # Materialize the parent before the freeze so its updated_at differs
1674
+ # from the touch time, forcing the touch updates to merge into the
1675
+ # child's insert.
1676
+ building
1677
+ Timecop.freeze(Time.utc(2026, 1, 2, 3, 4, 5)) do
1678
+ building.waiting_rooms.create!
1679
+ building.reload
1680
+ expect(building.last_used_at).not_to be_nil
1681
+ expect(building.last_used_at.to_f)
1682
+ .to be_within(1).of(Time.utc(2026, 1, 2, 3, 4, 5).to_f)
1683
+ end
1684
+ end
1685
+ end
1686
+
1687
+ context 'when the models use Timestamps::Short' do
1688
+ let(:building) do
1689
+ TouchableSpec::Short::Building.create!
1690
+ end
1691
+
1692
+ let(:floor) do
1693
+ building.floors.create!
1694
+ end
1695
+
1696
+ before do
1697
+ building
1698
+ floor
1699
+ end
1700
+
1701
+ it 'merges the touch updates into the insert round-trip' do
1702
+ expect_query(1) do
1703
+ floor.gadgets.push(TouchableSpec::Short::Gadget.new)
1704
+ end
1705
+ end
1706
+
1707
+ it 'touches the parent chain through the aliased timestamp fields' do
1708
+ original_building_updated_at = building.u_at
1709
+ original_floor_updated_at = floor.u_at
1710
+
1711
+ Timecop.travel(Time.now + 10) do
1712
+ floor.gadgets.push(TouchableSpec::Short::Gadget.new)
1713
+ end
1714
+
1715
+ building.reload
1716
+ expect(building.u_at).to be > original_building_updated_at
1717
+ expect(building.floors.first.u_at).to be > original_floor_updated_at
1718
+ end
1719
+ end
1720
+
1721
+ context 'when a sibling has a pending touch and the touch callback does not run' do
1722
+ let(:building) do
1723
+ TouchableSpec::Embedded::Building.create!(title: 'Tower')
1724
+ end
1725
+
1726
+ let(:first_floor) do
1727
+ building.floors.create!(level: 1)
1728
+ end
1729
+
1730
+ let(:second_floor) do
1731
+ building.floors.create!(level: 2)
1732
+ end
1733
+
1734
+ let(:pending_touch_time) do
1735
+ Time.now + 60
1736
+ end
1737
+
1738
+ before do
1739
+ first_floor
1740
+ # Leave a pending touch on the other floor: rewriting it and this
1741
+ # chain's own touch would collapse both onto the same positional
1742
+ # path, so the touch updates cannot be merged into the insert.
1743
+ second_floor.updated_at = pending_touch_time
1744
+ end
1745
+
1746
+ it 'persists the deferred touches at insert time' do
1747
+ Mongoid::Touchable.suppress_touch_callbacks(TouchableSpec::Embedded::Sofa.name) do
1748
+ first_floor.sofas.push(TouchableSpec::Embedded::Sofa.new)
1749
+ end
1750
+
1751
+ building.reload
1752
+ expect(building.floors[1].updated_at.to_f)
1753
+ .to be_within(1).of(pending_touch_time.to_f)
1754
+ end
1755
+ end
1539
1756
  end
1540
1757
  end
@@ -7,10 +7,12 @@ module TouchableSpec
7
7
  include Mongoid::Timestamps
8
8
 
9
9
  field :title, type: String
10
+ field :last_used_at, type: Time
10
11
 
11
12
  embeds_many :entrances, class_name: 'TouchableSpec::Embedded::Entrance'
12
13
  embeds_many :floors, class_name: 'TouchableSpec::Embedded::Floor'
13
14
  embeds_one :lobby, class_name: 'TouchableSpec::Embedded::Lobby'
15
+ embeds_many :waiting_rooms, class_name: 'TouchableSpec::Embedded::WaitingRoom'
14
16
  end
15
17
 
16
18
  class Lobby
@@ -75,6 +77,41 @@ module TouchableSpec
75
77
 
76
78
  embedded_in :floor, touch: true, class_name: 'TouchableSpec::Embedded::Floor'
77
79
  end
80
+
81
+ class WaitingRoom
82
+ include Mongoid::Document
83
+ include Mongoid::Timestamps
84
+
85
+ field :name, type: String
86
+
87
+ embedded_in :building, touch: :last_used_at,
88
+ class_name: 'TouchableSpec::Embedded::Building'
89
+ end
90
+ end
91
+
92
+ module Short
93
+ class Building
94
+ include Mongoid::Document
95
+ include Mongoid::Timestamps::Short
96
+
97
+ embeds_many :floors, class_name: 'TouchableSpec::Short::Floor'
98
+ end
99
+
100
+ class Floor
101
+ include Mongoid::Document
102
+ include Mongoid::Timestamps::Short
103
+
104
+ embedded_in :building, touch: true, class_name: 'TouchableSpec::Short::Building'
105
+
106
+ embeds_many :gadgets, class_name: 'TouchableSpec::Short::Gadget'
107
+ end
108
+
109
+ class Gadget
110
+ include Mongoid::Document
111
+ include Mongoid::Timestamps::Short
112
+
113
+ embedded_in :floor, touch: true, class_name: 'TouchableSpec::Short::Floor'
114
+ end
78
115
  end
79
116
 
80
117
  module Referenced
@@ -147,6 +147,48 @@ module Crypt
147
147
  embedded_in :account, class_name: 'Crypt::Account'
148
148
  end
149
149
 
150
+ # Embedded associations persisted under a store_as key that differs from
151
+ # the association name. The schema has to be keyed by the stored name.
152
+ class Dossier
153
+ include Mongoid::Document
154
+
155
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q=='
156
+
157
+ embeds_one :secret, class_name: 'Crypt::DossierSecret', store_as: :s
158
+ end
159
+
160
+ class DossierSecret
161
+ include Mongoid::Document
162
+
163
+ field :token, type: String, encrypt: { deterministic: true }
164
+
165
+ embedded_in :dossier, class_name: 'Crypt::Dossier'
166
+ end
167
+
168
+ # The same, one level down: both associations use store_as.
169
+ class Archive
170
+ include Mongoid::Document
171
+
172
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q=='
173
+
174
+ embeds_one :entry, class_name: 'Crypt::ArchiveEntry', store_as: :e
175
+ end
176
+
177
+ class ArchiveEntry
178
+ include Mongoid::Document
179
+
180
+ embedded_in :archive, class_name: 'Crypt::Archive'
181
+ embeds_one :payload, class_name: 'Crypt::ArchivePayload', store_as: :p
182
+ end
183
+
184
+ class ArchivePayload
185
+ include Mongoid::Document
186
+
187
+ field :token, type: String, encrypt: { deterministic: true }
188
+
189
+ embedded_in :entry, class_name: 'Crypt::ArchiveEntry'
190
+ end
191
+
150
192
  # A model that embeds itself. The walk over embedded relations has to stop
151
193
  # here, or generating the map never terminates.
152
194
  class Comment
@@ -203,4 +245,100 @@ module Crypt
203
245
 
204
246
  embeds_one :missing_note, class_name: 'Crypt::MissingNote'
205
247
  end
248
+
249
+ # Two unrelated models stored in one collection, each with its own encrypted
250
+ # field.
251
+ class Badge
252
+ include Mongoid::Document
253
+
254
+ store_in collection: 'crypt_badges'
255
+
256
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
257
+
258
+ field :serial, type: String, encrypt: true
259
+ end
260
+
261
+ class BadgeScan
262
+ include Mongoid::Document
263
+
264
+ store_in collection: 'crypt_badges'
265
+
266
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
267
+
268
+ field :holder, type: String, encrypt: true
269
+ end
270
+
271
+ # Single collection inheritance. Only Member is registered in
272
+ # Mongoid.models; the subclasses are not.
273
+ class Member
274
+ include Mongoid::Document
275
+
276
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
277
+
278
+ field :ssn, type: String, encrypt: true
279
+ end
280
+
281
+ class Employee < Member
282
+ field :salary, type: String, encrypt: true
283
+ end
284
+
285
+ class Contractor < Member
286
+ field :rate, type: String, encrypt: true
287
+ end
288
+
289
+ # Single collection inheritance where only the subclass is encrypted.
290
+ class Listing
291
+ include Mongoid::Document
292
+
293
+ field :title, type: String
294
+ end
295
+
296
+ class PrivateListing < Listing
297
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
298
+
299
+ field :owner_ssn, type: String, encrypt: true
300
+ end
301
+
302
+ # An embeds_one target whose subclass adds an encrypted field.
303
+ class Box
304
+ include Mongoid::Document
305
+
306
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
307
+
308
+ embeds_one :item, class_name: 'Crypt::Item'
309
+ end
310
+
311
+ class Item
312
+ include Mongoid::Document
313
+
314
+ field :label, type: String, encrypt: true
315
+
316
+ embedded_in :box, class_name: 'Crypt::Box'
317
+ end
318
+
319
+ class SealedItem < Item
320
+ field :seal, type: String, encrypt: true
321
+ end
322
+
323
+ # An embeds_one target with no encrypted field, whose subclass has one.
324
+ # Nothing on Locker or Content declares encryption.
325
+ class Locker
326
+ include Mongoid::Document
327
+
328
+ embeds_one :content, class_name: 'Crypt::Content'
329
+ end
330
+
331
+ class Content
332
+ include Mongoid::Document
333
+
334
+ field :label, type: String
335
+
336
+ embedded_in :locker, class_name: 'Crypt::Locker'
337
+ end
338
+
339
+ class SecretContent < Content
340
+ encrypt_with key_id: 'grolrnFVSSW9Gq04Q87R9Q==', deterministic: true
341
+
342
+ field :code, type: String, encrypt: true
343
+ end
206
344
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeleteAllAccount
4
+ include Mongoid::Document
5
+
6
+ has_many :items, class_name: 'DeleteAllItem', foreign_key: :account_id, inverse_of: :account,
7
+ scope: -> { where(:state.in => %w[draft open]) }
8
+ has_many :scoped_items, class_name: 'DeleteAllScopedItem', foreign_key: :account_id, inverse_of: :account
9
+ has_and_belongs_to_many :tags, class_name: 'DeleteAllTag', inverse_of: nil
10
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DeleteAllItem
4
+ include Mongoid::Document
5
+
6
+ field :state, type: String
7
+
8
+ belongs_to :account, class_name: 'DeleteAllAccount', inverse_of: :items
9
+ end