Skip to content

Commit

Permalink
MONGOID-5671 [Monkey Patch Removal] Remove Object#blank_criteria? and…
Browse files Browse the repository at this point in the history
… Hash#__mongoid_unsatisfiable_criteria? (#5700)

* - Remove ``Object#blank_criteria?`` method entirely (was previously deprecated and not used in code.)
- Remove ``Hash#_mongoid_unsatisfiable_criteria?`` method is removed (was previously marked @api private) and move it to a private method of Referenced::HasMany::Enumerable.

* Update enumerable.rb

* removing the specs for unsatisifiable_criteria

this tests an implementation detail and not a behavior, which is
fragile. I'm not even sure this is an implementation detail we want,
and testing it specifically pours metaphorical concrete around it,
making it that much harder to remove later.

---------

Co-authored-by: Jamis Buck <[email protected]>
  • Loading branch information
johnnyshields and jamis authored Nov 7, 2023
1 parent 7378141 commit fb1d280
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 226 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/mongoid-9.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
<https://mongodb.com/docs/ruby-driver/current/reference/query-cache/>`_ for more details.
- ``Object#blank_criteria?`` method is removed (was previously deprecated.)


``touch`` method now clears changed state
Expand Down
38 changes: 37 additions & 1 deletion lib/mongoid/association/referenced/has_many/enumerable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 0 additions & 22 deletions lib/mongoid/extensions/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -175,5 +155,3 @@ def resizable?

::Array.__send__(:include, Mongoid::Extensions::Array)
::Array.extend(Mongoid::Extensions::Array::ClassMethods)

::Mongoid.deprecate(Array, :blank_criteria)
50 changes: 0 additions & 50 deletions lib/mongoid/extensions/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -196,5 +148,3 @@ def resizable?

::Hash.__send__(:include, Mongoid::Extensions::Hash)
::Hash.extend(Mongoid::Extensions::Hash::ClassMethods)

::Mongoid.deprecate(Hash, :blank_criteria)
15 changes: 0 additions & 15 deletions lib/mongoid/extensions/object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 0 additions & 28 deletions spec/mongoid/extensions/array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
103 changes: 0 additions & 103 deletions spec/mongoid/extensions/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 0 additions & 7 deletions spec/mongoid/extensions/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fb1d280

Please sign in to comment.