mongoid 8.1.13 → 8.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 519052cca069b6c2b2c3aa7cf6fc561b275b8df20824d3b587665ff64e9823d7
4
- data.tar.gz: 130d16306e79e262499c1faa2320c3adb0dabb5874b1d3053a950a3026324352
3
+ metadata.gz: 94d2ad5f41824649c0f1a9ef93f186536496f05709d2de0ae2cc0fe85e8628b4
4
+ data.tar.gz: 3741473e63e50fc2c18b5a5273602963400003852d7074ef5d17aaaf72783e30
5
5
  SHA512:
6
- metadata.gz: 37f2ee4e9b4f45e56c4c2552ab522b0f716548bc114d033a17eab6a84b451c4a9dc2d6c3b1e255b3e44fbbc9800ccfdb4aef5f7f25c4c4839650fd1e16c2c1cd
7
- data.tar.gz: 57f22359f24b877b4d58ace31759e8e0958dd149a0973d1e47359196e3aa702034582991068df5158b1930c5cb773ac88ec5da45afc44bd85c329461b359fb1b
6
+ metadata.gz: a4e87015d4625699e160617c6ce84f3fa59acbdc29a6589218e9feffd72e0a7594d8fdd342f8425a2ac1829fb0810fc2c8fa57fa910bc2e3a54f30eae57dfda8
7
+ data.tar.gz: f249211c86004dc162ba0e225e79c3b946dd167cf4bbc9fb591b252cd1790559df5acd7d0050d454ad1bca5ea40af129ed428396b078ee3e9f41a64c79f58d07
@@ -485,8 +485,10 @@ module Mongoid
485
485
  #
486
486
  # @return [ Integer ] The number of documents deleted.
487
487
  def remove_all(conditions = nil, method = :delete_all)
488
- selector = conditions || {}
489
- selector.merge!(criteria.selector)
488
+ # Conjoin the conditions with the association selector so that a
489
+ # key present in both is intersected rather than overwritten. This
490
+ # also leaves the caller's Hash alone.
491
+ selector = criteria.where(conditions || {}).selector
490
492
 
491
493
  # Scanning before the delete means scanning only what is already in
492
494
  # memory, so it is only done where there is a pattern whose cost has
@@ -22,7 +22,25 @@ module Mongoid
22
22
  # @return [ true | false ] If the document is new, or if the field is not
23
23
  # readonly.
24
24
  def attribute_writable?(name)
25
- new_record? || (!self.class.readonly_attributes.include?(name) && _loaded?(name))
25
+ return true if new_record?
26
+
27
+ readonly = self.class.readonly_attributes
28
+ return _loaded?(name) if readonly.empty?
29
+
30
+ path = name.to_s
31
+ return false if readonly.include?(path)
32
+
33
+ readonly.each do |field|
34
+ # A readonly attribute that is a declared field with a dotted name
35
+ # is that literal field, not a path prefix.
36
+ next if field.include?('.') && self.class.fields.key?(field)
37
+
38
+ # A path that is a declared field with a dotted name addresses that
39
+ # literal field, not a descendant of the readonly attribute.
40
+ return false if path.start_with?("#{field}.") && !self.class.fields.key?(path)
41
+ end
42
+
43
+ _loaded?(name)
26
44
  end
27
45
 
28
46
  private
@@ -74,6 +74,7 @@ module Mongoid
74
74
  @previous_attributes = attributes.dup
75
75
  reset_atomic_updates!
76
76
  changed_attributes.clear
77
+ @pre_create_changed_attributes = nil
77
78
  end
78
79
 
79
80
  # Things that need to execute after a document has been persisted.
@@ -244,6 +245,13 @@ module Mongoid
244
245
  def attribute_changed?(attr, **kwargs)
245
246
  attr = database_field_name(attr)
246
247
  return false unless changed_attributes.key?(attr)
248
+ # An in-place change to a readonly attribute of a persisted document
249
+ # is not part of the dirty tracking API and will not be persisted. A
250
+ # change tracked while the document was new initializes the document,
251
+ # and new documents may set readonly attributes, so it is still
252
+ # reported as a saved change.
253
+ return false if !new_record? && self.class.readonly_attributes.include?(attr) &&
254
+ !@pre_create_changed_attributes&.include?(attr)
247
255
  return false if changed_attributes[attr] == attributes[attr]
248
256
  if kwargs.key?(:from)
249
257
  return false if changed_attributes[attr] != kwargs[:from]
@@ -308,9 +316,16 @@ module Mongoid
308
316
  #
309
317
  # @return [ Object ] The old value.
310
318
  def attribute_will_change!(attr)
311
- unless changed_attributes.key?(attr)
312
- changed_attributes[attr] = read_raw_attribute(attr).__deep_copy__
313
- end
319
+ return if changed_attributes.key?(attr)
320
+
321
+ # The snapshot is taken for readonly attributes as well. Their changes
322
+ # are hidden from the dirty tracking API (see #attribute_changed?), but
323
+ # the snapshot is needed to reset an in-place change. Changes tracked
324
+ # while the document is new initialize it, and new documents may set
325
+ # readonly attributes, so they are remembered here and still reported
326
+ # when the document is created.
327
+ (@pre_create_changed_attributes ||= ::Set.new).add(attr) if new_record?
328
+ changed_attributes[attr] = read_raw_attribute(attr).__deep_copy__
314
329
  end
315
330
 
316
331
  # Set the attribute back to its old value.
@@ -323,7 +338,10 @@ module Mongoid
323
338
  # @return [ Object ] The old value.
324
339
  def reset_attribute!(attr)
325
340
  attr = database_field_name(attr)
326
- attributes[attr] = changed_attributes.delete(attr) if attribute_changed?(attr)
341
+ return unless changed_attributes.key?(attr)
342
+ return if changed_attributes[attr] == attributes[attr]
343
+
344
+ attributes[attr] = changed_attributes.delete(attr)
327
345
  end
328
346
 
329
347
  def reset_attribute_to_default!(attr)
@@ -81,7 +81,7 @@ module Mongoid
81
81
  # @return [ Array<Object> ] The distinct values for the field.
82
82
  def distinct(field)
83
83
  if Mongoid.legacy_pluck_distinct
84
- documents.map{ |doc| doc.send(field) }.uniq
84
+ documents.map { |doc| read_field_value(doc, field) }.uniq
85
85
  else
86
86
  pluck(field).uniq
87
87
  end
@@ -45,6 +45,31 @@ module Mongoid
45
45
  other.is_a?(None)
46
46
  end
47
47
 
48
+ # Delete all documents in the database that match the selector. There
49
+ # are no documents in the null context, so nothing is deleted.
50
+ #
51
+ # @example Delete all the documents.
52
+ # context.delete
53
+ #
54
+ # @return [ Integer ] Always zero.
55
+ def delete
56
+ 0
57
+ end
58
+ alias delete_all delete
59
+
60
+ # Destroy all documents in the database that match the selector. There
61
+ # are no documents in the null context, so nothing is destroyed and no
62
+ # callbacks are run.
63
+ #
64
+ # @example Destroy all the documents.
65
+ # context.destroy
66
+ #
67
+ # @return [ Integer ] Always zero.
68
+ def destroy
69
+ 0
70
+ end
71
+ alias destroy_all destroy
72
+
48
73
  # Get the distinct field values in null context.
49
74
  #
50
75
  # @example Get the distinct values in null context.
@@ -128,6 +153,32 @@ module Mongoid
128
153
  {}
129
154
  end
130
155
 
156
+ # Update the first matching document atomically. There are no documents
157
+ # in the null context, so nothing is updated.
158
+ #
159
+ # @example Update the matching document.
160
+ # context.update(name: "Smiths")
161
+ #
162
+ # @param [ Hash ] attributes The new attributes for the document.
163
+ #
164
+ # @return [ nil | false ] False if no attributes were provided.
165
+ def update(attributes = nil, _opts = {})
166
+ attributes ? nil : false
167
+ end
168
+
169
+ # Update all the matching documents atomically. There are no documents
170
+ # in the null context, so nothing is updated.
171
+ #
172
+ # @example Update all the matching documents.
173
+ # context.update_all(name: "Smiths")
174
+ #
175
+ # @param [ Hash ] attributes The new attributes for each document.
176
+ #
177
+ # @return [ nil | false ] False if no attributes were provided.
178
+ def update_all(attributes = nil, _opts = {})
179
+ attributes ? nil : false
180
+ end
181
+
131
182
  # Create the new null context.
132
183
  #
133
184
  # @example Create the new context.
@@ -11,9 +11,13 @@ module Mongoid
11
11
  #
12
12
  # @return [ Criteria ] The criteria.
13
13
  def apply_default_scope
14
+ explicit_none = none_state == true
14
15
  klass.without_default_scope do
15
16
  merge!(klass.default_scoping.call)
16
17
  end
18
+ # A none coming from the default scope is not an explicit none, so it
19
+ # may be removed by unscoping.
20
+ @none = :default if !explicit_none && @none
17
21
  self.scoping_options = true, false
18
22
  end
19
23
 
@@ -98,7 +102,9 @@ module Mongoid
98
102
  crit = clone
99
103
  unless unscoped?
100
104
  crit.scoping_options = false, true
101
- crit.selector.clear; crit.options.clear
105
+ crit.selector.clear
106
+ crit.options.clear
107
+ crit.send(:clear_default_scope_none)
102
108
  end
103
109
  crit
104
110
  end
@@ -256,6 +256,20 @@ module Mongoid
256
256
  self.documents = criteria.documents.dup unless criteria.documents.empty?
257
257
  self.scoping_options = criteria.scoping_options
258
258
  self.inclusions = (inclusions + criteria.inclusions).uniq
259
+ if criteria.empty_and_chainable?
260
+ # An explicit none wins over a none that only came from a default
261
+ # scope. Association proxies forward is_a? to their target, so the
262
+ # runtime class is checked instead; anything that is not a criteria is
263
+ # treated as an explicit none.
264
+ explicit = if Criteria === criteria # rubocop:disable Style/CaseEquality
265
+ criteria.send(:none_state) == true
266
+ else
267
+ true
268
+ end
269
+ @none = true if @none == true || explicit
270
+ @none ||= :default
271
+ @context = nil
272
+ end
259
273
  self
260
274
  end
261
275
 
@@ -267,7 +281,9 @@ module Mongoid
267
281
  #
268
282
  # @return [ Criteria ] The none criteria.
269
283
  def none
270
- @none = true and self
284
+ @none = true
285
+ @context = nil
286
+ self
271
287
  end
272
288
 
273
289
  # Is the criteria an empty but chainable criteria?
@@ -449,6 +465,34 @@ module Mongoid
449
465
 
450
466
  private
451
467
 
468
+ # The kind of none on this criteria, if any. An explicit none is +true+,
469
+ # a none coming only from the default scope is +:default+, and a criteria
470
+ # that is not none returns +nil+.
471
+ #
472
+ # @api private
473
+ #
474
+ # @example Get the none state.
475
+ # criteria.send(:none_state)
476
+ #
477
+ # @return [ true | :default | nil ] The none state.
478
+ def none_state
479
+ @none
480
+ end
481
+
482
+ # Removes the none flag if it was set only by the default scope. An
483
+ # explicitly requested none is never removed.
484
+ #
485
+ # @api private
486
+ #
487
+ # @example Clear a default scope none.
488
+ # criteria.send(:clear_default_scope_none)
489
+ #
490
+ # @return [ Criteria ] The criteria.
491
+ def clear_default_scope_none
492
+ @none = nil if @none == :default
493
+ self
494
+ end
495
+
452
496
  # Are documents in the query missing, and are we configured to raise an
453
497
  # error?
454
498
  #
@@ -193,6 +193,12 @@ module Mongoid
193
193
  new_errors.copy!(errors)
194
194
  became.instance_variable_set(:@errors, new_errors)
195
195
  became.instance_variable_set(:@new_record, new_record?)
196
+ # The document was constructed as new, collecting pre-create change
197
+ # markers. Drop them when the copied state is that of a persisted
198
+ # document: its pending changes were all tracked after the document
199
+ # was persisted, and stale markers would let in-place changes to
200
+ # readonly attributes bypass the dirty filter.
201
+ became.instance_variable_set(:@pre_create_changed_attributes, nil) unless new_record?
196
202
  became.instance_variable_set(:@destroyed, destroyed?)
197
203
  became.changed_attributes[klass.discriminator_key] = self.class.discriminator_value
198
204
  became[klass.discriminator_key] = klass.discriminator_value
@@ -203,6 +209,10 @@ module Mongoid
203
209
  relation = became.__send__(name)
204
210
  Array.wrap(relation).each do |r|
205
211
  r.instance_variable_set(:@new_record, new_record?)
212
+ # The embedded documents were also constructed as new; drop
213
+ # their pre-create change markers when they are marked as
214
+ # persisted, as is done for the parent document.
215
+ r.instance_variable_set(:@pre_create_changed_attributes, nil) unless new_record?
206
216
  end
207
217
  end
208
218
  end
@@ -27,6 +27,7 @@ module Mongoid
27
27
  @attributes = reloaded
28
28
  @attributes_before_type_cast = @attributes.dup
29
29
  @changed_attributes = {}
30
+ @pre_create_changed_attributes = nil
30
31
  @previous_changes = {}
31
32
  @previous_attributes = {}
32
33
  @previously_new_record = false
@@ -31,7 +31,7 @@ module Mongoid
31
31
  EXECUTE_CALLBACKS = '[mongoid]:execute-callbacks'
32
32
 
33
33
  # The key for the time left in the current in-memory regexp budget.
34
- REGEXP_BUDGET_KEY = 'regexp-budget'
34
+ REGEXP_BUDGET_KEY = "[mongoid]:regexp-budget"
35
35
 
36
36
  extend self
37
37
 
@@ -5,5 +5,5 @@ module Mongoid
5
5
  #
6
6
  # Note that this file is automatically updated via `rake candidate:create`.
7
7
  # Manual changes to this file will be overwritten by that rake task.
8
- VERSION = '8.1.13'
8
+ VERSION = '8.1.14'
9
9
  end
@@ -3796,4 +3796,52 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3796
3796
  expect(contract.signature_ids).to eq([signature.id])
3797
3797
  end
3798
3798
  end
3799
+
3800
+ # Conditions that share a key with the association selector must be
3801
+ # intersected with it, whatever kind of key the caller used.
3802
+ key_variants = {
3803
+ 'String' => :stringify_keys,
3804
+ 'Symbol' => :symbolize_keys,
3805
+ 'indifferent access' => :with_indifferent_access
3806
+ }.freeze
3807
+
3808
+ %i[ delete_all destroy_all ].each do |method|
3809
+ describe "##{method} with conditions on a key the association selector uses" do
3810
+ key_variants.each do |label, converter|
3811
+ context "when the keys are #{label}" do
3812
+ let(:draft) { DeleteAllTag.create!(state: 'draft') }
3813
+ let(:outside) { DeleteAllTag.create!(state: 'draft') }
3814
+ let(:account) { DeleteAllAccount.create!(tags: [ draft, DeleteAllTag.create!(state: 'open') ]) }
3815
+ let(:proxy) { DeleteAllAccount.find(account.id).tags }
3816
+
3817
+ before { outside }
3818
+
3819
+ context 'with a condition on the primary key' do
3820
+ let(:conditions) { { _id: outside.id }.send(converter) }
3821
+
3822
+ it 'deletes nothing outside the association' do
3823
+ expect(proxy.send(method, conditions)).to eq(0)
3824
+ expect(DeleteAllTag.count).to eq(3)
3825
+ end
3826
+
3827
+ it 'does not modify the conditions' do
3828
+ original = conditions.deep_dup
3829
+ proxy.send(method, conditions)
3830
+ expect(conditions).to eq(original)
3831
+ end
3832
+ end
3833
+
3834
+ context 'with a condition on the primary key inside the association' do
3835
+ let(:conditions) { { _id: draft.id }.send(converter) }
3836
+
3837
+ it 'deletes only the matching document' do
3838
+ expect(proxy.send(method, conditions)).to eq(1)
3839
+ expect(DeleteAllTag.pluck(:_id)).not_to include(draft.id)
3840
+ expect(DeleteAllTag.count).to eq(2)
3841
+ end
3842
+ end
3843
+ end
3844
+ end
3845
+ end
3846
+ end
3799
3847
  end
@@ -4292,4 +4292,87 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
4292
4292
  expect(agent.basic_ids).to eq([basic.id])
4293
4293
  end
4294
4294
  end
4295
+
4296
+ # Conditions that share a key with the association selector must be
4297
+ # intersected with it, whatever kind of key the caller used.
4298
+ key_variants = {
4299
+ 'String' => :stringify_keys,
4300
+ 'Symbol' => :symbolize_keys,
4301
+ 'indifferent access' => :with_indifferent_access
4302
+ }.freeze
4303
+
4304
+ %i[ delete_all destroy_all ].each do |method|
4305
+ describe "##{method} with conditions on a key the association selector uses" do
4306
+ let(:account) { DeleteAllAccount.create! }
4307
+ let(:other) { DeleteAllAccount.create! }
4308
+
4309
+ key_variants.each do |label, converter|
4310
+ context "when the keys are #{label}" do
4311
+ # rubocop:disable RSpec/MultipleMemoizedHelpers
4312
+ shared_examples 'an intersected bulk delete' do
4313
+ let(:reloaded) { DeleteAllAccount.find(account.id) }
4314
+ let(:proxy) { reloaded.send(association) }
4315
+ let(:conditions) { raw_conditions.send(converter) }
4316
+ let!(:expected) { DeleteAllAccount.find(account.id).send(association).where(raw_conditions).pluck(:_id) }
4317
+ let!(:everything) { klass.pluck(:_id) }
4318
+
4319
+ it 'deletes the same documents as the where chain' do
4320
+ expect(proxy.send(method, conditions)).to eq(expected.size)
4321
+ expect(klass.pluck(:_id)).to match_array(everything - expected)
4322
+ end
4323
+
4324
+ it 'does not modify the conditions' do
4325
+ original = conditions.deep_dup
4326
+ proxy.send(method, conditions)
4327
+ expect(conditions).to eq(original)
4328
+ end
4329
+ end
4330
+ # rubocop:enable RSpec/MultipleMemoizedHelpers
4331
+
4332
+ context 'with an association scope' do
4333
+ let(:klass) { DeleteAllItem }
4334
+ let(:association) { :items }
4335
+ let(:raw_conditions) { { state: 'draft' } }
4336
+
4337
+ before do
4338
+ 2.times { DeleteAllItem.create!(account: account, state: 'open') }
4339
+ DeleteAllItem.create!(account: account, state: 'draft')
4340
+ DeleteAllItem.create!(account: other, state: 'draft')
4341
+ DeleteAllItem.create!(account: account, state: 'closed')
4342
+ end
4343
+
4344
+ it_behaves_like 'an intersected bulk delete'
4345
+ end
4346
+
4347
+ context 'with a child default scope' do
4348
+ let(:klass) { DeleteAllScopedItem }
4349
+ let(:association) { :scoped_items }
4350
+ let(:raw_conditions) { { state: 'draft' } }
4351
+
4352
+ before do
4353
+ 2.times { DeleteAllScopedItem.create!(account: account, state: 'open') }
4354
+ DeleteAllScopedItem.create!(account: account, state: 'draft')
4355
+ DeleteAllScopedItem.create!(account: other, state: 'draft')
4356
+ DeleteAllScopedItem.collection.insert_one(account_id: account.id, state: 'closed')
4357
+ end
4358
+
4359
+ it_behaves_like 'an intersected bulk delete'
4360
+ end
4361
+
4362
+ context 'with a condition on the foreign key' do
4363
+ let(:klass) { DeleteAllItem }
4364
+ let(:association) { :items }
4365
+ let(:raw_conditions) { { account_id: other.id } }
4366
+
4367
+ before do
4368
+ 2.times { DeleteAllItem.create!(account: account, state: 'open') }
4369
+ DeleteAllItem.create!(account: other, state: 'open')
4370
+ end
4371
+
4372
+ it_behaves_like 'an intersected bulk delete'
4373
+ end
4374
+ end
4375
+ end
4376
+ end
4377
+ end
4295
4378
  end