Skip to content

Commit

Permalink
Return nil for cursor_for_record when record is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
JProgrammer committed May 15, 2024
1 parent 76c4bf3 commit 72100eb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ GEM
ruby-progressbar (1.13.0)
sqlite3 (1.7.2)
mini_portile2 (~> 2.8.0)
sqlite3 (1.7.2-arm64-darwin)
sqlite3 (1.7.2-x86_64-darwin)
sqlite3 (1.7.2-x86_64-linux)
timeout (0.4.1)
Expand All @@ -72,6 +73,7 @@ GEM
unicode-display_width (2.5.0)

PLATFORMS
arm64-darwin-23
ruby
x86_64-darwin-21
x86_64-linux
Expand Down
2 changes: 2 additions & 0 deletions lib/activerecord_cursor_paginate/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def cursors

private
def cursor_for_record(record)
return nil unless record

cursor = Cursor.from_record(record, columns: @order_columns)
cursor.encode
end
Expand Down
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()
page1 = p.fetch

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

0 comments on commit 72100eb

Please sign in to comment.