Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MONGOID-5780 Fix chaining nots resulting in incorrect negation state (backport for 8.1) #5915

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/mongoid/criteria/queryable/selectable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def negating?
# @return [ Selectable ] The new selectable.
def not(*criteria)
if criteria.empty?
dup.tap { |query| query.negating = true }
dup.tap { |query| query.negating = !query.negating }
else
criteria.compact.inject(self.clone) do |c, new_s|
if new_s.is_a?(Selectable)
Expand Down
29 changes: 29 additions & 0 deletions spec/mongoid/criteria/queryable/selectable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2059,6 +2059,35 @@ def localized?
end
end

describe "#not" do
context "when negating a criterion" do
let(:selection) do
query.not(field: /value/)
end

it "adds the $not selector" do
expect(selection.selector).to eq({
"field" => { "$not" => /value/ }
})
end

it "returns a cloned query" do
expect(selection).to_not equal(query)
end

context "when toggling negation state" do
it "negates the negating value" do
expect(query.negating).to be_nil
negated_query = query.not
expect(negated_query.negating).to be true
double_negated_query = negated_query.not
expect(double_negated_query.negating).to be false
end
end
end
end


describe Symbol do

describe "#all" do
Expand Down
Loading