-
Notifications
You must be signed in to change notification settings - Fork 1.4k
MONGOID-5910 Implement enumerable methods on top level models (any?, many?, and one?) #6069
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
Conversation
| end | ||
| end | ||
|
|
||
| context "when called on class" do |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
lib/mongoid/findable.rb
Outdated
| # criteria.any? | ||
| # | ||
| # @return [ true | false ] If any documents exist. | ||
| def any?(*args, &block) |
There was a problem hiding this comment.
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. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed!
lib/mongoid/findable.rb
Outdated
| # Return true if any documents exist in the criteria. | ||
| # | ||
| # @example Determine if any documents exist | ||
| # criteria.any? |
There was a problem hiding this comment.
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.
| # criteria.any? | |
| # Model.any? |
Ditto for the other new methods you added.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 trueThere was a problem hiding this 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.
lib/mongoid/findable.rb
Outdated
| # Model.any? | ||
| # | ||
| # @return [ true | false ] If any documents exist. | ||
| def any?() |
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
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.
lib/mongoid/findable.rb
Outdated
| # Model.one? | ||
| # | ||
| # @return [ true | false ] If only one document exists. | ||
| def one?() |
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
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.
lib/mongoid/findable.rb
Outdated
| # Model.many? | ||
| # | ||
| # @return [ true | false ] If many documents exist. | ||
| def many?() |
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
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.
| expect(Band.one?).to be false | ||
| end | ||
| end | ||
|
|
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
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.
This PR adds the enumerable methods
any,many, andoneto theCriteriaclass. It also delegates calls to these functions at the class level to the methods defined onCriteria. For example,User.any?should return if any documents exist in theuserstable.