Skip to content

Commit 4e2149d

Browse files
authored
Merge pull request #21 from healthie/fix-total_count
Fix `Paginator#total_count` to not change when paginating
2 parents 5419731 + 5852e28 commit 4e2149d

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## master (unreleased)
22

3+
- Fix `Paginator#total_count` to not change when paginating
34
- Fix `Paginator#limit` to return default page size by default
45

56
## 0.4.0 (2025-03-10)

lib/activerecord_cursor_paginate/paginator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def pages
184184
# @return [Integer]
185185
#
186186
def total_count
187-
@total_count ||= build_cursor_relation(@cursor).count(:all)
187+
@total_count ||= @relation.count(:all)
188188
end
189189

190190
private

test/paginator_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,16 @@ def test_does_not_append_id_if_asked
149149
end
150150

151151
def test_paginates_forward_after_cursor
152+
total_count = User.count
153+
152154
p1 = User.cursor_paginate(limit: 3)
155+
assert_equal total_count, p1.total_count
156+
153157
page1 = p1.fetch
154158

155159
p2 = User.cursor_paginate(after: page1.cursor, limit: 4)
160+
assert_equal total_count, p2.total_count
161+
156162
page2 = p2.fetch
157163
assert_equal([4, 5, 6, 7], page2.records.pluck(:id))
158164
end

0 commit comments

Comments
 (0)