File tree Expand file tree Collapse file tree 5 files changed +66
-3
lines changed Expand file tree Collapse file tree 5 files changed +66
-3
lines changed Original file line number Diff line number Diff line change 181
181
message : " Invalid criteria for estimated_count."
182
182
summary : " Estimated count is strictly a collection-level operation and cannot be called
183
183
on a filtered criteria."
184
- resolution : " Try calling estimated_count directly on the class: %{class_name}.estimated_count.\n\n
185
- \_ If the class defines a default scope, use unscoped: %{class_name}.unscoped.estimated_count."
184
+ resolution : " Try calling estimated_count directly on the class: %{class_name}.estimated_count."
185
+ invalid_estimated_count_scoping :
186
+ message : " Invalid criteria for estimated_count."
187
+ summary : " Estimated count is strictly a collection-level operation and cannot be called
188
+ on a model that uses a default scope."
189
+ resolution : " Try calling estimated_count using unscoped: %{class_name}.unscoped.estimated_count.
190
+ Alternatively, the #count method can be used, which is usable with scoping."
186
191
invalid_expression_operator :
187
192
message : " Invalid expression operator '%{operator}'."
188
193
summary : " You misspelled an operator, are using an operator that
Original file line number Diff line number Diff line change @@ -73,7 +73,11 @@ def count(options = {}, &block)
73
73
# @return [ Integer ] The number of matches.
74
74
def estimated_count ( options = { } )
75
75
unless self . criteria . selector . empty?
76
- raise Mongoid ::Errors ::InvalidEstimatedCountCriteria . new ( self . klass )
76
+ if klass . default_scoping?
77
+ raise Mongoid ::Errors ::InvalidEstimatedCountScoping . new ( self . klass )
78
+ else
79
+ raise Mongoid ::Errors ::InvalidEstimatedCountCriteria . new ( self . klass )
80
+ end
77
81
end
78
82
view . estimated_document_count ( options )
79
83
end
Original file line number Diff line number Diff line change 29
29
require "mongoid/errors/invalid_dot_dollar_assignment"
30
30
require "mongoid/errors/invalid_elem_match_operator"
31
31
require "mongoid/errors/invalid_estimated_count_criteria"
32
+ require "mongoid/errors/invalid_estimated_count_scoping"
32
33
require "mongoid/errors/invalid_expression_operator"
33
34
require "mongoid/errors/invalid_field_operator"
34
35
require "mongoid/errors/invalid_relation"
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ module Mongoid
4
+ module Errors
5
+
6
+ class InvalidEstimatedCountScoping < MongoidError
7
+
8
+ # Creates the exception raised when trying to call estimated_count
9
+ # on Model with a default scope
10
+ #
11
+ # @param [ String ] class_name The klass of the criteria used to call
12
+ # estimated count.
13
+ #
14
+ # @api private
15
+ def initialize ( class_name )
16
+ super (
17
+ compose_message (
18
+ "invalid_estimated_count_scoping" ,
19
+ { class_name : class_name }
20
+ )
21
+ )
22
+ end
23
+ end
24
+ end
25
+ end
Original file line number Diff line number Diff line change 223
223
end
224
224
end
225
225
end
226
+
227
+ context "when including a default scope" do
228
+
229
+ let ( :criteria ) do
230
+ Band . where ( name : "New Order" )
231
+ end
232
+
233
+ before do
234
+ 5 . times { Band . create! }
235
+ Band . default_scope -> { criteria }
236
+ end
237
+
238
+ after do
239
+ Band . default_scoping = nil
240
+ end
241
+
242
+ it 'raises an error' do
243
+ expect do
244
+ Band . estimated_count
245
+ end . to raise_error ( Mongoid ::Errors ::InvalidEstimatedCountScoping )
246
+ end
247
+
248
+ it "does not raise an error on unscoped" do
249
+ expect do
250
+ expect ( Band . unscoped . estimated_count ) . to eq ( 5 )
251
+ end
252
+ end
253
+ end
226
254
end
227
255
228
256
You can’t perform that action at this time.
0 commit comments