Skip to content

Commit 3406353

Browse files
committed
With this approach mongoid will fire only one count query
The previous version caused 3 query: 1. collection.empty? => load an item to ensure there are items in the collection 2. collection.first ... => again load an item to be sure about we querying mongo 3. collection.count(true) => actual count command
1 parent 3fa3b4b commit 3406353

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

lib/active_admin/mongoid/helpers/collection.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ module Collection
44

55
alias original_collection_size collection_size
66
original_collection_size = instance_method(:collection_size)
7+
78
def collection_size(collection=collection)
8-
if(not collection.empty? and collection.first.class.included_modules.include?(Mongoid::Document))
9+
if collection.is_a?(::Mongoid::Criteria)
910
collection.count(true)
1011
else
1112
original_collection_size(collection)
1213
end
1314
end
15+
1416
end
1517
end
1618
end

0 commit comments

Comments
 (0)