Module: ActiveItem::Pagination
- Defined in:
- lib/active_item/pagination.rb
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
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/active_item/pagination.rb', line 8 def self.paginate_array(items, cursor = nil, per_page: DEFAULT_PER_PAGE) per_page = [[per_page.to_i, 1].max, MAX_PER_PAGE].min 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 |