mongoid 7.6.2 → 7.6.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.
- checksums.yaml +4 -4
- data/lib/mongoid/association/referenced/has_many/proxy.rb +4 -2
- data/lib/mongoid/attributes/readonly.rb +33 -3
- data/lib/mongoid/changeable.rb +22 -4
- data/lib/mongoid/contextual/none.rb +51 -0
- data/lib/mongoid/criteria/queryable/selectable.rb +15 -10
- data/lib/mongoid/criteria/scopable.rb +7 -1
- data/lib/mongoid/criteria.rb +45 -1
- data/lib/mongoid/document.rb +10 -0
- data/lib/mongoid/reloadable.rb +1 -0
- data/lib/mongoid/threaded.rb +1 -1
- data/lib/mongoid/version.rb +1 -1
- data/spec/integration/query_operator_guard_spec.rb +5 -0
- data/spec/mongoid/association/referenced/has_and_belongs_to_many/proxy_spec.rb +48 -0
- data/spec/mongoid/association/referenced/has_many/proxy_spec.rb +83 -0
- data/spec/mongoid/attributes/readonly_dotted_spec.rb +286 -0
- data/spec/mongoid/attributes/readonly_spec.rb +387 -0
- data/spec/mongoid/changeable_spec.rb +18 -0
- data/spec/mongoid/contextual/none_spec.rb +18 -0
- data/spec/mongoid/criteria/queryable/selectable_logical_spec.rb +27 -0
- data/spec/mongoid/criteria_spec.rb +17 -0
- data/spec/mongoid/findable_spec.rb +14 -0
- data/spec/mongoid/matcher/regexp_budget_spec.rb +6 -0
- data/spec/mongoid/scopable_spec.rb +116 -2
- data/spec/spec_helper.rb +8 -0
- data/spec/support/models/delete_all_account.rb +10 -0
- data/spec/support/models/delete_all_item.rb +9 -0
- data/spec/support/models/delete_all_scoped_item.rb +11 -0
- data/spec/support/models/delete_all_tag.rb +7 -0
- metadata +1817 -1790
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 392d6b1bb74f5a5a42854b8570d46f882832e03712899538e242031905f846cf
|
|
4
|
+
data.tar.gz: 692b7ed4b30243d0af63498ae24912a346b5720a094d54662f62ad818cace43d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3ff42af86c18207426f73c76d6c74142ad47f4e2aea68d2be60d1dd5e0bbc616419437e506515f7568195a322c0282a298c4b61f17716b0ac47d35983f6fae15
|
|
7
|
+
data.tar.gz: 2510038577fde763eeb486cd671b3960b1b2a0990fe43badec5ce2860686801d6801fb6fc9d78c0238ceb6402f67815ac748e9009b8ef07458965caf59d68f83
|
|
@@ -466,8 +466,10 @@ module Mongoid
|
|
|
466
466
|
#
|
|
467
467
|
# @return [ Integer ] The number of documents deleted.
|
|
468
468
|
def remove_all(conditions = nil, method = :delete_all)
|
|
469
|
-
|
|
470
|
-
|
|
469
|
+
# Conjoin the conditions with the association selector so that a
|
|
470
|
+
# key present in both is intersected rather than overwritten. This
|
|
471
|
+
# also leaves the caller's Hash alone.
|
|
472
|
+
selector = criteria.where(conditions || {}).selector
|
|
471
473
|
|
|
472
474
|
# Scanning before the delete means scanning only what is already in
|
|
473
475
|
# memory, so it is only done where there is a pattern whose cost has
|
|
@@ -22,13 +22,31 @@ 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
|
-
|
|
25
|
+
return true if new_record?
|
|
26
|
+
|
|
27
|
+
readonly = self.class.readonly_attributes
|
|
28
|
+
return _loaded?(name) if readonly.empty?
|
|
29
|
+
|
|
30
|
+
path = self.class.readonly_field_name(name)
|
|
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
|
|
29
47
|
|
|
30
48
|
def as_writable_attribute!(name, value = :nil)
|
|
31
|
-
normalized_name =
|
|
49
|
+
normalized_name = self.class.readonly_field_name(name)
|
|
32
50
|
if attribute_writable?(normalized_name)
|
|
33
51
|
yield(normalized_name)
|
|
34
52
|
else
|
|
@@ -64,9 +82,21 @@ module Mongoid
|
|
|
64
82
|
# @param [ Array<Symbol> ] names The names of the fields.
|
|
65
83
|
def attr_readonly(*names)
|
|
66
84
|
names.each do |name|
|
|
67
|
-
readonly_attributes <<
|
|
85
|
+
readonly_attributes << readonly_field_name(name)
|
|
68
86
|
end
|
|
69
87
|
end
|
|
88
|
+
|
|
89
|
+
# Returns the database field name for a readonly attribute, also
|
|
90
|
+
# resolving an aliased first segment of a dotted path.
|
|
91
|
+
#
|
|
92
|
+
# @api private
|
|
93
|
+
def readonly_field_name(name)
|
|
94
|
+
name = name.to_s
|
|
95
|
+
return database_field_name(name) if !name.include?('.') || fields.key?(name) || aliased_fields.key?(name)
|
|
96
|
+
|
|
97
|
+
head, tail = name.split('.', 2)
|
|
98
|
+
"#{aliased_fields.fetch(head, head)}.#{tail}"
|
|
99
|
+
end
|
|
70
100
|
end
|
|
71
101
|
end
|
|
72
102
|
end
|
data/lib/mongoid/changeable.rb
CHANGED
|
@@ -73,6 +73,7 @@ module Mongoid
|
|
|
73
73
|
send(update).clear
|
|
74
74
|
end
|
|
75
75
|
changed_attributes.clear
|
|
76
|
+
@pre_create_changed_attributes = nil
|
|
76
77
|
end
|
|
77
78
|
|
|
78
79
|
# Things that need to execute after a document has been persisted.
|
|
@@ -157,6 +158,13 @@ module Mongoid
|
|
|
157
158
|
def attribute_changed?(attr)
|
|
158
159
|
attr = database_field_name(attr)
|
|
159
160
|
return false unless changed_attributes.key?(attr)
|
|
161
|
+
# An in-place change to a readonly attribute of a persisted document
|
|
162
|
+
# is not part of the dirty tracking API and will not be persisted. A
|
|
163
|
+
# change tracked while the document was new initializes the document,
|
|
164
|
+
# and new documents may set readonly attributes, so it is still
|
|
165
|
+
# reported as a saved change.
|
|
166
|
+
return false if !new_record? && self.class.readonly_attributes.include?(attr) &&
|
|
167
|
+
!@pre_create_changed_attributes&.include?(attr)
|
|
160
168
|
changed_attributes[attr] != attributes[attr]
|
|
161
169
|
end
|
|
162
170
|
|
|
@@ -194,9 +202,16 @@ module Mongoid
|
|
|
194
202
|
#
|
|
195
203
|
# @return [ Object ] The old value.
|
|
196
204
|
def attribute_will_change!(attr)
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
205
|
+
return if changed_attributes.key?(attr)
|
|
206
|
+
|
|
207
|
+
# The snapshot is taken for readonly attributes as well. Their changes
|
|
208
|
+
# are hidden from the dirty tracking API (see #attribute_changed?), but
|
|
209
|
+
# the snapshot is needed to reset an in-place change. Changes tracked
|
|
210
|
+
# while the document is new initialize it, and new documents may set
|
|
211
|
+
# readonly attributes, so they are remembered here and still reported
|
|
212
|
+
# when the document is created.
|
|
213
|
+
(@pre_create_changed_attributes ||= ::Set.new).add(attr) if new_record?
|
|
214
|
+
changed_attributes[attr] = read_raw_attribute(attr).__deep_copy__
|
|
200
215
|
end
|
|
201
216
|
|
|
202
217
|
# Set the attribute back to its old value.
|
|
@@ -209,7 +224,10 @@ module Mongoid
|
|
|
209
224
|
# @return [ Object ] The old value.
|
|
210
225
|
def reset_attribute!(attr)
|
|
211
226
|
attr = database_field_name(attr)
|
|
212
|
-
|
|
227
|
+
return unless changed_attributes.key?(attr)
|
|
228
|
+
return if changed_attributes[attr] == attributes[attr]
|
|
229
|
+
|
|
230
|
+
attributes[attr] = changed_attributes.delete(attr)
|
|
213
231
|
end
|
|
214
232
|
|
|
215
233
|
def reset_attribute_to_default!(attr)
|
|
@@ -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.
|
|
@@ -95,6 +120,32 @@ module Mongoid
|
|
|
95
120
|
[]
|
|
96
121
|
end
|
|
97
122
|
|
|
123
|
+
# Update the first matching document atomically. There are no documents
|
|
124
|
+
# in the null context, so nothing is updated.
|
|
125
|
+
#
|
|
126
|
+
# @example Update the matching document.
|
|
127
|
+
# context.update(name: "Smiths")
|
|
128
|
+
#
|
|
129
|
+
# @param [ Hash ] attributes The new attributes for the document.
|
|
130
|
+
#
|
|
131
|
+
# @return [ nil | false ] False if no attributes were provided.
|
|
132
|
+
def update(attributes = nil, _opts = {})
|
|
133
|
+
attributes ? nil : false
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# Update all the matching documents atomically. There are no documents
|
|
137
|
+
# in the null context, so nothing is updated.
|
|
138
|
+
#
|
|
139
|
+
# @example Update all the matching documents.
|
|
140
|
+
# context.update_all(name: "Smiths")
|
|
141
|
+
#
|
|
142
|
+
# @param [ Hash ] attributes The new attributes for each document.
|
|
143
|
+
#
|
|
144
|
+
# @return [ nil | false ] False if no attributes were provided.
|
|
145
|
+
def update_all(attributes = nil, _opts = {})
|
|
146
|
+
attributes ? nil : false
|
|
147
|
+
end
|
|
148
|
+
|
|
98
149
|
# Create the new null context.
|
|
99
150
|
#
|
|
100
151
|
# @example Create the new context.
|
|
@@ -666,22 +666,27 @@ module Mongoid
|
|
|
666
666
|
else
|
|
667
667
|
# When we have multiple criteria, combine them all with $or
|
|
668
668
|
# and add the result to self.
|
|
669
|
+
#
|
|
670
|
+
# Every criterion is normalized through _mongoid_expand_keys, which
|
|
671
|
+
# runs the operator guard (MONGOID-6005). Note that the plain Hash
|
|
672
|
+
# branch must use _mongoid_expand_keys too: building the hash by
|
|
673
|
+
# hand skips the guard, letting a criterion such as
|
|
674
|
+
# {'$where' => ...} reach the selector.
|
|
675
|
+
#
|
|
676
|
+
# The disjunction is stored directly rather than via
|
|
677
|
+
# and('$or' => exprs). Routing it through #and would nest the new
|
|
678
|
+
# $or under a top-level $and (the MONGOID-5172 behavior, first
|
|
679
|
+
# released in 8.0), which changes both the selector shape and the
|
|
680
|
+
# meaning of a pre-existing top-level $or. On 7.6 the historical
|
|
681
|
+
# shape is preserved: append to the existing $or when one is
|
|
682
|
+
# present.
|
|
669
683
|
exprs = criteria.map do |criterion|
|
|
670
684
|
if criterion.is_a?(Selectable)
|
|
671
685
|
_mongoid_expand_keys(criterion.selector)
|
|
672
686
|
else
|
|
673
|
-
|
|
674
|
-
if k.is_a?(Symbol)
|
|
675
|
-
[k.to_s, v]
|
|
676
|
-
else
|
|
677
|
-
[k, v]
|
|
678
|
-
end
|
|
679
|
-
end]
|
|
687
|
+
_mongoid_expand_keys(criterion)
|
|
680
688
|
end
|
|
681
689
|
end
|
|
682
|
-
# Should be able to do:
|
|
683
|
-
#where('$or' => exprs)
|
|
684
|
-
# But since that is broken do instead:
|
|
685
690
|
clone.tap do |query|
|
|
686
691
|
if query.selector['$or']
|
|
687
692
|
query.selector.store('$or', query.selector['$or'] + exprs)
|
|
@@ -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
|
|
105
|
+
crit.selector.clear
|
|
106
|
+
crit.options.clear
|
|
107
|
+
crit.send(:clear_default_scope_none)
|
|
102
108
|
end
|
|
103
109
|
crit
|
|
104
110
|
end
|
data/lib/mongoid/criteria.rb
CHANGED
|
@@ -276,6 +276,20 @@ module Mongoid
|
|
|
276
276
|
self.documents = criteria.documents.dup unless criteria.documents.empty?
|
|
277
277
|
self.scoping_options = criteria.scoping_options
|
|
278
278
|
self.inclusions = (inclusions + criteria.inclusions).uniq
|
|
279
|
+
if criteria.empty_and_chainable?
|
|
280
|
+
# An explicit none wins over a none that only came from a default
|
|
281
|
+
# scope. Association proxies forward is_a? to their target, so the
|
|
282
|
+
# runtime class is checked instead; anything that is not a criteria is
|
|
283
|
+
# treated as an explicit none.
|
|
284
|
+
explicit = if Criteria === criteria # rubocop:disable Style/CaseEquality
|
|
285
|
+
criteria.send(:none_state) == true
|
|
286
|
+
else
|
|
287
|
+
true
|
|
288
|
+
end
|
|
289
|
+
@none = true if @none == true || explicit
|
|
290
|
+
@none ||= :default
|
|
291
|
+
@context = nil
|
|
292
|
+
end
|
|
279
293
|
self
|
|
280
294
|
end
|
|
281
295
|
|
|
@@ -287,7 +301,9 @@ module Mongoid
|
|
|
287
301
|
#
|
|
288
302
|
# @return [ Criteria ] The none criteria.
|
|
289
303
|
def none
|
|
290
|
-
@none = true
|
|
304
|
+
@none = true
|
|
305
|
+
@context = nil
|
|
306
|
+
self
|
|
291
307
|
end
|
|
292
308
|
|
|
293
309
|
# Is the criteria an empty but chainable criteria?
|
|
@@ -469,6 +485,34 @@ module Mongoid
|
|
|
469
485
|
|
|
470
486
|
private
|
|
471
487
|
|
|
488
|
+
# The kind of none on this criteria, if any. An explicit none is +true+,
|
|
489
|
+
# a none coming only from the default scope is +:default+, and a criteria
|
|
490
|
+
# that is not none returns +nil+.
|
|
491
|
+
#
|
|
492
|
+
# @api private
|
|
493
|
+
#
|
|
494
|
+
# @example Get the none state.
|
|
495
|
+
# criteria.send(:none_state)
|
|
496
|
+
#
|
|
497
|
+
# @return [ true | :default | nil ] The none state.
|
|
498
|
+
def none_state
|
|
499
|
+
@none
|
|
500
|
+
end
|
|
501
|
+
|
|
502
|
+
# Removes the none flag if it was set only by the default scope. An
|
|
503
|
+
# explicitly requested none is never removed.
|
|
504
|
+
#
|
|
505
|
+
# @api private
|
|
506
|
+
#
|
|
507
|
+
# @example Clear a default scope none.
|
|
508
|
+
# criteria.send(:clear_default_scope_none)
|
|
509
|
+
#
|
|
510
|
+
# @return [ Criteria ] The criteria.
|
|
511
|
+
def clear_default_scope_none
|
|
512
|
+
@none = nil if @none == :default
|
|
513
|
+
self
|
|
514
|
+
end
|
|
515
|
+
|
|
472
516
|
# Are documents in the query missing, and are we configured to raise an
|
|
473
517
|
# error?
|
|
474
518
|
#
|
data/lib/mongoid/document.rb
CHANGED
|
@@ -211,6 +211,12 @@ module Mongoid
|
|
|
211
211
|
new_errors.copy!(errors)
|
|
212
212
|
became.instance_variable_set(:@errors, new_errors)
|
|
213
213
|
became.instance_variable_set(:@new_record, new_record?)
|
|
214
|
+
# The document was constructed as new, collecting pre-create change
|
|
215
|
+
# markers. Drop them when the copied state is that of a persisted
|
|
216
|
+
# document: its pending changes were all tracked after the document
|
|
217
|
+
# was persisted, and stale markers would let in-place changes to
|
|
218
|
+
# readonly attributes bypass the dirty filter.
|
|
219
|
+
became.instance_variable_set(:@pre_create_changed_attributes, nil) unless new_record?
|
|
214
220
|
became.instance_variable_set(:@destroyed, destroyed?)
|
|
215
221
|
became.changed_attributes[klass.discriminator_key] = self.class.discriminator_value
|
|
216
222
|
became[klass.discriminator_key] = klass.discriminator_value
|
|
@@ -221,6 +227,10 @@ module Mongoid
|
|
|
221
227
|
relation = became.__send__(name)
|
|
222
228
|
Array.wrap(relation).each do |r|
|
|
223
229
|
r.instance_variable_set(:@new_record, new_record?)
|
|
230
|
+
# The embedded documents were also constructed as new; drop
|
|
231
|
+
# their pre-create change markers when they are marked as
|
|
232
|
+
# persisted, as is done for the parent document.
|
|
233
|
+
r.instance_variable_set(:@pre_create_changed_attributes, nil) unless new_record?
|
|
224
234
|
end
|
|
225
235
|
end
|
|
226
236
|
end
|
data/lib/mongoid/reloadable.rb
CHANGED
data/lib/mongoid/threaded.rb
CHANGED
data/lib/mongoid/version.rb
CHANGED
|
@@ -68,6 +68,11 @@ describe 'query operator injection guard' do
|
|
|
68
68
|
it 'rejects a $where smuggled through any_of' do
|
|
69
69
|
expect { Band.any_of('$where' => js).first }.to raise_error(Mongoid::Errors::InvalidQuery)
|
|
70
70
|
end
|
|
71
|
+
|
|
72
|
+
it 'rejects a $where smuggled through multi-criteria any_of' do
|
|
73
|
+
expect { Band.any_of({ '$where' => js }, { name: band.name }).first }
|
|
74
|
+
.to raise_error(Mongoid::Errors::InvalidQuery)
|
|
75
|
+
end
|
|
71
76
|
end
|
|
72
77
|
end
|
|
73
78
|
|
|
@@ -3848,4 +3848,52 @@ describe Mongoid::Association::Referenced::HasAndBelongsToMany::Proxy do
|
|
|
3848
3848
|
expect(contract.signature_ids).to eq([signature.id])
|
|
3849
3849
|
end
|
|
3850
3850
|
end
|
|
3851
|
+
|
|
3852
|
+
# Conditions that share a key with the association selector must be
|
|
3853
|
+
# intersected with it, whatever kind of key the caller used.
|
|
3854
|
+
key_variants = {
|
|
3855
|
+
'String' => :stringify_keys,
|
|
3856
|
+
'Symbol' => :symbolize_keys,
|
|
3857
|
+
'indifferent access' => :with_indifferent_access
|
|
3858
|
+
}.freeze
|
|
3859
|
+
|
|
3860
|
+
%i[ delete_all destroy_all ].each do |method|
|
|
3861
|
+
describe "##{method} with conditions on a key the association selector uses" do
|
|
3862
|
+
key_variants.each do |label, converter|
|
|
3863
|
+
context "when the keys are #{label}" do
|
|
3864
|
+
let(:draft) { DeleteAllTag.create!(state: 'draft') }
|
|
3865
|
+
let(:outside) { DeleteAllTag.create!(state: 'draft') }
|
|
3866
|
+
let(:account) { DeleteAllAccount.create!(tags: [ draft, DeleteAllTag.create!(state: 'open') ]) }
|
|
3867
|
+
let(:proxy) { DeleteAllAccount.find(account.id).tags }
|
|
3868
|
+
|
|
3869
|
+
before { outside }
|
|
3870
|
+
|
|
3871
|
+
context 'with a condition on the primary key' do
|
|
3872
|
+
let(:conditions) { { _id: outside.id }.send(converter) }
|
|
3873
|
+
|
|
3874
|
+
it 'deletes nothing outside the association' do
|
|
3875
|
+
expect(proxy.send(method, conditions)).to eq(0)
|
|
3876
|
+
expect(DeleteAllTag.count).to eq(3)
|
|
3877
|
+
end
|
|
3878
|
+
|
|
3879
|
+
it 'does not modify the conditions' do
|
|
3880
|
+
original = conditions.deep_dup
|
|
3881
|
+
proxy.send(method, conditions)
|
|
3882
|
+
expect(conditions).to eq(original)
|
|
3883
|
+
end
|
|
3884
|
+
end
|
|
3885
|
+
|
|
3886
|
+
context 'with a condition on the primary key inside the association' do
|
|
3887
|
+
let(:conditions) { { _id: draft.id }.send(converter) }
|
|
3888
|
+
|
|
3889
|
+
it 'deletes only the matching document' do
|
|
3890
|
+
expect(proxy.send(method, conditions)).to eq(1)
|
|
3891
|
+
expect(DeleteAllTag.pluck(:_id)).not_to include(draft.id)
|
|
3892
|
+
expect(DeleteAllTag.count).to eq(2)
|
|
3893
|
+
end
|
|
3894
|
+
end
|
|
3895
|
+
end
|
|
3896
|
+
end
|
|
3897
|
+
end
|
|
3898
|
+
end
|
|
3851
3899
|
end
|
|
@@ -4254,4 +4254,87 @@ describe Mongoid::Association::Referenced::HasMany::Proxy do
|
|
|
4254
4254
|
expect(agent.basic_ids).to eq([basic.id])
|
|
4255
4255
|
end
|
|
4256
4256
|
end
|
|
4257
|
+
|
|
4258
|
+
# Conditions that share a key with the association selector must be
|
|
4259
|
+
# intersected with it, whatever kind of key the caller used.
|
|
4260
|
+
key_variants = {
|
|
4261
|
+
'String' => :stringify_keys,
|
|
4262
|
+
'Symbol' => :symbolize_keys,
|
|
4263
|
+
'indifferent access' => :with_indifferent_access
|
|
4264
|
+
}.freeze
|
|
4265
|
+
|
|
4266
|
+
%i[ delete_all destroy_all ].each do |method|
|
|
4267
|
+
describe "##{method} with conditions on a key the association selector uses" do
|
|
4268
|
+
let(:account) { DeleteAllAccount.create! }
|
|
4269
|
+
let(:other) { DeleteAllAccount.create! }
|
|
4270
|
+
|
|
4271
|
+
key_variants.each do |label, converter|
|
|
4272
|
+
context "when the keys are #{label}" do
|
|
4273
|
+
# rubocop:disable RSpec/MultipleMemoizedHelpers
|
|
4274
|
+
shared_examples 'an intersected bulk delete' do
|
|
4275
|
+
let(:reloaded) { DeleteAllAccount.find(account.id) }
|
|
4276
|
+
let(:proxy) { reloaded.send(association) }
|
|
4277
|
+
let(:conditions) { raw_conditions.send(converter) }
|
|
4278
|
+
let!(:expected) { DeleteAllAccount.find(account.id).send(association).where(raw_conditions).pluck(:_id) }
|
|
4279
|
+
let!(:everything) { klass.pluck(:_id) }
|
|
4280
|
+
|
|
4281
|
+
it 'deletes the same documents as the where chain' do
|
|
4282
|
+
expect(proxy.send(method, conditions)).to eq(expected.size)
|
|
4283
|
+
expect(klass.pluck(:_id)).to match_array(everything - expected)
|
|
4284
|
+
end
|
|
4285
|
+
|
|
4286
|
+
it 'does not modify the conditions' do
|
|
4287
|
+
original = conditions.deep_dup
|
|
4288
|
+
proxy.send(method, conditions)
|
|
4289
|
+
expect(conditions).to eq(original)
|
|
4290
|
+
end
|
|
4291
|
+
end
|
|
4292
|
+
# rubocop:enable RSpec/MultipleMemoizedHelpers
|
|
4293
|
+
|
|
4294
|
+
context 'with an association scope' do
|
|
4295
|
+
let(:klass) { DeleteAllItem }
|
|
4296
|
+
let(:association) { :items }
|
|
4297
|
+
let(:raw_conditions) { { state: 'draft' } }
|
|
4298
|
+
|
|
4299
|
+
before do
|
|
4300
|
+
2.times { DeleteAllItem.create!(account: account, state: 'open') }
|
|
4301
|
+
DeleteAllItem.create!(account: account, state: 'draft')
|
|
4302
|
+
DeleteAllItem.create!(account: other, state: 'draft')
|
|
4303
|
+
DeleteAllItem.create!(account: account, state: 'closed')
|
|
4304
|
+
end
|
|
4305
|
+
|
|
4306
|
+
it_behaves_like 'an intersected bulk delete'
|
|
4307
|
+
end
|
|
4308
|
+
|
|
4309
|
+
context 'with a child default scope' do
|
|
4310
|
+
let(:klass) { DeleteAllScopedItem }
|
|
4311
|
+
let(:association) { :scoped_items }
|
|
4312
|
+
let(:raw_conditions) { { state: 'draft' } }
|
|
4313
|
+
|
|
4314
|
+
before do
|
|
4315
|
+
2.times { DeleteAllScopedItem.create!(account: account, state: 'open') }
|
|
4316
|
+
DeleteAllScopedItem.create!(account: account, state: 'draft')
|
|
4317
|
+
DeleteAllScopedItem.create!(account: other, state: 'draft')
|
|
4318
|
+
DeleteAllScopedItem.collection.insert_one(account_id: account.id, state: 'closed')
|
|
4319
|
+
end
|
|
4320
|
+
|
|
4321
|
+
it_behaves_like 'an intersected bulk delete'
|
|
4322
|
+
end
|
|
4323
|
+
|
|
4324
|
+
context 'with a condition on the foreign key' do
|
|
4325
|
+
let(:klass) { DeleteAllItem }
|
|
4326
|
+
let(:association) { :items }
|
|
4327
|
+
let(:raw_conditions) { { account_id: other.id } }
|
|
4328
|
+
|
|
4329
|
+
before do
|
|
4330
|
+
2.times { DeleteAllItem.create!(account: account, state: 'open') }
|
|
4331
|
+
DeleteAllItem.create!(account: other, state: 'open')
|
|
4332
|
+
end
|
|
4333
|
+
|
|
4334
|
+
it_behaves_like 'an intersected bulk delete'
|
|
4335
|
+
end
|
|
4336
|
+
end
|
|
4337
|
+
end
|
|
4338
|
+
end
|
|
4339
|
+
end
|
|
4257
4340
|
end
|