Skip to content

Commit

Permalink
Fix total_count to use original cursor value (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima authored Dec 16, 2024
1 parent a4b8634 commit 4649fd2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/activerecord_cursor_paginate/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module ActiveRecordCursorPaginate
#
class Page
# Records this page contains.
# @return [ActiveRecord::Base]
# @return [Array<ActiveRecord::Base>]
#
attr_reader :records

Expand Down
18 changes: 10 additions & 8 deletions lib/activerecord_cursor_paginate/paginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def before=(value)
end

@cursor = value || after
@current_cursor = @cursor
@is_forward_pagination = value.blank?
@before = value
end
Expand All @@ -73,6 +74,7 @@ def after=(value)
end

@cursor = before || value
@current_cursor = @cursor
@after = value
end

Expand All @@ -96,7 +98,7 @@ def order=(value)
# @note Calling this method advances the paginator.
#
def fetch
relation = build_cursor_relation
relation = build_cursor_relation(@current_cursor)

relation = relation.limit(@page_size + 1)
records_plus_one = relation.to_a
Expand All @@ -107,9 +109,9 @@ def fetch

if @is_forward_pagination
has_next_page = has_additional
has_previous_page = @cursor.present?
has_previous_page = @current_cursor.present?
else
has_next_page = @cursor.present?
has_next_page = @current_cursor.present?
has_previous_page = has_additional
end

Expand Down Expand Up @@ -144,7 +146,7 @@ def pages
# @return [Integer]
#
def total_count
@total_count ||= build_cursor_relation.count(:all)
@total_count ||= build_cursor_relation(@cursor).count(:all)
end

private
Expand Down Expand Up @@ -176,7 +178,7 @@ def normalize_order(order)
result
end

def build_cursor_relation
def build_cursor_relation(cursor)
relation = @relation

# Non trivial columns (expressions or joined tables columns).
Expand All @@ -199,8 +201,8 @@ def build_cursor_relation
pagination_directions = @directions.map { |direction| pagination_direction(direction) }
relation = relation.reorder(cursor_column_names.zip(pagination_directions).to_h)

if @cursor
decoded_cursor = Cursor.decode(cursor_string: @cursor, columns: @columns)
if cursor
decoded_cursor = Cursor.decode(cursor_string: cursor, columns: @columns)
relation = apply_cursor(relation, decoded_cursor)
end

Expand Down Expand Up @@ -261,7 +263,7 @@ def pagination_operator(direction)
end

def advance_by_page(page)
@cursor =
@current_cursor =
if @is_forward_pagination
page.next_cursor
else
Expand Down
3 changes: 2 additions & 1 deletion test/paginator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ def test_works_with_composite_primary_keys
def test_returns_page_object
user1, user2 = User.first(2)
p = User.cursor_paginate(limit: 2)
assert_equal(User.count, p.total_count)

page1 = p.fetch
assert_equal([user1, user2], page1.records)
Expand All @@ -283,6 +282,8 @@ def test_returns_page_object
page2 = p.fetch
assert page2.has_next?
assert page2.has_previous?

assert_equal(User.count, p.total_count)
end

def test_empty_page
Expand Down

0 comments on commit 4649fd2

Please sign in to comment.