Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix next_cursor/previous_cursor for empty pages #6

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading