Skip to content

Commit

Permalink
Fix next_cursor/previous_cursor for empty pages
Browse files Browse the repository at this point in the history
  • Loading branch information
JProgrammer authored and fatkodima committed May 15, 2024
1 parent 76c4bf3 commit cba0a0f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Style/SymbolArray:
Style/IfUnlessModifier:
Enabled: false

Style/GuardClause:
Enabled: false

Layout/EmptyLinesAroundAccessModifier:
EnforcedStyle: only_before

Expand Down Expand Up @@ -53,3 +56,6 @@ Minitest/MultipleAssertions:

Minitest/EmptyLineBeforeAssertionMethods:
Enabled: false

Minitest/AssertEmptyLiteral:
Enabled: false
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## master (unreleased)

- Fix `next_cursor`/`previous_cursor` for empty pages
- Fix iterating using only a timestamp column

- Add the ability to skip implicitly appending a primary key to the list of sorting columns.
Expand Down
6 changes: 4 additions & 2 deletions lib/activerecord_cursor_paginate/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ def cursors

private
def cursor_for_record(record)
cursor = Cursor.from_record(record, columns: @order_columns)
cursor.encode
if record
cursor = Cursor.from_record(record, columns: @order_columns)
cursor.encode
end
end
end
end
13 changes: 13 additions & 0 deletions test/paginator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,17 @@ def test_returns_page_object
assert page2.has_next?
assert page2.has_previous?
end

def test_empty_page
p = User.where("created_at > ?", Time.current).cursor_paginate
page = p.fetch

assert_equal([], page.records)
assert_equal(0, page.count)
assert_nil page.next_cursor
assert_nil page.previous_cursor
assert_not page.has_previous?
assert_not page.has_next?
assert_empty page.cursors
end
end

0 comments on commit cba0a0f

Please sign in to comment.