Skip to content

Commit e055336

Browse files
Moving from Criteria::Findable to avoid clobbering Enumerable#any?
1 parent 6cf4b36 commit e055336

File tree

2 files changed

+30
-58
lines changed

2 files changed

+30
-58
lines changed

lib/mongoid/criteria/findable.rb

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -81,63 +81,6 @@ def multiple_from_db(ids)
8181
ids.empty? ? [] : from_database(ids)
8282
end
8383

84-
# Return true if any documents exist in the criteria.
85-
#
86-
# @example Determine if any documents exist
87-
# criteria.any?
88-
#
89-
# @example Determine if any documents match a block
90-
# criteria.any? { |doc| doc.name == "John" }
91-
#
92-
# @param [ Object... ] *args Args to delegate to the target.
93-
#
94-
# @param [ Proc ] &block Block to delegate to the target.
95-
#
96-
# @return [ true | false ] If any documents exist.
97-
def any?(*args, &block)
98-
return entries.any?(*args, &block) if args.any? || block_given?
99-
100-
limit(1).count > 0
101-
end
102-
103-
# Return true if only one document exists in the criteria.
104-
#
105-
# @example Determine if only one document exists
106-
# criteria.one?
107-
#
108-
# @example Determine if only one document matches a block
109-
# criteria.one? { |doc| doc.name == "John" }
110-
#
111-
# @param [ Object... ] *args Args to delegate to the target.
112-
#
113-
# @param [ Proc ] &block Block to delegate to the target.
114-
#
115-
# @return [ true | false ] If only one document exists.
116-
def one?(*args, &block)
117-
return entries.one?(*args, &block) if args.any? || block_given?
118-
119-
limit(2).count == 1
120-
end
121-
122-
# Return true if more than one document exists in the criteria.
123-
#
124-
# @example Determine if many documents exist
125-
# criteria.many?
126-
#
127-
# @example Determine if many documents match a block
128-
# criteria.many? { |doc| doc.name.start_with?("J") }
129-
#
130-
# @param [ Object... ] *args Args to delegate to the target.
131-
#
132-
# @param [ Proc ] &block Block to delegate to the target.
133-
#
134-
# @return [ true | false ] If many documents exist.
135-
def many?(*args, &block)
136-
return entries.many?(*args, &block) if args.any? || block_given?
137-
138-
limit(2).count > 1
139-
end
140-
14184
private
14285

14386
# Get the finder used to generate the id query.

lib/mongoid/findable.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ module Findable
1717
# directly from the class level.
1818
def_delegators :with_default_scope,
1919
:aggregates,
20-
:any?,
2120
:avg,
2221
:create_with,
2322
:distinct,
@@ -119,6 +118,36 @@ def exists?(id_or_conditions = :none)
119118
with_default_scope.exists?(id_or_conditions)
120119
end
121120

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

0 commit comments

Comments
 (0)