The simplest, most flexible, most standards-compliant pagination gem there is. Pairs nicely with begriffs/angular-paginate-anything.
class ApplicationController < ActionController::Base
include CleanPagination
# Using activemodel
def index
max_per_page = 100
paginate Bla.count, max_per_page do |limit, offset|
render json: Bla.limit(limit).offset(offset)
end
end
# Using some custom data
def numbers
paginate Float::INFINITY, 100 do |limit, offset|
render json: (offset...offset+limit).to_a
end
end
# Using optional settings
def options
begin
paginate Foo.scope.count, max_per_page, allow_render: false, raise_errors: true do |limit, offset|
respond_with Foo.scope.limit(limit).offset(offset)
end
rescue RangeError
render json: { error: { message: "invalid pagination range" } }
end
end
end
per_page
and
page
parameters, allows the client to request any (possibly unbounded)
interval of items.To prevent this gem from rendering while still allowing it to set
headers and response codes, pass allow_render: false
to paginate
.
This may be used to avoid DoubleRenderError
situations.
To handle the invalid request range condition in your app, pass the
raise_errors: true
option. This will raise a RangeError
which you can
rescue (and thus control what is rendered). Headers will still be set.
[TODO: explain what the headers mean.] Until this is written you can consult the tests for an idea how it works, or use a client that is compatible, such as begriffs/angular-paginate-anything.