Skip to content

Commit

Permalink
Add ability to incrementally configure a paginator
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Dec 10, 2024
1 parent 8fb1904 commit daf3d85
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## master (unreleased)

- Add ability to incrementally configure a paginator

- Add ability to get the total number of records

```ruby
Expand Down
43 changes: 34 additions & 9 deletions lib/activerecord_cursor_paginate/paginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit daf3d85

Please sign in to comment.