Skip to content

Commit 17d8df3

Browse files
MONGOID-5910 Implement enumerable methods on top level models (any?, many?, and one?) (#6069)
* Using .count to implement any? one? and many?, adding testing * Reverting change to .gitignore * removing unneeded code * Allowing a block of code/optional args to be passed to better align with Ruby's any? method * Small change * Moving from Criteria::Findable to avoid clobbering Enumerable#any? * removing unneeded delegates * Moving tests to different file * removing extra nesting * Removed unnecessary spacing and ()
1 parent bfdf0ab commit 17d8df3

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

lib/mongoid/findable.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,36 @@ def exists?(id_or_conditions = :none)
116116
with_default_scope.exists?(id_or_conditions)
117117
end
118118

119+
# Return true if any documents exist in the criteria.
120+
#
121+
# @example Determine if any documents exist
122+
# Model.any?
123+
#
124+
# @return [ true | false ] If any documents exist.
125+
def any?
126+
limit(1).count > 0
127+
end
128+
129+
# Return true if only one document exists in the criteria.
130+
#
131+
# @example Determine if only one document exists
132+
# Model.one?
133+
#
134+
# @return [ true | false ] If only one document exists.
135+
def one?
136+
limit(2).count == 1
137+
end
138+
139+
# Return true if more than one document exists in the criteria.
140+
#
141+
# @example Determine if many documents exist
142+
# Model.many?
143+
#
144+
# @return [ true | false ] If many documents exist.
145+
def many?
146+
limit(2).count > 1
147+
end
148+
119149
# Finds a +Document+ or multiple documents by their _id values.
120150
#
121151
# If a single non-Array argument is given, this argument is interpreted

spec/mongoid/findable_spec.rb

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,84 @@
160160
end
161161
end
162162

163+
describe "#any?" do
164+
context "when called on collection with documents" do
165+
before do
166+
Band.create!(name: "Tool")
167+
end
168+
169+
it "returns true" do
170+
expect(Band.any?).to be true
171+
end
172+
end
173+
174+
context "when called on collection with no documents" do
175+
176+
it "returns false" do
177+
expect(Band.any?).to be false
178+
end
179+
end
180+
end
181+
182+
describe "#one?" do
183+
context "when called on collection with no documents" do
184+
it "returns false" do
185+
expect(Band.one?).to be false
186+
end
187+
end
188+
189+
context "when called on collection with one document" do
190+
before do
191+
Band.create!(name: "Radiohead")
192+
end
193+
194+
it "returns true" do
195+
expect(Band.one?).to be true
196+
end
197+
end
198+
199+
context "when called on collection with multiple documents" do
200+
before do
201+
Band.create!(name: "Tool")
202+
Band.create!(name: "Radiohead")
203+
end
204+
205+
it "returns false" do
206+
expect(Band.one?).to be false
207+
end
208+
end
209+
210+
end
211+
212+
describe "#many?" do
213+
context "when called on collection with no documents" do
214+
it "returns false" do
215+
expect(Band.many?).to be false
216+
end
217+
end
218+
219+
context "when called on collection with one document" do
220+
before do
221+
Band.create!(name: "Radiohead")
222+
end
223+
224+
it "returns false" do
225+
expect(Band.many?).to be false
226+
end
227+
end
228+
229+
context "when called on collection with multiple documents" do
230+
before do
231+
Band.create!(name: "Radiohead")
232+
Band.create!(name: "Tool")
233+
end
234+
235+
it "returns true" do
236+
expect(Band.many?).to be true
237+
end
238+
end
239+
end
240+
163241
describe "find_by!" do
164242

165243
context "when the document is found" do
@@ -927,4 +1005,4 @@
9271005
expect(User.distinct(:name)).to match_array(['Tom'])
9281006
end
9291007
end
930-
end
1008+
end

0 commit comments

Comments
 (0)