From daf3d8568eb55af5b1e622877776836b64c93934 Mon Sep 17 00:00:00 2001 From: fatkodima Date: Tue, 13 Aug 2024 16:35:32 +0300 Subject: [PATCH] Add ability to incrementally configure a paginator --- CHANGELOG.md | 2 + lib/activerecord_cursor_paginate/paginator.rb | 43 +++++++++++++++---- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f548a4b..c891cc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## master (unreleased) +- Add ability to incrementally configure a paginator + - Add ability to get the total number of records ```ruby diff --git a/lib/activerecord_cursor_paginate/paginator.rb b/lib/activerecord_cursor_paginate/paginator.rb index 52e9474..cc5619d 100644 --- a/lib/activerecord_cursor_paginate/paginator.rb +++ b/lib/activerecord_cursor_paginate/paginator.rb @@ -19,6 +19,8 @@ module ActiveRecordCursorPaginate # end # class Paginator + attr_reader :relation, :before, :after, :limit, :order, :append_primary_key + # Create a new instance of the `ActiveRecordCursorPaginate::Paginator` # # @param relation [ActiveRecord::Relation] Relation that will be paginated. @@ -45,24 +47,47 @@ def initialize(relation, before: nil, after: nil, limit: nil, order: nil, append raise ArgumentError, "relation is not an ActiveRecord::Relation" end - if before.present? && after.present? + @relation = relation + @primary_key = @relation.primary_key + @append_primary_key = append_primary_key + + self.before = before + self.after = after + self.limit = limit + self.order = order + end + + def before=(value) + if value.present? && after.present? raise ArgumentError, "Only one of :before and :after can be provided" end - @relation = relation - @primary_key = @relation.primary_key - @cursor = before || after - @is_forward_pagination = before.blank? + @cursor = value || after + @is_forward_pagination = value.blank? + @before = value + end + + def after=(value) + if before.present? && value.present? + raise ArgumentError, "Only one of :before and :after can be provided" + end + @cursor = before || value + @after = value + end + + def limit=(value) config = ActiveRecordCursorPaginate.config - @page_size = limit || config.default_page_size + @page_size = value || config.default_page_size @page_size = [@page_size, config.max_page_size].min if config.max_page_size + @limit = value + end - @append_primary_key = append_primary_key - order = normalize_order(order) + def order=(value) + order = normalize_order(value) @columns = order.keys @directions = order.values - @total_count = nil + @order = value end # Get the paginated result.