diff --git a/.rubocop.yml b/.rubocop.yml index 0762e5f..4560ac7 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -21,6 +21,9 @@ Style/SymbolArray: Style/IfUnlessModifier: Enabled: false +Style/GuardClause: + Enabled: false + Layout/EmptyLinesAroundAccessModifier: EnforcedStyle: only_before @@ -53,3 +56,6 @@ Minitest/MultipleAssertions: Minitest/EmptyLineBeforeAssertionMethods: Enabled: false + +Minitest/AssertEmptyLiteral: + Enabled: false diff --git a/CHANGELOG.md b/CHANGELOG.md index 7846ce5..e5631b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/activerecord_cursor_paginate/page.rb b/lib/activerecord_cursor_paginate/page.rb index 6b72527..a08e42b 100644 --- a/lib/activerecord_cursor_paginate/page.rb +++ b/lib/activerecord_cursor_paginate/page.rb @@ -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 diff --git a/test/paginator_test.rb b/test/paginator_test.rb index edc9145..4de3ed9 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 + 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