diff --git a/docs/release-notes/mongoid-9.0.txt b/docs/release-notes/mongoid-9.0.txt index f6974ac5d2..620f2a69a4 100644 --- a/docs/release-notes/mongoid-9.0.txt +++ b/docs/release-notes/mongoid-9.0.txt @@ -74,6 +74,7 @@ Deprecated functionality removed The method ``Mongoid::QueryCache#clear_cache`` should be replaced with ``Mongo::QueryCache#clear``. All other methods and submodules are identically named. Refer to the `driver query cache documentation `_ for more details. +- ``Object#blank_criteria?`` method is removed (was previously deprecated.) ``touch`` method now clears changed state diff --git a/lib/mongoid/association/referenced/has_many/enumerable.rb b/lib/mongoid/association/referenced/has_many/enumerable.rb index 131a095680..b388e404be 100644 --- a/lib/mongoid/association/referenced/has_many/enumerable.rb +++ b/lib/mongoid/association/referenced/has_many/enumerable.rb @@ -478,12 +478,48 @@ def set_base(document) end def unloaded_documents - if _unloaded.selector._mongoid_unsatisfiable_criteria? + if unsatisfiable_criteria?(_unloaded.selector) [] else _unloaded end end + + # Checks whether conditions in the given hash are known to be + # unsatisfiable, i.e. querying with this hash will always return no + # documents. + # + # This method only handles condition shapes that Mongoid itself uses when + # it builds association queries. Return value true indicates the condition + # always produces an empty document set. Note however that return value false + # is not a guarantee that the condition won't produce an empty document set. + # + # @example Unsatisfiable conditions + # unsatisfiable_criteria?({'_id' => {'$in' => []}}) + # # => true + # + # @example Conditions which may be satisfiable + # unsatisfiable_criteria?({'_id' => '123'}) + # # => false + # + # @example Conditions which are unsatisfiable that this method does not handle + # unsatisfiable_criteria?({'foo' => {'$in' => []}}) + # # => false + # + # @param [ Hash ] selector The conditions to check. + # + # @return [ true | false ] Whether hash contains known unsatisfiable + # conditions. + def unsatisfiable_criteria?(selector) + unsatisfiable_criteria = { '_id' => { '$in' => [] } } + return true if selector == unsatisfiable_criteria + return false unless selector.length == 1 && selector.keys == %w[$and] + + value = selector.values.first + value.is_a?(Array) && value.any? do |sub_value| + sub_value.is_a?(Hash) && unsatisfiable_criteria?(sub_value) + end + end end end end diff --git a/lib/mongoid/extensions/array.rb b/lib/mongoid/extensions/array.rb index 072a1d42b4..5188ca557a 100644 --- a/lib/mongoid/extensions/array.rb +++ b/lib/mongoid/extensions/array.rb @@ -55,26 +55,6 @@ def __mongoize_time__ ::Time.configured.local(*self) end - # Checks whether conditions given in this array are known to be - # unsatisfiable, i.e., querying with this array will always return no - # documents. - # - # This method used to assume that the array is the list of criteria - # to be used with an $and operator. This assumption is no longer made; - # therefore, since the interpretation of conditions in the array differs - # between $and, $or and $nor operators, this method now always returns - # false. - # - # This method is deprecated. Mongoid now uses - # +_mongoid_unsatisfiable_criteria?+ internally; this method is retained - # for backwards compatibility only. It always returns false. - # - # @return [ false ] Always false. - # @deprecated - def blank_criteria? - false - end - # Is the array a set of multiple arguments in a method? # # @example Is this multi args? @@ -175,5 +155,3 @@ def resizable? ::Array.__send__(:include, Mongoid::Extensions::Array) ::Array.extend(Mongoid::Extensions::Array::ClassMethods) - -::Mongoid.deprecate(Array, :blank_criteria) diff --git a/lib/mongoid/extensions/hash.rb b/lib/mongoid/extensions/hash.rb index 1cf19d539d..435dc1afd5 100644 --- a/lib/mongoid/extensions/hash.rb +++ b/lib/mongoid/extensions/hash.rb @@ -47,54 +47,6 @@ def __consolidate__(klass) end Mongoid.deprecate(self, :__consolidate__) - # Checks whether conditions given in this hash are known to be - # unsatisfiable, i.e., querying with this hash will always return no - # documents. - # - # This method only handles condition shapes that Mongoid itself uses when - # it builds association queries. It does not guarantee that a false - # return value means the condition can produce a non-empty document set - - # only that if the return value is true, the condition always produces - # an empty document set. - # - # @example Unsatisfiable conditions - # {'_id' => {'$in' => []}}._mongoid_unsatisfiable_criteria? - # # => true - # - # @example Conditions which could be satisfiable - # {'_id' => '123'}._mongoid_unsatisfiable_criteria? - # # => false - # - # @example Conditions which are unsatisfiable that this method does not handle - # {'foo' => {'$in' => []}}._mongoid_unsatisfiable_criteria? - # # => false - # - # @return [ true | false ] Whether hash contains known unsatisfiable - # conditions. - # @api private - def _mongoid_unsatisfiable_criteria? - unsatisfiable_criteria = { "_id" => { "$in" => [] }} - return true if self == unsatisfiable_criteria - return false unless length == 1 && keys == %w($and) - value = values.first - value.is_a?(Array) && value.any? do |sub_v| - sub_v.is_a?(Hash) && sub_v._mongoid_unsatisfiable_criteria? - end - end - - # Checks whether conditions given in this hash are known to be - # unsatisfiable, i.e., querying with this hash will always return no - # documents. - # - # This method is deprecated. Mongoid now uses - # +_mongoid_unsatisfiable_criteria?+ internally; this method is retained - # for backwards compatibility only. - # - # @return [ true | false ] Whether hash contains known unsatisfiable - # conditions. - # @deprecated - alias :blank_criteria? :_mongoid_unsatisfiable_criteria? - # Deletes an id value from the hash. # # @example Delete an id value. @@ -192,5 +144,3 @@ def resizable? ::Hash.__send__(:include, Mongoid::Extensions::Hash) ::Hash.extend(Mongoid::Extensions::Hash::ClassMethods) - -::Mongoid.deprecate(Hash, :blank_criteria) diff --git a/lib/mongoid/extensions/object.rb b/lib/mongoid/extensions/object.rb index 089269bc7e..52fa4b4b37 100644 --- a/lib/mongoid/extensions/object.rb +++ b/lib/mongoid/extensions/object.rb @@ -84,21 +84,6 @@ def __to_inc__ end Mongoid.deprecate(self, :__to_inc__) - # Checks whether conditions given in this object are known to be - # unsatisfiable, i.e., querying with this object will always return no - # documents. - # - # This method is deprecated. Mongoid now uses - # +_mongoid_unsatisfiable_criteria?+ internally; this method is retained - # for backwards compatibility only. It always returns false. - # - # @return [ false ] Always false. - # @deprecated - def blank_criteria? - false - end - Mongoid.deprecate(self, :blank_criteria?) - # Do or do not, there is no try. -- Yoda. # # @example Do or do not. diff --git a/spec/mongoid/extensions/array_spec.rb b/spec/mongoid/extensions/array_spec.rb index 49c402986e..e9fa5b2fde 100644 --- a/spec/mongoid/extensions/array_spec.rb +++ b/spec/mongoid/extensions/array_spec.rb @@ -378,34 +378,6 @@ end end - describe "#blank_criteria?" do - - context "when the array has an empty _id criteria" do - - context "when only the id criteria is in the array" do - - let(:array) do - [{ "_id" => { "$in" => [] }}] - end - - it "is false" do - expect(array.blank_criteria?).to be false - end - end - - context "when the id criteria is in the array with others" do - - let(:array) do - [{ "_id" => "test" }, { "_id" => { "$in" => [] }}] - end - - it "is false" do - expect(array.blank_criteria?).to be false - end - end - end - end - describe "#delete_one" do context "when the object doesn't exist" do diff --git a/spec/mongoid/extensions/hash_spec.rb b/spec/mongoid/extensions/hash_spec.rb index bcc1a58d6f..5d797a14da 100644 --- a/spec/mongoid/extensions/hash_spec.rb +++ b/spec/mongoid/extensions/hash_spec.rb @@ -294,107 +294,4 @@ expect(Hash).to be_resizable end end - - shared_examples_for 'unsatisfiable criteria method' do - - context "when the hash has only an empty _id criteria" do - - let(:hash) do - { "_id" => { "$in" => [] }} - end - - it "is true" do - expect(hash.send(meth)).to be true - end - end - - context "when the hash has an empty _id criteria and another criteria" do - - let(:hash) do - { "_id" => { "$in" => [] }, 'foo' => 'bar'} - end - - it "is false" do - expect(hash.send(meth)).to be false - end - end - - context "when the hash has an empty _id criteria via $and" do - - let(:hash) do - {'$and' => [{ "_id" => { "$in" => [] }}]} - end - - it "is true" do - expect(hash.send(meth)).to be true - end - end - - context "when the hash has an empty _id criteria via $and and another criteria at top level" do - - let(:hash) do - {'$and' => [{ "_id" => { "$in" => [] }}], 'foo' => 'bar'} - end - - it "is false" do - expect(hash.send(meth)).to be false - end - end - - context "when the hash has an empty _id criteria via $and and another criteria in $and" do - - let(:hash) do - {'$and' => [{ "_id" => { "$in" => [] }}, {'foo' => 'bar'}]} - end - - it "is true" do - expect(hash.send(meth)).to be true - end - end - - context "when the hash has an empty _id criteria via $and and another criteria in $and value" do - - let(:hash) do - {'$and' => [{ "_id" => { "$in" => [] }, 'foo' => 'bar'}]} - end - - it "is false" do - expect(hash.send(meth)).to be false - end - end - - context "when the hash has an empty _id criteria via $or" do - - let(:hash) do - {'$or' => [{ "_id" => { "$in" => [] }}]} - end - - it "is false" do - expect(hash.send(meth)).to be false - end - end - - context "when the hash has an empty _id criteria via $nor" do - - let(:hash) do - {'$nor' => [{ "_id" => { "$in" => [] }}]} - end - - it "is false" do - expect(hash.send(meth)).to be false - end - end - end - - describe "#blank_criteria?" do - let(:meth) { :blank_criteria? } - - it_behaves_like 'unsatisfiable criteria method' - end - - describe "#_mongoid_unsatisfiable_criteria?" do - let(:meth) { :_mongoid_unsatisfiable_criteria? } - - it_behaves_like 'unsatisfiable criteria method' - end end diff --git a/spec/mongoid/extensions/object_spec.rb b/spec/mongoid/extensions/object_spec.rb index 4b2e294ded..73c01fb680 100644 --- a/spec/mongoid/extensions/object_spec.rb +++ b/spec/mongoid/extensions/object_spec.rb @@ -265,11 +265,4 @@ expect(object.numeric?).to eq(false) end end - - describe "#blank_criteria?" do - - it "is false" do - expect(object.blank_criteria?).to be false - end - end end