diff --git a/lib/activerecord_cursor_paginate/page.rb b/lib/activerecord_cursor_paginate/page.rb index a08e42b..089418e 100644 --- a/lib/activerecord_cursor_paginate/page.rb +++ b/lib/activerecord_cursor_paginate/page.rb @@ -6,7 +6,7 @@ module ActiveRecordCursorPaginate # class Page # Records this page contains. - # @return [ActiveRecord::Base] + # @return [Array] # attr_reader :records diff --git a/lib/activerecord_cursor_paginate/paginator.rb b/lib/activerecord_cursor_paginate/paginator.rb index cc5619d..dc1b9d9 100644 --- a/lib/activerecord_cursor_paginate/paginator.rb +++ b/lib/activerecord_cursor_paginate/paginator.rb @@ -63,6 +63,7 @@ def before=(value) end @cursor = value || after + @current_cursor = @cursor @is_forward_pagination = value.blank? @before = value end @@ -73,6 +74,7 @@ def after=(value) end @cursor = before || value + @current_cursor = @cursor @after = value end @@ -96,7 +98,7 @@ def order=(value) # @note Calling this method advances the paginator. # def fetch - relation = build_cursor_relation + relation = build_cursor_relation(@current_cursor) relation = relation.limit(@page_size + 1) records_plus_one = relation.to_a @@ -107,9 +109,9 @@ def fetch if @is_forward_pagination has_next_page = has_additional - has_previous_page = @cursor.present? + has_previous_page = @current_cursor.present? else - has_next_page = @cursor.present? + has_next_page = @current_cursor.present? has_previous_page = has_additional end @@ -144,7 +146,7 @@ def pages # @return [Integer] # def total_count - @total_count ||= build_cursor_relation.count(:all) + @total_count ||= build_cursor_relation(@cursor).count(:all) end private @@ -176,7 +178,7 @@ def normalize_order(order) result end - def build_cursor_relation + def build_cursor_relation(cursor) relation = @relation # Non trivial columns (expressions or joined tables columns). @@ -199,8 +201,8 @@ def build_cursor_relation pagination_directions = @directions.map { |direction| pagination_direction(direction) } relation = relation.reorder(cursor_column_names.zip(pagination_directions).to_h) - if @cursor - decoded_cursor = Cursor.decode(cursor_string: @cursor, columns: @columns) + if cursor + decoded_cursor = Cursor.decode(cursor_string: cursor, columns: @columns) relation = apply_cursor(relation, decoded_cursor) end @@ -261,7 +263,7 @@ def pagination_operator(direction) end def advance_by_page(page) - @cursor = + @current_cursor = if @is_forward_pagination page.next_cursor else diff --git a/test/paginator_test.rb b/test/paginator_test.rb index f394649..02e4a23 100644 --- a/test/paginator_test.rb +++ b/test/paginator_test.rb @@ -267,7 +267,6 @@ def test_works_with_composite_primary_keys def test_returns_page_object user1, user2 = User.first(2) p = User.cursor_paginate(limit: 2) - assert_equal(User.count, p.total_count) page1 = p.fetch assert_equal([user1, user2], page1.records) @@ -283,6 +282,8 @@ def test_returns_page_object page2 = p.fetch assert page2.has_next? assert page2.has_previous? + + assert_equal(User.count, p.total_count) end def test_empty_page