Module: ActiveItem::Pagination
- Defined in:
- lib/active_item/pagination.rb
Overview
Cursor-based pagination for in-memory arrays of ActiveItem records. Provides a paginate_array helper and a PaginatedResult wrapper.
Defined Under Namespace
Classes: PaginatedResult
Constant Summary collapse
- DEFAULT_PER_PAGE =
25- MAX_PER_PAGE =
100
Class Method Summary collapse
Class Method Details
.paginate_array(items, cursor = nil, per_page: DEFAULT_PER_PAGE) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/active_item/pagination.rb', line 10 def self.paginate_array(items, cursor = nil, per_page: DEFAULT_PER_PAGE) per_page = per_page.to_i.clamp(1, MAX_PER_PAGE) if cursor && !cursor.empty? cursor_time, cursor_id = cursor.split('|', 2) items = items.drop_while { |i| ([i.created_at || '', i.id] <=> [cursor_time, cursor_id]) >= 0 } end page_items = items.first(per_page) has_more = items.length > per_page last = page_items.last next_cursor = has_more && last ? "#{last.created_at}|#{last.id}" : nil PaginatedResult.new(items: page_items, next_cursor: next_cursor, per_page: per_page) end |