Skip to content

Commit

Permalink
Fix cursor decoding with only one time column
Browse files Browse the repository at this point in the history
When the cursor is created with only one time column, decoding it would
crash, reporting that the columns and values have different sizes.

This is because `Array(values)` attempts to call `#to_ary` and `#to_a`
on its arguments before manually wrapping it in an array. This means
that `Time#to_a` is called, which returns an array of 10 elements.

The remedy is to do that manual wrapping ourselves, so that we can
ensure that the values are always wrapped in an array, and then call
`flatten` so that if an array was passed in, it is unwrapped back into
a single level array.
  • Loading branch information
scottmatthewman committed May 12, 2024
1 parent 0c26280 commit 503ce51
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/activerecord_cursor_paginate/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def deserialize_time_if_needed(value)
attr_reader :columns, :values

def initialize(columns:, values:)
@columns = Array(columns)
@values = Array(values)
@columns = [columns].flatten
@values = [values].flatten

raise ArgumentError, "Cursor values can not be nil" if @values.any?(nil)
raise ArgumentError, ":columns and :values have different sizes" if @columns.size != @values.size
Expand Down
9 changes: 9 additions & 0 deletions test/paginator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ def test_paginating_over_time_column
assert_equal([7, 4, 10], page2.records.pluck(:id))
end

def test_paginating_over_time_column_without_primary_key
p = User.cursor_paginate(limit: 3, order: { created_at: :asc }, append_primary_key: false)
page1 = p.fetch
assert_equal([5, 3, 8], page1.records.pluck(:id))

page2 = p.fetch
assert_equal([7, 4, 10], page2.records.pluck(:id))
end

def test_paginating_over_all_pages
p = User.cursor_paginate(limit: 2)

Expand Down

0 comments on commit 503ce51

Please sign in to comment.