total_count has to be retrieved from DB if loaded AR::Relation is empty
fixes #845
This commit is contained in:
parent
c5612094ec
commit
11d4aaf3ed
|
@ -21,7 +21,7 @@ module Kaminari
|
|||
# Total count has to be 0 if loaded records are 0
|
||||
return @total_count = 0 if (current_page == 1) && @records.empty?
|
||||
# Total count is calculatable at the last page
|
||||
return @total_count = (current_page - 1) * @_per + @records.length if defined?(@_per) && (@records.length < @_per)
|
||||
return @total_count = (current_page - 1) * @_per + @records.length if @records.any? && defined?(@_per) && (@records.length < @_per)
|
||||
end
|
||||
|
||||
# #count overrides the #select which could include generated columns referenced in #order, so skip #order here, where it's irrelevant to the result anyway
|
||||
|
|
|
@ -20,6 +20,20 @@ if defined? ActiveRecord
|
|||
Readership.delete_all
|
||||
end
|
||||
|
||||
test 'total_count on not yet loaded Relation' do
|
||||
assert_equal 0, User.where('1 = 0').page(1).per(10).total_count
|
||||
assert_equal 7, User.page(1).per(10).total_count
|
||||
assert_equal 7, User.page(2).per(10).total_count
|
||||
assert_equal 7, User.page(2).per(2).total_count
|
||||
end
|
||||
|
||||
test 'total_count on loded Relation' do
|
||||
assert_equal 0, User.where('1 = 0').page(1).per(10).load.total_count
|
||||
assert_equal 7, User.page(1).per(10).load.total_count
|
||||
assert_equal 7, User.page(2).per(10).load.total_count
|
||||
assert_equal 7, User.page(2).per(2).load.total_count
|
||||
end
|
||||
|
||||
test 'it should reset total_count memoization when the scope is cloned' do
|
||||
assert_equal 1, User.page.tap(&:total_count).where(name: 'author').total_count
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue