Module: Chronicle::Pagination

Extended by:
ActiveSupport::Concern
Included in:
ResourceController
Defined in:
app/controllers/concerns/chronicle/pagination.rb

Constant Summary collapse

DEFAULT_LIMIT =
20
MAX_LIMIT =
100

Instance Method Summary collapse

Instance Method Details

#paginate(scope) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/concerns/chronicle/pagination.rb', line 8

def paginate(scope)
  limit = validate_limit
  last_id = params[:last_id]&.to_i

  scope = scope.where(id: ...last_id) if last_id.present?
  scope = scope.order(id: order_key).limit(limit)
  batch_count = scope.size

  {
    data: scope.map { |record| item_data(record) },
    pagination: {
      has_more: batch_count == limit,
      last_id: scope.last&.id,
      batch_count: batch_count,
    },
  }
end