From 72100eb69afc71ea049642421645a955d72fb793 Mon Sep 17 00:00:00 2001 From: Cameron Crothers Date: Wed, 15 May 2024 10:09:06 +1000 Subject: [PATCH] Return nil for cursor_for_record when record is nil --- Gemfile.lock | 2 ++ lib/activerecord_cursor_paginate/page.rb | 2 ++ test/paginator_test.rb | 13 +++++++++++++ 3 files changed, 17 insertions(+) diff --git a/Gemfile.lock b/Gemfile.lock index ca41fb7..bca3f37 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -72,6 +73,7 @@ GEM unicode-display_width (2.5.0) PLATFORMS + arm64-darwin-23 ruby x86_64-darwin-21 x86_64-linux diff --git a/lib/activerecord_cursor_paginate/page.rb b/lib/activerecord_cursor_paginate/page.rb index 6b72527..77e9b66 100644 --- a/lib/activerecord_cursor_paginate/page.rb +++ b/lib/activerecord_cursor_paginate/page.rb @@ -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 diff --git a/test/paginator_test.rb b/test/paginator_test.rb index edc9145..dd8eb27 100644 --- a/test/paginator_test.rb +++ b/test/paginator_test.rb @@ -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