Skip to content

Conversation

@kyleburgess2025
Copy link
Contributor

This PR adds the enumerable methods any, many, and one to the Criteria class. It also delegates calls to these functions at the class level to the methods defined on Criteria. For example, User.any? should return if any documents exist in the users table.

end
end

context "when called on class" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me know if this needs to be moved elsewhere or isnt needed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this should probably be moved to spec/mongoid/findable_spec.rb, since it's no longer implemented on Mongoid::Criteria.

When you move these, you should also make sure the tests invoke the new methods directly on the model classes, rather than on criteria instances. E.g.

let(:model) { Band } # instance of :criteria

# ...

expect(model.any?).to be true

# criteria.any?
#
# @return [ true | false ] If any documents exist.
def any?(*args, &block)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safe to leave off the *args and &block here, since they aren't used in the method body anymore.

Ditto for the other two methods you added. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!

# Return true if any documents exist in the criteria.
#
# @example Determine if any documents exist
# criteria.any?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is an artifact from when you had this on Mongoid::Criteria, but for documentation purposes you can just reference Model, generically.

Suggested change
# criteria.any?
# Model.any?

Ditto for the other new methods you added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!

end
end

context "when called on class" do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this should probably be moved to spec/mongoid/findable_spec.rb, since it's no longer implemented on Mongoid::Criteria.

When you move these, you should also make sure the tests invoke the new methods directly on the model classes, rather than on criteria instances. E.g.

let(:model) { Band } # instance of :criteria

# ...

expect(model.any?).to be true

@kyleburgess2025 kyleburgess2025 requested a review from jamis December 5, 2025 21:58
@kyleburgess2025 kyleburgess2025 marked this pull request as ready for review December 8, 2025 17:54
@kyleburgess2025 kyleburgess2025 requested a review from a team as a code owner December 8, 2025 17:54
Copilot AI review requested due to automatic review settings December 8, 2025 17:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements three enumerable methods (any?, many?, and one?) on Mongoid models to check document existence patterns. These methods are added to the Findable module and can be called directly on model classes (e.g., Band.any?) to query the database efficiently.

  • Adds any? method to check if at least one document exists
  • Adds one? method to check if exactly one document exists
  • Adds many? method to check if more than one document exists

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
lib/mongoid/findable.rb Implements the three enumerable methods with optimized queries using limit clauses
spec/mongoid/findable_spec.rb Adds comprehensive test coverage for all three methods across different document count scenarios

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

# Model.any?
#
# @return [ true | false ] If any documents exist.
def any?()
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary empty parentheses from method definitions. In Ruby, parentheses are optional when a method takes no parameters.

Copilot uses AI. Check for mistakes.
# Model.one?
#
# @return [ true | false ] If only one document exists.
def one?()
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary empty parentheses from method definitions. In Ruby, parentheses are optional when a method takes no parameters.

Copilot uses AI. Check for mistakes.
# Model.many?
#
# @return [ true | false ] If many documents exist.
def many?()
Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unnecessary empty parentheses from method definitions. In Ruby, parentheses are optional when a method takes no parameters.

Copilot uses AI. Check for mistakes.
expect(Band.one?).to be false
end
end

Copy link

Copilot AI Dec 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra blank line before 'end' to maintain consistent spacing with other test blocks in this file.

Suggested change

Copilot uses AI. Check for mistakes.
@kyleburgess2025 kyleburgess2025 merged commit 17d8df3 into mongodb:master Dec 9, 2025
70 of 71 checks passed
@kyleburgess2025 kyleburgess2025 deleted the MONGOID-5910 branch December 9, 2025 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants