-
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
base: master
Are you sure you want to change the base?
Changes from 7 commits
a3313fc
310a2d0
0412484
8d8b8ae
6cf4b36
e055336
377bbd5
d7c5298
b37cf86
9416996
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,6 +116,36 @@ def exists?(id_or_conditions = :none) | |
| with_default_scope.exists?(id_or_conditions) | ||
| end | ||
|
|
||
| # Return true if any documents exist in the criteria. | ||
| # | ||
| # @example Determine if any documents exist | ||
| # criteria.any? | ||
| # | ||
| # @return [ true | false ] If any documents exist. | ||
| def any?(*args, &block) | ||
|
||
| limit(1).count > 0 | ||
| end | ||
|
|
||
| # Return true if only one document exists in the criteria. | ||
| # | ||
| # @example Determine if only one document exists | ||
| # criteria.one? | ||
| # | ||
| # @return [ true | false ] If only one document exists. | ||
| def one?(*args, &block) | ||
| limit(2).count == 1 | ||
| end | ||
|
|
||
| # Return true if more than one document exists in the criteria. | ||
| # | ||
| # @example Determine if many documents exist | ||
| # criteria.many? | ||
| # | ||
| # @return [ true | false ] If many documents exist. | ||
| def many?(*args, &block) | ||
| limit(2).count > 1 | ||
| end | ||
|
|
||
| # Finds a +Document+ or multiple documents by their _id values. | ||
| # | ||
| # If a single non-Array argument is given, this argument is interpreted | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1302,6 +1302,201 @@ | |
| end | ||
| end | ||
|
|
||
| describe "#any?" do | ||
| context "when called on criteria" do | ||
| let!(:band) do | ||
| Band.create!(name: "Depeche Mode") | ||
| end | ||
|
|
||
| let!(:band_two) do | ||
| Band.create!(name: "Radiohead") | ||
| end | ||
|
|
||
| context "when documents exist" do | ||
|
|
||
| let(:criteria) do | ||
| Band.all | ||
| end | ||
|
|
||
| it "returns true" do | ||
| expect(criteria.any?).to be true | ||
| end | ||
| end | ||
|
|
||
| context "when no documents exist" do | ||
|
|
||
| let(:criteria) do | ||
| Band.where(name: "Nonexistent Band") | ||
| end | ||
|
|
||
| it "returns false" do | ||
| expect(criteria.any?).to be false | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context "when called on class" do | ||
| context "with documents" do | ||
| before do | ||
| Band.create!(name: "New Band") | ||
| end | ||
|
|
||
| it "returns true" do | ||
| expect(Band.any?).to be true | ||
| end | ||
| end | ||
|
|
||
| context "with no documents" do | ||
| it "returns false" do | ||
| expect(Band.any?).to be false | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#one?" do | ||
| context "when called on criteria" do | ||
| let!(:band) do | ||
| Band.create!(name: "Depeche Mode") | ||
| end | ||
|
|
||
| let!(:band_two) do | ||
| Band.create!(name: "Radiohead") | ||
| end | ||
|
|
||
| context "when one document exists in criteria" do | ||
| let(:criteria) do | ||
| Band.where(name: "Depeche Mode") | ||
| end | ||
|
|
||
| it "returns true" do | ||
| expect(criteria.one?).to be true | ||
| end | ||
| end | ||
|
|
||
| context "when multiple documents exist in criteria" do | ||
| let(:criteria) do | ||
| Band.all | ||
| end | ||
|
|
||
| it "returns false" do | ||
| expect(criteria.one?).to be false | ||
| end | ||
| end | ||
|
|
||
| context "when no documents exist in criteria" do | ||
| let(:criteria) do | ||
| Band.where(name: "Nonexistent Band") | ||
| end | ||
|
|
||
| it "returns false" do | ||
| expect(criteria.one?).to be false | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context "when called on class" do | ||
|
||
| context "with no documents" do | ||
| it "returns false" do | ||
| expect(Band.one?).to be false | ||
| end | ||
| end | ||
|
|
||
| context "with one document" do | ||
| before do | ||
| Band.create!(name: "Another Band") | ||
| end | ||
|
|
||
| it "returns true" do | ||
| expect(Band.one?).to be true | ||
| end | ||
| end | ||
|
|
||
| context "with multiple documents" do | ||
| before do | ||
| Band.create!(name: "New Band") | ||
| Band.create!(name: "Yet Another Band") | ||
| end | ||
|
|
||
| it "returns false" do | ||
| expect(Band.one?).to be false | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe "#many?" do | ||
| context "when called on criteria" do | ||
| let!(:band) do | ||
| Band.create!(name: "Depeche Mode") | ||
| end | ||
|
|
||
| let!(:band_two) do | ||
| Band.create!(name: "Radiohead") | ||
| end | ||
|
|
||
| context "when multiple documents exist in criteria" do | ||
| let(:criteria) do | ||
| Band.all | ||
| end | ||
|
|
||
| it "returns true" do | ||
| expect(criteria.many?).to be true | ||
| end | ||
| end | ||
|
|
||
| context "when one document exists in criteria" do | ||
| let(:criteria) do | ||
| Band.where(name: "Depeche Mode") | ||
| end | ||
|
|
||
| it "returns false" do | ||
| expect(criteria.many?).to be false | ||
| end | ||
| end | ||
|
|
||
| context "when no documents exist in criteria" do | ||
| let(:criteria) do | ||
| Band.where(name: "Nonexistent Band") | ||
| end | ||
|
|
||
| it "returns false" do | ||
| expect(criteria.many?).to be false | ||
| end | ||
| end | ||
|
|
||
| end | ||
|
|
||
| context "when called on class" do | ||
| context "with no documents" do | ||
| it "returns false" do | ||
| expect(Band.many?).to be false | ||
| end | ||
| end | ||
|
|
||
| context "with one document" do | ||
| before do | ||
| Band.create!(name: "Another Band") | ||
| end | ||
|
|
||
| it "returns false" do | ||
| expect(Band.many?).to be false | ||
| end | ||
| end | ||
|
|
||
| context "with multiple documents" do | ||
| before do | ||
| Band.create!(name: "New Band") | ||
| Band.create!(name: "Yet Another Band") | ||
| end | ||
|
|
||
| it "returns true" do | ||
| expect(Band.many?).to be true | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '#from_database_selector' do | ||
| let(:criteria) { Mongoid::Criteria.new(Band) } | ||
| let(:result) { criteria.send(:from_database_selector, [1]) } | ||
|
|
||
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 referenceModel, generically.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!