mongoid 8.0.13 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a9945b53ebb2236e5b44ef4cfead09b790b3b00881f62dba5c7007c1d99fdcba
4
- data.tar.gz: f092a211395767e35a20bd2492e9c9be56c3bc53721bb6e8b437c0ef4d4c3111
3
+ metadata.gz: 25cd7a90ef28bbf811d80c2f11edb549f48a9b038775b7487dec2a3c10a320a0
4
+ data.tar.gz: 4f3a7ab5cb3f2315d78631e8c8e7bc4007e17ebd26c9cda0fcc4d8bb8572458a
5
5
  SHA512:
6
- metadata.gz: 7fa47f5a3a012f20238d7813bc173c3a22b0f96e493bb391fc5d5d4cb5ffc37d50bd27959f2fa871f11b402ef739d43ab3ff044a7cd256ef4531b340bf1f882d
7
- data.tar.gz: 8bf1abb2f757cb18b92ccfae1214a7d19c6d6331e2ed30a4a39068eab365e8cf4cd0fc017810029106a4823ddb8a8a416a63de4faa334332e012f403167b7351
6
+ metadata.gz: 938ed265403968f8f571b3be0452dcebd51fd855c724023d438217fd2130e449e410e83b0babbe44aa6757aaaa2f01d906fd40fabdea7f6f558375ef2640ef12
7
+ data.tar.gz: b3975411c3e49d8549a9bc5ef145d819f76c563510037245065719c5767871fe1c3419fcba125568917b59d08c01e455655650f6bdf9846d0f2a3cff77ac2a04
@@ -476,8 +476,10 @@ module Mongoid
476
476
  #
477
477
  # @return [ Integer ] The number of documents deleted.
478
478
  def remove_all(conditions = nil, method = :delete_all)
479
- selector = conditions || {}
480
- selector.merge!(criteria.selector)
479
+ # Conjoin the conditions with the association selector so that a
480
+ # key present in both is intersected rather than overwritten. This
481
+ # also leaves the caller's Hash alone.
482
+ selector = criteria.where(conditions || {}).selector
481
483
 
482
484
  # Scanning before the delete means scanning only what is already in
483
485
  # 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? || (!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
@@ -72,6 +72,7 @@ module Mongoid
72
72
  @previous_attributes = attributes.dup
73
73
  reset_atomic_updates!
74
74
  changed_attributes.clear
75
+ @pre_create_changed_attributes = nil
75
76
  end
76
77
 
77
78
  # Things that need to execute after a document has been persisted.
@@ -164,6 +165,13 @@ module Mongoid
164
165
  def attribute_changed?(attr)
165
166
  attr = database_field_name(attr)
166
167
  return false unless changed_attributes.key?(attr)
168
+ # An in-place change to a readonly attribute of a persisted document
169
+ # is not part of the dirty tracking API and will not be persisted. A
170
+ # change tracked while the document was new initializes the document,
171
+ # and new documents may set readonly attributes, so it is still
172
+ # reported as a saved change.
173
+ return false if !new_record? && self.class.readonly_attributes.include?(attr) &&
174
+ !@pre_create_changed_attributes&.include?(attr)
167
175
  changed_attributes[attr] != attributes[attr]
168
176
  end
169
177
 
@@ -220,9 +228,16 @@ module Mongoid
220
228
  #
221
229
  # @return [ Object ] The old value.
222
230
  def attribute_will_change!(attr)
223
- unless changed_attributes.key?(attr)
224
- changed_attributes[attr] = read_raw_attribute(attr).__deep_copy__
225
- end
231
+ return if changed_attributes.key?(attr)
232
+
233
+ # The snapshot is taken for readonly attributes as well. Their changes
234
+ # are hidden from the dirty tracking API (see #attribute_changed?), but
235
+ # the snapshot is needed to reset an in-place change. Changes tracked
236
+ # while the document is new initialize it, and new documents may set
237
+ # readonly attributes, so they are remembered here and still reported
238
+ # when the document is created.
239
+ (@pre_create_changed_attributes ||= ::Set.new).add(attr) if new_record?
240
+ changed_attributes[attr] = read_raw_attribute(attr).__deep_copy__
226
241
  end
227
242
 
228
243
  # Set the attribute back to its old value.
@@ -235,7 +250,10 @@ module Mongoid
235
250
  # @return [ Object ] The old value.
236
251
  def reset_attribute!(attr)
237
252
  attr = database_field_name(attr)
238
- attributes[attr] = changed_attributes.delete(attr) if attribute_changed?(attr)
253
+ return unless changed_attributes.key?(attr)
254
+ return if changed_attributes[attr] == attributes[attr]
255
+
256
+ attributes[attr] = changed_attributes.delete(attr)
239
257
  end
240
258
 
241
259
  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.
@@ -119,6 +144,32 @@ module Mongoid
119
144
  {}
120
145
  end
121
146
 
147
+ # Update the first matching document atomically. There are no documents
148
+ # in the null context, so nothing is updated.
149
+ #
150
+ # @example Update the matching document.
151
+ # context.update(name: "Smiths")
152
+ #
153
+ # @param [ Hash ] attributes The new attributes for the document.
154
+ #
155
+ # @return [ nil | false ] False if no attributes were provided.
156
+ def update(attributes = nil, _opts = {})
157
+ attributes ? nil : false
158
+ end
159
+
160
+ # Update all the matching documents atomically. There are no documents
161
+ # in the null context, so nothing is updated.
162
+ #
163
+ # @example Update all the matching documents.
164
+ # context.update_all(name: "Smiths")
165
+ #
166
+ # @param [ Hash ] attributes The new attributes for each document.
167
+ #
168
+ # @return [ nil | false ] False if no attributes were provided.
169
+ def update_all(attributes = nil, _opts = {})
170
+ attributes ? nil : false
171
+ end
172
+
122
173
  # Create the new null context.
123
174
  #
124
175
  # @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?
@@ -448,6 +464,34 @@ module Mongoid
448
464
 
449
465
  private
450
466
 
467
+ # The kind of none on this criteria, if any. An explicit none is +true+,
468
+ # a none coming only from the default scope is +:default+, and a criteria
469
+ # that is not none returns +nil+.
470
+ #
471
+ # @api private
472
+ #
473
+ # @example Get the none state.
474
+ # criteria.send(:none_state)
475
+ #
476
+ # @return [ true | :default | nil ] The none state.
477
+ def none_state
478
+ @none
479
+ end
480
+
481
+ # Removes the none flag if it was set only by the default scope. An
482
+ # explicitly requested none is never removed.
483
+ #
484
+ # @api private
485
+ #
486
+ # @example Clear a default scope none.
487
+ # criteria.send(:clear_default_scope_none)
488
+ #
489
+ # @return [ Criteria ] The criteria.
490
+ def clear_default_scope_none
491
+ @none = nil if @none == :default
492
+ self
493
+ end
494
+
451
495
  # Are documents in the query missing, and are we configured to raise an
452
496
  # error?
453
497
  #
@@ -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.0.13'
8
+ VERSION = '8.0.14'
9
9
  end
@@ -3818,4 +3818,52 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
3818
3818
  expect(contract.signature_ids).to eq([signature.id])
3819
3819
  end
3820
3820
  end
3821
+
3822
+ # Conditions that share a key with the association selector must be
3823
+ # intersected with it, whatever kind of key the caller used.
3824
+ key_variants = {
3825
+ 'String' => :stringify_keys,
3826
+ 'Symbol' => :symbolize_keys,
3827
+ 'indifferent access' => :with_indifferent_access
3828
+ }.freeze
3829
+
3830
+ %i[ delete_all destroy_all ].each do |method|
3831
+ describe "##{method} with conditions on a key the association selector uses" do
3832
+ key_variants.each do |label, converter|
3833
+ context "when the keys are #{label}" do
3834
+ let(:draft) { DeleteAllTag.create!(state: 'draft') }
3835
+ let(:outside) { DeleteAllTag.create!(state: 'draft') }
3836
+ let(:account) { DeleteAllAccount.create!(tags: [ draft, DeleteAllTag.create!(state: 'open') ]) }
3837
+ let(:proxy) { DeleteAllAccount.find(account.id).tags }
3838
+
3839
+ before { outside }
3840
+
3841
+ context 'with a condition on the primary key' do
3842
+ let(:conditions) { { _id: outside.id }.send(converter) }
3843
+
3844
+ it 'deletes nothing outside the association' do
3845
+ expect(proxy.send(method, conditions)).to eq(0)
3846
+ expect(DeleteAllTag.count).to eq(3)
3847
+ end
3848
+
3849
+ it 'does not modify the conditions' do
3850
+ original = conditions.deep_dup
3851
+ proxy.send(method, conditions)
3852
+ expect(conditions).to eq(original)
3853
+ end
3854
+ end
3855
+
3856
+ context 'with a condition on the primary key inside the association' do
3857
+ let(:conditions) { { _id: draft.id }.send(converter) }
3858
+
3859
+ it 'deletes only the matching document' do
3860
+ expect(proxy.send(method, conditions)).to eq(1)
3861
+ expect(DeleteAllTag.pluck(:_id)).not_to include(draft.id)
3862
+ expect(DeleteAllTag.count).to eq(2)
3863
+ end
3864
+ end
3865
+ end
3866
+ end
3867
+ end
3868
+ end
3821
3869
  end
@@ -4291,4 +4291,87 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
4291
4291
  expect(agent.basic_ids).to eq([basic.id])
4292
4292
  end
4293
4293
  end
4294
+
4295
+ # Conditions that share a key with the association selector must be
4296
+ # intersected with it, whatever kind of key the caller used.
4297
+ key_variants = {
4298
+ 'String' => :stringify_keys,
4299
+ 'Symbol' => :symbolize_keys,
4300
+ 'indifferent access' => :with_indifferent_access
4301
+ }.freeze
4302
+
4303
+ %i[ delete_all destroy_all ].each do |method|
4304
+ describe "##{method} with conditions on a key the association selector uses" do
4305
+ let(:account) { DeleteAllAccount.create! }
4306
+ let(:other) { DeleteAllAccount.create! }
4307
+
4308
+ key_variants.each do |label, converter|
4309
+ context "when the keys are #{label}" do
4310
+ # rubocop:disable RSpec/MultipleMemoizedHelpers
4311
+ shared_examples 'an intersected bulk delete' do
4312
+ let(:reloaded) { DeleteAllAccount.find(account.id) }
4313
+ let(:proxy) { reloaded.send(association) }
4314
+ let(:conditions) { raw_conditions.send(converter) }
4315
+ let!(:expected) { DeleteAllAccount.find(account.id).send(association).where(raw_conditions).pluck(:_id) }
4316
+ let!(:everything) { klass.pluck(:_id) }
4317
+
4318
+ it 'deletes the same documents as the where chain' do
4319
+ expect(proxy.send(method, conditions)).to eq(expected.size)
4320
+ expect(klass.pluck(:_id)).to match_array(everything - expected)
4321
+ end
4322
+
4323
+ it 'does not modify the conditions' do
4324
+ original = conditions.deep_dup
4325
+ proxy.send(method, conditions)
4326
+ expect(conditions).to eq(original)
4327
+ end
4328
+ end
4329
+ # rubocop:enable RSpec/MultipleMemoizedHelpers
4330
+
4331
+ context 'with an association scope' do
4332
+ let(:klass) { DeleteAllItem }
4333
+ let(:association) { :items }
4334
+ let(:raw_conditions) { { state: 'draft' } }
4335
+
4336
+ before do
4337
+ 2.times { DeleteAllItem.create!(account: account, state: 'open') }
4338
+ DeleteAllItem.create!(account: account, state: 'draft')
4339
+ DeleteAllItem.create!(account: other, state: 'draft')
4340
+ DeleteAllItem.create!(account: account, state: 'closed')
4341
+ end
4342
+
4343
+ it_behaves_like 'an intersected bulk delete'
4344
+ end
4345
+
4346
+ context 'with a child default scope' do
4347
+ let(:klass) { DeleteAllScopedItem }
4348
+ let(:association) { :scoped_items }
4349
+ let(:raw_conditions) { { state: 'draft' } }
4350
+
4351
+ before do
4352
+ 2.times { DeleteAllScopedItem.create!(account: account, state: 'open') }
4353
+ DeleteAllScopedItem.create!(account: account, state: 'draft')
4354
+ DeleteAllScopedItem.create!(account: other, state: 'draft')
4355
+ DeleteAllScopedItem.collection.insert_one(account_id: account.id, state: 'closed')
4356
+ end
4357
+
4358
+ it_behaves_like 'an intersected bulk delete'
4359
+ end
4360
+
4361
+ context 'with a condition on the foreign key' do
4362
+ let(:klass) { DeleteAllItem }
4363
+ let(:association) { :items }
4364
+ let(:raw_conditions) { { account_id: other.id } }
4365
+
4366
+ before do
4367
+ 2.times { DeleteAllItem.create!(account: account, state: 'open') }
4368
+ DeleteAllItem.create!(account: other, state: 'open')
4369
+ end
4370
+
4371
+ it_behaves_like 'an intersected bulk delete'
4372
+ end
4373
+ end
4374
+ end
4375
+ end
4376
+ end
4294
4377
  end