mongoid 8.1.2 → 8.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2100bfc3ff5a707914028bbcca16fe61db68cfc271f2603b79a9f420baccdd3a
4
- data.tar.gz: c1147f2941f122d45a5678ed2c9d95ab840a65e1f8883970384f846f979bfb9f
3
+ metadata.gz: 561c6173b8a11bb858b0df7dc72cec9e96767e7780572ecbc1233c11de84fb6a
4
+ data.tar.gz: 1bc15420d04291843d26e52d1d04f2689d3e28b25db115d5a5f2c765b872fd30
5
5
  SHA512:
6
- metadata.gz: 4710b315e359d87807dc3b882b39d929eb52bf53612bf2d37e6bbf409309b3116773eab6e810b8b7dbbbfc33f24e135758c757b3de43dca4c591881b39d1e2df
7
- data.tar.gz: 758af68d937986c4a01963ff9e8c6f106276fc40805c1133966d88461eabd19ca7b41e412b50566fe962b180525a925bae529ff5b58846fa8433336d8880a6ad
6
+ metadata.gz: 112499f5ac9998aa952c851ac4e6d64014bf35fcfeeed0b29bdb5fac5519866657b8f7fe087a001033cee7adcf1f07a7eb4b4c68dcef2611cbf36a1aef22d31c
7
+ data.tar.gz: 43f784a2d2d5782f464f064d35bd384a6711a2355f6a1cd3f9529372184928ecac9d2e74e4cf90092857733945a10c08452ede7e1a4cbba903d3617d5ccbf601
checksums.yaml.gz.sig CHANGED
Binary file
@@ -185,6 +185,19 @@ module Mongoid
185
185
  # See https://jira.mongodb.org/browse/MONGOID-5542
186
186
  option :prevent_multiple_calls_of_embedded_callbacks, default: false
187
187
 
188
+ # When this flag is true, callbacks for embedded documents will not be
189
+ # called. This is the default in 8.x, but will be changed to false in 9.0.
190
+ #
191
+ # Setting this flag to true (as it is in 8.x) may lead to stack
192
+ # overflow errors if there are more than cicrca 1000 embedded
193
+ # documents in the root document's dependencies graph.
194
+ #
195
+ # It is strongly recommended to set this flag to false in 8.x, if you
196
+ # are not using around callbacks for embedded documents.
197
+ #
198
+ # See https://jira.mongodb.org/browse/MONGOID-5658 for more details.
199
+ option :around_callbacks_for_embeds, default: true
200
+
188
201
  # Returns the Config singleton, for use in the configure DSL.
189
202
  #
190
203
  # @return [ self ] The Config singleton.
@@ -69,7 +69,12 @@ module Mongoid
69
69
  # @return [ Integer ] The number of matches.
70
70
  def count(options = {}, &block)
71
71
  return super(&block) if block_given?
72
- view.count_documents(options)
72
+
73
+ if valid_for_count_documents?
74
+ view.count_documents(options)
75
+ else
76
+ view.count(options)
77
+ end
73
78
  end
74
79
 
75
80
  # Get the estimated number of documents matching the query.
@@ -1046,6 +1051,24 @@ module Mongoid
1046
1051
  limit ? docs : docs.first
1047
1052
  end
1048
1053
 
1054
+ # Queries whether the current context is valid for use with
1055
+ # the #count_documents? predicate. A context is valid if it
1056
+ # does not include a `$where` operator.
1057
+ #
1058
+ # @return [ true | false ] whether or not the current context
1059
+ # excludes a `$where` operator.
1060
+ def valid_for_count_documents?(hash = view.filter)
1061
+ # Note that `view.filter` is a BSON::Document, and all keys in a
1062
+ # BSON::Document are strings; we don't need to worry about symbol
1063
+ # representations of `$where`.
1064
+ hash.keys.each do |key|
1065
+ return false if key == '$where'
1066
+ return false if hash[key].is_a?(Hash) && !valid_for_count_documents?(hash[key])
1067
+ end
1068
+
1069
+ true
1070
+ end
1071
+
1049
1072
  def raise_document_not_found_error
1050
1073
  raise Errors::DocumentNotFound.new(klass, nil, nil)
1051
1074
  end
@@ -27,7 +27,8 @@ module Mongoid
27
27
  # @param [ [ Symbol | Hash<Symbol, [ Symbol | String ]> ]... ] *method_descriptors
28
28
  # The methods to deprecate, with optional replacement instructions.
29
29
  def deprecate(target_module, *method_descriptors)
30
- Mongoid::Deprecation.deprecate_methods(target_module, *method_descriptors)
30
+ @_deprecator ||= Mongoid::Deprecation.new
31
+ @_deprecator.deprecate_methods(target_module, *method_descriptors)
31
32
  end
32
33
  end
33
34
  end
@@ -15,10 +15,10 @@ module Mongoid
15
15
  #
16
16
  # @return Array<Proc> The deprecation behavior.
17
17
  def behavior
18
- @behavior ||= Array(->(message, callstack, _deprecation_horizon, _gem_name) {
18
+ @behavior ||= Array(->(*args) {
19
19
  logger = Mongoid.logger
20
- logger.warn(message)
21
- logger.debug(callstack.join("\n ")) if debug
20
+ logger.warn(args[0])
21
+ logger.debug(args[1].join("\n ")) if debug
22
22
  })
23
23
  end
24
24
  end
@@ -43,7 +43,7 @@ module Mongoid
43
43
  real_key = klass.database_field_name(key2)
44
44
 
45
45
  value.delete(key2) if real_key != key2
46
- value[real_key] = (key == "$rename") ? value2.to_s : mongoize_for(key, klass, real_key, value2)
46
+ value[real_key] = value_for(key, klass, real_key, value2)
47
47
  end
48
48
  consolidated[key] ||= {}
49
49
  consolidated[key].update(value)
@@ -185,6 +185,24 @@ module Mongoid
185
185
 
186
186
  private
187
187
 
188
+ # Get the value for the provided operator, klass, key and value.
189
+ #
190
+ # This is necessary for special cases like $rename, $addToSet and $push.
191
+ #
192
+ # @param [ String ] operator The operator.
193
+ # @param [ Class ] klass The model class.
194
+ # @param [ String | Symbol ] key The field key.
195
+ # @param [ Object ] value The original value.
196
+ #
197
+ # @return [ Object ] Value prepared for the provided operator.
198
+ def value_for(operator, klass, key, value)
199
+ case operator
200
+ when "$rename" then value.to_s
201
+ when "$addToSet", "$push" then value.mongoize
202
+ else mongoize_for(operator, klass, operator, value)
203
+ end
204
+ end
205
+
188
206
  # Mongoize for the klass, key and value.
189
207
  #
190
208
  # @api private
@@ -43,6 +43,8 @@ module Mongoid
43
43
  # @api private
44
44
  define_model_callbacks :persist_parent
45
45
 
46
+ define_model_callbacks :commit, :rollback, only: :after
47
+
46
48
  attr_accessor :before_callback_halted
47
49
  end
48
50
 
@@ -140,6 +142,28 @@ module Mongoid
140
142
  #
141
143
  # @api private
142
144
  def _mongoid_run_child_callbacks(kind, children: nil, &block)
145
+ if Mongoid::Config.around_callbacks_for_embeds
146
+ _mongoid_run_child_callbacks_with_around(kind, children: children, &block)
147
+ else
148
+ _mongoid_run_child_callbacks_without_around(kind, children: children, &block)
149
+ end
150
+ end
151
+
152
+ # Execute the callbacks of given kind for embedded documents including
153
+ # around callbacks.
154
+ #
155
+ # @note This method is prone to stack overflow errors if the document
156
+ # has a large number of embedded documents. It is recommended to avoid
157
+ # using around callbacks for embedded documents until a proper solution
158
+ # is implemented.
159
+ #
160
+ # @param [ Symbol ] kind The type of callback to execute.
161
+ # @param [ Array<Document> ] children Children to execute callbacks on. If
162
+ # nil, callbacks will be executed on all cascadable children of
163
+ # the document.
164
+ #
165
+ # @api private
166
+ def _mongoid_run_child_callbacks_with_around(kind, children: nil, &block)
143
167
  child, *tail = (children || cascadable_children(kind))
144
168
  with_children = !Mongoid::Config.prevent_multiple_calls_of_embedded_callbacks
145
169
  if child.nil?
@@ -148,23 +172,91 @@ module Mongoid
148
172
  child.run_callbacks(child_callback_type(kind, child), with_children: with_children, &block)
149
173
  else
150
174
  child.run_callbacks(child_callback_type(kind, child), with_children: with_children) do
151
- _mongoid_run_child_callbacks(kind, children: tail, &block)
175
+ _mongoid_run_child_callbacks_with_around(kind, children: tail, &block)
152
176
  end
153
177
  end
154
178
  end
155
179
 
156
- # This is used to store callbacks to be executed later. A good use case for
157
- # this is delaying the after_find and after_initialize callbacks until the
158
- # associations are set on the document. This can also be used to delay
159
- # applying the defaults on a document.
180
+ # Execute the callbacks of given kind for embedded documents without
181
+ # around callbacks.
160
182
  #
161
- # @return [ Array<Symbol> ] an array of symbols that represent the pending callbacks.
183
+ # @param [ Symbol ] kind The type of callback to execute.
184
+ # @param [ Array<Document> ] children Children to execute callbacks on. If
185
+ # nil, callbacks will be executed on all cascadable children of
186
+ # the document.
187
+ #
188
+ # @api private
189
+ def _mongoid_run_child_callbacks_without_around(kind, children: nil, &block)
190
+ children = (children || cascadable_children(kind))
191
+ callback_list = _mongoid_run_child_before_callbacks(kind, children: children)
192
+ return false if callback_list == false
193
+ value = block&.call
194
+ callback_list.each do |_next_sequence, env|
195
+ env.value &&= value
196
+ end
197
+ return false if _mongoid_run_child_after_callbacks(callback_list: callback_list) == false
198
+
199
+ value
200
+ end
201
+
202
+ # Execute the before callbacks of given kind for embedded documents.
203
+ #
204
+ # @param [ Symbol ] kind The type of callback to execute.
205
+ # @param [ Array<Document> ] children Children to execute callbacks on.
206
+ # @param [ Array<ActiveSupport::Callbacks::CallbackSequence, ActiveSupport::Callbacks::Filters::Environment> ] callback_list List of
207
+ # pairs of callback sequence and environment. This list will be later used
208
+ # to execute after callbacks in reverse order.
209
+ #
210
+ # @api private
211
+ def _mongoid_run_child_before_callbacks(kind, children: [], callback_list: [])
212
+ children.each do |child|
213
+ chain = child.__callbacks[child_callback_type(kind, child)]
214
+ env = ActiveSupport::Callbacks::Filters::Environment.new(child, false, nil)
215
+ next_sequence = compile_callbacks(chain)
216
+ unless next_sequence.final?
217
+ Mongoid.logger.warn("Around callbacks are disabled for embedded documents. Skipping around callbacks for #{child.class.name}.")
218
+ Mongoid.logger.warn("To enable around callbacks for embedded documents, set Mongoid::Config.around_callbacks_for_embeds to true.")
219
+ end
220
+ next_sequence.invoke_before(env)
221
+ return false if env.halted
222
+ env.value = !env.halted
223
+ callback_list << [next_sequence, env]
224
+ if (grandchildren = child.send(:cascadable_children, kind))
225
+ _mongoid_run_child_before_callbacks(kind, children: grandchildren, callback_list: callback_list)
226
+ end
227
+ end
228
+ callback_list
229
+ end
230
+
231
+ # Execute the after callbacks.
232
+ #
233
+ # @param [ Array<ActiveSupport::Callbacks::CallbackSequence, ActiveSupport::Callbacks::Filters::Environment> ] callback_list List of
234
+ # pairs of callback sequence and environment.
235
+ def _mongoid_run_child_after_callbacks(callback_list: [])
236
+ callback_list.reverse_each do |next_sequence, env|
237
+ next_sequence.invoke_after(env)
238
+ return false if env.halted
239
+ end
240
+ end
241
+
242
+ # Returns the stored callbacks to be executed later.
243
+ #
244
+ # @return [ Array<Symbol> ] Method symbols of the stored pending callbacks.
162
245
  #
163
246
  # @api private
164
247
  def pending_callbacks
165
248
  @pending_callbacks ||= [].to_set
166
249
  end
167
250
 
251
+ # Stores callbacks to be executed later. A good use case for
252
+ # this is delaying the after_find and after_initialize callbacks until the
253
+ # associations are set on the document. This can also be used to delay
254
+ # applying the defaults on a document.
255
+ #
256
+ # @param [ Array<Symbol> ] value Method symbols of the pending callbacks to store.
257
+ #
258
+ # @return [ Array<Symbol> ] Method symbols of the stored pending callbacks.
259
+ #
168
260
  # @api private
169
261
  def pending_callbacks=(value)
170
262
  @pending_callbacks = value
@@ -299,7 +391,7 @@ module Mongoid
299
391
  end
300
392
  self.class.send :define_method, name do
301
393
  env = ActiveSupport::Callbacks::Filters::Environment.new(self, false, nil)
302
- sequence = chain.compile
394
+ sequence = compile_callbacks(chain)
303
395
  sequence.invoke_before(env)
304
396
  env.value = !env.halted
305
397
  sequence.invoke_after(env)
@@ -309,5 +401,24 @@ module Mongoid
309
401
  end
310
402
  send(name)
311
403
  end
404
+
405
+ # Compile the callback chain.
406
+ #
407
+ # This method hides the differences between ActiveSupport implementations
408
+ # before and after 7.1.
409
+ #
410
+ # @param [ ActiveSupport::Callbacks::CallbackChain ] chain The callback chain.
411
+ # @param [ Symbol | nil ] type The type of callback chain to compile.
412
+ #
413
+ # @return [ ActiveSupport::Callbacks::CallbackSequence ] The compiled callback sequence.
414
+ def compile_callbacks(chain, type = nil)
415
+ if chain.method(:compile).arity == 0
416
+ # ActiveSupport < 7.1
417
+ chain.compile
418
+ else
419
+ # ActiveSupport >= 7.1
420
+ chain.compile(type)
421
+ end
422
+ end
312
423
  end
313
424
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mongoid
4
- VERSION = "8.1.2"
4
+ VERSION = "8.1.3"
5
5
  end
@@ -557,6 +557,7 @@ describe 'callbacks integration tests' do
557
557
 
558
558
  context 'nested embedded documents' do
559
559
  config_override :prevent_multiple_calls_of_embedded_callbacks, true
560
+ config_override :around_callbacks_for_embeds, true
560
561
 
561
562
  let(:logger) { Array.new }
562
563
 
@@ -581,4 +582,24 @@ describe 'callbacks integration tests' do
581
582
  expect(logger).to eq(%i[embedded_twice embedded_once root])
582
583
  end
583
584
  end
585
+
586
+ context 'cascade callbacks' do
587
+ ruby_version_gte '3.0'
588
+ config_override :around_callbacks_for_embeds, false
589
+
590
+ let(:book) do
591
+ Book.new
592
+ end
593
+
594
+ before do
595
+ 1500.times do
596
+ book.pages.build
597
+ end
598
+ end
599
+
600
+ # https://jira.mongodb.org/browse/MONGOID-5658
601
+ it 'does not raise SystemStackError' do
602
+ expect { book.save! }.not_to raise_error(SystemStackError)
603
+ end
604
+ end
584
605
  end
@@ -158,6 +158,16 @@ describe Mongoid::Contextual::Mongo do
158
158
  end
159
159
  end
160
160
  end
161
+
162
+ context 'when for_js is present' do
163
+ let(:context) do
164
+ Band.for_js('this.name == "Depeche Mode"')
165
+ end
166
+
167
+ it 'counts the expected records' do
168
+ expect(context.count).to eq(1)
169
+ end
170
+ end
161
171
  end
162
172
 
163
173
  describe "#estimated_count" do
@@ -3690,16 +3700,51 @@ describe Mongoid::Contextual::Mongo do
3690
3700
 
3691
3701
  context "when the attributes are in the correct type" do
3692
3702
 
3693
- before do
3694
- context.update_all("$set" => { name: "Smiths" })
3703
+ context "when operation is $set" do
3704
+
3705
+ before do
3706
+ context.update_all("$set" => { name: "Smiths" })
3707
+ end
3708
+
3709
+ it "updates the first matching document" do
3710
+ expect(depeche_mode.reload.name).to eq("Smiths")
3711
+ end
3712
+
3713
+ it "updates the last matching document" do
3714
+ expect(new_order.reload.name).to eq("Smiths")
3715
+ end
3695
3716
  end
3696
3717
 
3697
- it "updates the first matching document" do
3698
- expect(depeche_mode.reload.name).to eq("Smiths")
3718
+ context "when operation is $push" do
3719
+
3720
+ before do
3721
+ depeche_mode.update_attribute(:genres, ["electronic"])
3722
+ new_order.update_attribute(:genres, ["electronic"])
3723
+ context.update_all("$push" => { genres: "pop" })
3724
+ end
3725
+
3726
+ it "updates the first matching document" do
3727
+ expect(depeche_mode.reload.genres).to eq(["electronic", "pop"])
3728
+ end
3729
+
3730
+ it "updates the last matching document" do
3731
+ expect(new_order.reload.genres).to eq(["electronic", "pop"])
3732
+ end
3699
3733
  end
3700
3734
 
3701
- it "updates the last matching document" do
3702
- expect(new_order.reload.name).to eq("Smiths")
3735
+ context "when operation is $addToSet" do
3736
+
3737
+ before do
3738
+ context.update_all("$addToSet" => { genres: "electronic" })
3739
+ end
3740
+
3741
+ it "updates the first matching document" do
3742
+ expect(depeche_mode.reload.genres).to eq(["electronic"])
3743
+ end
3744
+
3745
+ it "updates the last matching document" do
3746
+ expect(new_order.reload.genres).to eq(["electronic"])
3747
+ end
3703
3748
  end
3704
3749
  end
3705
3750