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
@@ -86,27 +86,58 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Buildable do
86
86
  end
87
87
  end
88
88
 
89
- context 'when provided an array of hashes' do
90
- let(:object) do
89
+ context 'when provided preloaded documents' do
90
+ let(:attributes) do
91
91
  [
92
92
  { '_id' => BSON::ObjectId.new, 'name' => 'Emails' },
93
93
  { '_id' => BSON::ObjectId.new, 'name' => 'SMS' }
94
94
  ]
95
95
  end
96
96
 
97
+ let(:object) do
98
+ Mongoid::Association::PreloadedDocuments.new(attributes)
99
+ end
100
+
97
101
  let(:preference1) do
98
- Preference.new(object[0])
102
+ Preference.new(attributes[0])
99
103
  end
100
104
 
101
105
  let(:preference2) do
102
- Preference.new(object[1])
106
+ Preference.new(attributes[1])
103
107
  end
104
108
 
105
- it 'returns the documents' do
109
+ it 'returns the preloaded documents' do
106
110
  expect(documents).to eq([ preference1, preference2 ])
107
111
  end
108
112
  end
109
113
 
114
+ context 'when the foreign key is an array of hashes' do
115
+ let(:object) do
116
+ [
117
+ { '_id' => BSON::ObjectId.new, 'name' => 'Emails' },
118
+ { '_id' => BSON::ObjectId.new, 'name' => 'SMS' }
119
+ ]
120
+ end
121
+
122
+ it 'does not instantiate the key as preloaded documents' do
123
+ allow(association).to receive(:query_criteria).and_return(Preference.none)
124
+ expect(Mongoid::Factory).not_to receive(:execute_from_db)
125
+
126
+ documents
127
+ end
128
+ end
129
+
130
+ context 'when provided an empty array' do
131
+ let(:object) do
132
+ []
133
+ end
134
+
135
+ it 'returns an empty criteria' do
136
+ expect(documents).to be_a(Mongoid::Criteria)
137
+ expect(documents.empty_and_chainable?).to be true
138
+ end
139
+ end
140
+
110
141
  context 'when provided a object' do
111
142
  context 'when the object is not nil' do
112
143
  let(:object) do
@@ -3590,4 +3590,52 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3590
3590
  expect(contract.signature_ids).to eq([ signature.id ])
3591
3591
  end
3592
3592
  end
3593
+
3594
+ # Conditions that share a key with the association selector must be
3595
+ # intersected with it, whatever kind of key the caller used.
3596
+ key_variants = {
3597
+ 'String' => :stringify_keys,
3598
+ 'Symbol' => :symbolize_keys,
3599
+ 'indifferent access' => :with_indifferent_access
3600
+ }.freeze
3601
+
3602
+ %i[ delete_all destroy_all ].each do |method|
3603
+ describe "##{method} with conditions on a key the association selector uses" do
3604
+ key_variants.each do |label, converter|
3605
+ context "when the keys are #{label}" do
3606
+ let(:draft) { DeleteAllTag.create!(state: 'draft') }
3607
+ let(:outside) { DeleteAllTag.create!(state: 'draft') }
3608
+ let(:account) { DeleteAllAccount.create!(tags: [ draft, DeleteAllTag.create!(state: 'open') ]) }
3609
+ let(:proxy) { DeleteAllAccount.find(account.id).tags }
3610
+
3611
+ before { outside }
3612
+
3613
+ context 'with a condition on the primary key' do
3614
+ let(:conditions) { { _id: outside.id }.send(converter) }
3615
+
3616
+ it 'deletes nothing outside the association' do
3617
+ expect(proxy.send(method, conditions)).to eq(0)
3618
+ expect(DeleteAllTag.count).to eq(3)
3619
+ end
3620
+
3621
+ it 'does not modify the conditions' do
3622
+ original = conditions.deep_dup
3623
+ proxy.send(method, conditions)
3624
+ expect(conditions).to eq(original)
3625
+ end
3626
+ end
3627
+
3628
+ context 'with a condition on the primary key inside the association' do
3629
+ let(:conditions) { { _id: draft.id }.send(converter) }
3630
+
3631
+ it 'deletes only the matching document' do
3632
+ expect(proxy.send(method, conditions)).to eq(1)
3633
+ expect(DeleteAllTag.pluck(:_id)).not_to include(draft.id)
3634
+ expect(DeleteAllTag.count).to eq(2)
3635
+ end
3636
+ end
3637
+ end
3638
+ end
3639
+ end
3640
+ end
3593
3641
  end
@@ -102,15 +102,17 @@ describe Mongoid::Association::Referenced::HasMany::Buildable do
102
102
  end
103
103
  end
104
104
 
105
- context 'when provided an array of hashes' do
105
+ context 'when provided preloaded documents' do
106
106
  let(:object) do
107
- [
108
- { '_id' => BSON::ObjectId.new, 'title' => 'Post 1' },
109
- { '_id' => BSON::ObjectId.new, 'title' => 'Post 2' }
110
- ]
107
+ Mongoid::Association::PreloadedDocuments.new(
108
+ [
109
+ { '_id' => BSON::ObjectId.new, 'title' => 'Post 1' },
110
+ { '_id' => BSON::ObjectId.new, 'title' => 'Post 2' }
111
+ ]
112
+ )
111
113
  end
112
114
 
113
- it 'queries and returns the documents' do
115
+ it 'returns the preloaded documents' do
114
116
  for doc in documents
115
117
  expect(doc).to be_a(Post)
116
118
  end
@@ -118,6 +120,22 @@ describe Mongoid::Association::Referenced::HasMany::Buildable do
118
120
  end
119
121
  end
120
122
 
123
+ context 'when the key is an array of hashes' do
124
+ let(:object) do
125
+ [
126
+ { '_id' => BSON::ObjectId.new, 'title' => 'Post 1' },
127
+ { '_id' => BSON::ObjectId.new, 'title' => 'Post 2' }
128
+ ]
129
+ end
130
+
131
+ it 'does not instantiate the key as preloaded documents' do
132
+ allow(association).to receive(:query_criteria).and_return(Post.none)
133
+ expect(Mongoid::Factory).not_to receive(:execute_from_db)
134
+
135
+ documents
136
+ end
137
+ end
138
+
121
139
  context 'when provided a object' do
122
140
  let(:object) do
123
141
  [ Person.new ]
@@ -3537,4 +3537,85 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
3537
3537
  end
3538
3538
  end
3539
3539
  end
3540
+
3541
+ # Conditions that share a key with the association selector must be
3542
+ # intersected with it, whatever kind of key the caller used.
3543
+ key_variants = {
3544
+ 'String' => :stringify_keys,
3545
+ 'Symbol' => :symbolize_keys,
3546
+ 'indifferent access' => :with_indifferent_access
3547
+ }.freeze
3548
+
3549
+ %i[ delete_all destroy_all ].each do |method|
3550
+ describe "##{method} with conditions on a key the association selector uses" do
3551
+ let(:account) { DeleteAllAccount.create! }
3552
+ let(:other) { DeleteAllAccount.create! }
3553
+
3554
+ key_variants.each do |label, converter|
3555
+ context "when the keys are #{label}" do
3556
+ shared_examples 'an intersected bulk delete' do
3557
+ let(:reloaded) { DeleteAllAccount.find(account.id) }
3558
+ let(:proxy) { reloaded.send(association) }
3559
+ let(:conditions) { raw_conditions.send(converter) }
3560
+ let!(:expected) { DeleteAllAccount.find(account.id).send(association).where(raw_conditions).pluck(:_id) }
3561
+ let!(:everything) { klass.pluck(:_id) }
3562
+
3563
+ it 'deletes the same documents as the where chain' do
3564
+ expect(proxy.send(method, conditions)).to eq(expected.size)
3565
+ expect(klass.pluck(:_id)).to match_array(everything - expected)
3566
+ end
3567
+
3568
+ it 'does not modify the conditions' do
3569
+ original = conditions.deep_dup
3570
+ proxy.send(method, conditions)
3571
+ expect(conditions).to eq(original)
3572
+ end
3573
+ end
3574
+
3575
+ context 'with an association scope' do
3576
+ let(:klass) { DeleteAllItem }
3577
+ let(:association) { :items }
3578
+ let(:raw_conditions) { { state: 'draft' } }
3579
+
3580
+ before do
3581
+ 2.times { DeleteAllItem.create!(account: account, state: 'open') }
3582
+ DeleteAllItem.create!(account: account, state: 'draft')
3583
+ DeleteAllItem.create!(account: other, state: 'draft')
3584
+ DeleteAllItem.create!(account: account, state: 'closed')
3585
+ end
3586
+
3587
+ it_behaves_like 'an intersected bulk delete'
3588
+ end
3589
+
3590
+ context 'with a child default scope' do
3591
+ let(:klass) { DeleteAllScopedItem }
3592
+ let(:association) { :scoped_items }
3593
+ let(:raw_conditions) { { state: 'draft' } }
3594
+
3595
+ before do
3596
+ 2.times { DeleteAllScopedItem.create!(account: account, state: 'open') }
3597
+ DeleteAllScopedItem.create!(account: account, state: 'draft')
3598
+ DeleteAllScopedItem.create!(account: other, state: 'draft')
3599
+ DeleteAllScopedItem.collection.insert_one(account_id: account.id, state: 'closed')
3600
+ end
3601
+
3602
+ it_behaves_like 'an intersected bulk delete'
3603
+ end
3604
+
3605
+ context 'with a condition on the foreign key' do
3606
+ let(:klass) { DeleteAllItem }
3607
+ let(:association) { :items }
3608
+ let(:raw_conditions) { { account_id: other.id } }
3609
+
3610
+ before do
3611
+ 2.times { DeleteAllItem.create!(account: account, state: 'open') }
3612
+ DeleteAllItem.create!(account: other, state: 'open')
3613
+ end
3614
+
3615
+ it_behaves_like 'an intersected bulk delete'
3616
+ end
3617
+ end
3618
+ end
3619
+ end
3620
+ end
3540
3621
  end
@@ -75,17 +75,32 @@ describe Mongoid::Association::Referenced::HasOne::Buildable do
75
75
  end
76
76
  end
77
77
 
78
- context 'when provided an array of hashes' do
78
+ context 'when provided preloaded documents' do
79
79
  let(:object) do
80
- [ { '_id' => BSON::ObjectId.new, 'name' => 'Savings', 'balance' => 500 } ]
80
+ Mongoid::Association::PreloadedDocuments.new(
81
+ [ { '_id' => BSON::ObjectId.new, 'name' => 'Savings', 'balance' => 500 } ]
82
+ )
81
83
  end
82
84
 
83
- it 'returns the document' do
85
+ it 'returns the preloaded document' do
84
86
  expect(document.name).to eq('Savings')
85
87
  expect(document.balance).to eq(500)
86
88
  end
87
89
  end
88
90
 
91
+ context 'when the key is an array of hashes' do
92
+ let(:object) do
93
+ [ { '_id' => BSON::ObjectId.new, 'name' => 'Savings' } ]
94
+ end
95
+
96
+ it 'does not instantiate the key as a preloaded document' do
97
+ allow(association).to receive(:execute_query).and_return(nil)
98
+ expect(Mongoid::Factory).not_to receive(:execute_from_db)
99
+
100
+ document
101
+ end
102
+ end
103
+
89
104
  context 'when provided a object' do
90
105
  let(:object) do
91
106
  Account.new
@@ -74,6 +74,78 @@ describe Mongoid::Atomic::Modifiers do
74
74
  end
75
75
  end
76
76
  end
77
+
78
+ context 'when a conflicting modification exists' do
79
+ context 'when the conflicting modification is a set' do
80
+ let(:sets) do
81
+ { 'addresses.0.street' => 'Bond' }
82
+ end
83
+
84
+ let(:adds) do
85
+ { 'addresses.0.tags' => %w[tag] }
86
+ end
87
+
88
+ before do
89
+ modifiers.set(sets)
90
+ modifiers.add_to_set(adds)
91
+ end
92
+
93
+ it 'adds the add to set modifiers to the conflicts hash' do
94
+ expect(modifiers).to eq(
95
+ { '$set' => { 'addresses.0.street' => 'Bond' },
96
+ conflicts: { '$addToSet' =>
97
+ { 'addresses.0.tags' => { '$each' => %w[tag] } } } }
98
+ )
99
+ end
100
+ end
101
+
102
+ context 'when the conflicting modification is a push' do
103
+ let(:pushes) do
104
+ { 'addresses' => { 'street' => 'Oxford St' } }
105
+ end
106
+
107
+ let(:adds) do
108
+ { 'addresses.0.tags' => %w[tag] }
109
+ end
110
+
111
+ before do
112
+ modifiers.push(pushes)
113
+ modifiers.add_to_set(adds)
114
+ end
115
+
116
+ it 'adds the add to set modifiers to the conflicts hash' do
117
+ expect(modifiers).to eq(
118
+ { '$push' => { 'addresses' => { '$each' => [ { 'street' => 'Oxford St' } ] } },
119
+ conflicts: { '$addToSet' =>
120
+ { 'addresses.0.tags' => { '$each' => %w[tag] } } } }
121
+ )
122
+ end
123
+ end
124
+
125
+ context 'when the conflicting modification is a pull' do
126
+ let(:pulls) do
127
+ { 'addresses' => { '_id' => { '$in' => [ 'one' ] } } }
128
+ end
129
+
130
+ let(:adds) do
131
+ { 'addresses.0.tags' => %w[tag] }
132
+ end
133
+
134
+ before do
135
+ modifiers.pull(pulls)
136
+ modifiers.add_to_set(adds)
137
+ end
138
+
139
+ it 'does not split the operations, leaving MongoDB to reject the update' do
140
+ # A $pull shifts array elements, so applying the $addToSet in a
141
+ # separate write afterwards could target a different element.
142
+ expect(modifiers).to eq(
143
+ { '$pull' => { 'addresses' => { '_id' => { '$in' => [ 'one' ] } } },
144
+ '$addToSet' => { 'addresses.0.tags' => { '$each' => %w[tag] } } }
145
+ )
146
+ end
147
+ end
148
+ end
77
149
  end
78
150
 
79
151
  describe '#pull' do
@@ -125,6 +197,98 @@ describe Mongoid::Atomic::Modifiers do
125
197
  end
126
198
  end
127
199
  end
200
+
201
+ context 'when a conflicting modification exists' do
202
+ # A $pull shifts the positions of the remaining elements, so it stays in
203
+ # the main update. MongoDB rejects the update if the paths overlap.
204
+ context 'when the conflicting modification is a set' do
205
+ let(:sets) do
206
+ { 'addresses.0.street' => 'Bond' }
207
+ end
208
+
209
+ let(:pulls) do
210
+ { 'addresses' => { '_id' => { '$in' => [ 'one' ] } } }
211
+ end
212
+
213
+ before do
214
+ modifiers.set(sets)
215
+ modifiers.pull(pulls)
216
+ end
217
+
218
+ it 'keeps the pull modifiers in the main update' do
219
+ expect(modifiers).to eq(
220
+ { '$set' => { 'addresses.0.street' => 'Bond' },
221
+ '$pull' => { 'addresses' => { '_id' => { '$in' => [ 'one' ] } } } }
222
+ )
223
+ end
224
+ end
225
+
226
+ context 'when the conflicting modification is a push' do
227
+ let(:pushes) do
228
+ { 'addresses' => { 'street' => 'Oxford St' } }
229
+ end
230
+
231
+ let(:pulls) do
232
+ { 'addresses' => { '_id' => { '$in' => [ 'one' ] } } }
233
+ end
234
+
235
+ before do
236
+ modifiers.push(pushes)
237
+ modifiers.pull(pulls)
238
+ end
239
+
240
+ it 'keeps the pull modifiers in the main update' do
241
+ expect(modifiers).to eq(
242
+ { '$push' => { 'addresses' => { '$each' => [ { 'street' => 'Oxford St' } ] } },
243
+ '$pull' => { 'addresses' => { '_id' => { '$in' => [ 'one' ] } } } }
244
+ )
245
+ end
246
+ end
247
+
248
+ context 'when the conflicting modification is an add to set' do
249
+ let(:adds) do
250
+ { 'addresses.0.tags' => %w[tag] }
251
+ end
252
+
253
+ let(:pulls) do
254
+ { 'addresses' => { '_id' => { '$in' => [ 'one' ] } } }
255
+ end
256
+
257
+ before do
258
+ modifiers.add_to_set(adds)
259
+ modifiers.pull(pulls)
260
+ end
261
+
262
+ it 'keeps the pull modifiers in the main update' do
263
+ expect(modifiers).to eq(
264
+ { '$addToSet' => { 'addresses.0.tags' => { '$each' => %w[tag] } },
265
+ '$pull' => { 'addresses' => { '_id' => { '$in' => [ 'one' ] } } } }
266
+ )
267
+ end
268
+ end
269
+
270
+ context 'when the conflicting modification is a pull all' do
271
+ let(:pull_alls) do
272
+ { 'addresses' => [ { '_id' => 'one' } ] }
273
+ end
274
+
275
+ let(:pulls) do
276
+ { 'addresses' => { '_id' => { '$in' => [ 'one' ] } } }
277
+ end
278
+
279
+ before do
280
+ modifiers.pull_all(pull_alls)
281
+ modifiers.pull(pulls)
282
+ end
283
+
284
+ it 'keeps the pull modifiers in the main update' do
285
+ expect(modifiers).to eq(
286
+ { '$pullAll' => { 'addresses' => [ { '_id' => 'one' } ] },
287
+ '$pull' => { 'addresses' => { '_id' => { '$in' => [ 'one' ] } } } }
288
+ )
289
+ end
290
+ end
291
+ end
128
292
  end
129
293
 
130
294
  describe '#pull_all' do
@@ -183,6 +347,54 @@ describe Mongoid::Atomic::Modifiers do
183
347
  end
184
348
  end
185
349
  end
350
+
351
+ context 'when a conflicting modification exists' do
352
+ context 'when the conflicting modification is a pull' do
353
+ let(:pulls) do
354
+ { 'addresses' => { '_id' => { '$in' => [ 'one' ] } } }
355
+ end
356
+
357
+ let(:pull_alls) do
358
+ { 'addresses' => [ { '_id' => 'one' } ] }
359
+ end
360
+
361
+ before do
362
+ modifiers.pull(pulls)
363
+ modifiers.pull_all(pull_alls)
364
+ end
365
+
366
+ it 'adds the pull all modifiers to the conflicts hash' do
367
+ expect(modifiers).to eq(
368
+ { '$pull' => { 'addresses' => { '_id' => { '$in' => [ 'one' ] } } },
369
+ conflicts: { '$pullAll' =>
370
+ { 'addresses' => [ { '_id' => 'one' } ] } } }
371
+ )
372
+ end
373
+ end
374
+
375
+ context 'when the conflicting modification is a push' do
376
+ let(:pushes) do
377
+ { 'addresses' => { 'street' => 'Oxford St' } }
378
+ end
379
+
380
+ let(:pull_alls) do
381
+ { 'addresses' => [ { '_id' => 'one' } ] }
382
+ end
383
+
384
+ before do
385
+ modifiers.push(pushes)
386
+ modifiers.pull_all(pull_alls)
387
+ end
388
+
389
+ it 'adds the pull all modifiers to the conflicts hash' do
390
+ expect(modifiers).to eq(
391
+ { '$push' => { 'addresses' => { '$each' => [ { 'street' => 'Oxford St' } ] } },
392
+ conflicts: { '$pullAll' =>
393
+ { 'addresses' => [ { '_id' => 'one' } ] } } }
394
+ )
395
+ end
396
+ end
397
+ end
186
398
  end
187
399
 
188
400
  describe '#push' do
@@ -321,6 +533,56 @@ describe Mongoid::Atomic::Modifiers do
321
533
  )
322
534
  end
323
535
  end
536
+
537
+ context 'when the conflicting modification is an add to set' do
538
+ let(:adds) do
539
+ { 'addresses.0.tags' => %w[tag] }
540
+ end
541
+
542
+ let(:pushes) do
543
+ { 'addresses' => { 'street' => 'Oxford St' } }
544
+ end
545
+
546
+ before do
547
+ modifiers.add_to_set(adds)
548
+ modifiers.push(pushes)
549
+ end
550
+
551
+ it 'adds the push modifiers to the conflicts hash' do
552
+ expect(modifiers).to eq(
553
+ { '$addToSet' => { 'addresses.0.tags' => { '$each' => %w[tag] } },
554
+ conflicts: { '$push' =>
555
+ { 'addresses' => { '$each' => [
556
+ { 'street' => 'Oxford St' }
557
+ ] } } } }
558
+ )
559
+ end
560
+ end
561
+
562
+ context 'when the conflicting modification is an unset' do
563
+ let(:unsets) do
564
+ [ 'addresses.0.street' ]
565
+ end
566
+
567
+ let(:pushes) do
568
+ { 'addresses' => { 'street' => 'Oxford St' } }
569
+ end
570
+
571
+ before do
572
+ modifiers.unset(unsets)
573
+ modifiers.push(pushes)
574
+ end
575
+
576
+ it 'adds the push modifiers to the conflicts hash' do
577
+ expect(modifiers).to eq(
578
+ { '$unset' => { 'addresses.0.street' => true },
579
+ conflicts: { '$push' =>
580
+ { 'addresses' => { '$each' => [
581
+ { 'street' => 'Oxford St' }
582
+ ] } } } }
583
+ )
584
+ end
585
+ end
324
586
  end
325
587
 
326
588
  context 'when there is another operation on a sibling of a key which is nested' do
@@ -419,6 +681,30 @@ describe Mongoid::Atomic::Modifiers do
419
681
  )
420
682
  end
421
683
  end
684
+
685
+ context 'when the conflicting modification is an add to set' do
686
+ let(:adds) do
687
+ { 'addresses.0.tags' => %w[tag] }
688
+ end
689
+
690
+ let(:sets) do
691
+ { 'addresses.0.title' => 'Sir' }
692
+ end
693
+
694
+ before do
695
+ modifiers.add_to_set(adds)
696
+ modifiers.set(sets)
697
+ end
698
+
699
+ it 'adds the set modifiers to the conflicts hash' do
700
+ expect(modifiers).to eq(
701
+ { '$addToSet' =>
702
+ { 'addresses.0.tags' => { '$each' => %w[tag] } },
703
+ conflicts:
704
+ { '$set' => { 'addresses.0.title' => 'Sir' } } }
705
+ )
706
+ end
707
+ end
422
708
  end
423
709
  end
424
710
 
@@ -488,6 +774,67 @@ describe Mongoid::Atomic::Modifiers do
488
774
  end
489
775
  end
490
776
  end
777
+
778
+ context 'when the unsets conflict with a prior push' do
779
+ # A $push appends to the array and does not shift existing element
780
+ # positions, so the $unset can be applied safely in a separate write.
781
+ let(:pushes) { { 'books' => { 'title' => '1984' } } }
782
+
783
+ let(:unsets) { [ 'books.0.title' ] }
784
+
785
+ before do
786
+ modifiers.push(pushes)
787
+ modifiers.unset(unsets)
788
+ end
789
+
790
+ it 'adds the unset modifiers to the conflicts hash' do
791
+ expect(modifiers).to eq(
792
+ { '$push' => { 'books' => { '$each' => [ { 'title' => '1984' } ] } },
793
+ conflicts: { '$unset' => { 'books.0.title' => true } } }
794
+ )
795
+ end
796
+ end
797
+
798
+ context 'when the unsets conflict with a prior add to set' do
799
+ let(:adds) { { 'books.0.tags' => %w[tag] } }
800
+
801
+ let(:unsets) { [ 'books.0.title' ] }
802
+
803
+ before do
804
+ modifiers.add_to_set(adds)
805
+ modifiers.unset(unsets)
806
+ end
807
+
808
+ it 'adds the unset modifiers to the conflicts hash' do
809
+ expect(modifiers).to eq(
810
+ { '$addToSet' => { 'books.0.tags' => { '$each' => %w[tag] } },
811
+ conflicts: { '$unset' => { 'books.0.title' => true } } }
812
+ )
813
+ end
814
+ end
815
+
816
+ context 'when there is a pending pull on the same field' do
817
+ # A $pull shifts array elements, so routing the $unset into a separate
818
+ # write could make it target a different element. The operations are
819
+ # left together so MongoDB rejects the update (code 40) atomically.
820
+ let(:pulls) do
821
+ { 'books' => { '_id' => { '$in' => [ 'one' ] } } }
822
+ end
823
+
824
+ let(:unsets) { [ 'books.0.title' ] }
825
+
826
+ before do
827
+ modifiers.pull(pulls)
828
+ modifiers.unset(unsets)
829
+ end
830
+
831
+ it 'does not split the operations' do
832
+ expect(modifiers).to eq(
833
+ { '$pull' => { 'books' => { '_id' => { '$in' => [ 'one' ] } } },
834
+ '$unset' => { 'books.0.title' => true } }
835
+ )
836
+ end
837
+ end
491
838
  end
492
839
  end
493
840
  end