Pageturner
Cursor based pagination for activerecord queries.
Usage
In your controllers:
query = Model.where(params[:filters])
@pagination = Pageturner.new(
anchor_column: "name",
anchor_id: params[:last_id],
anchor_value: params[:last_value],
ar_relation: query,
sort_direction: Pageturner::DESC,
page_size: 25,
path_helper: lambda do |anchor_column:, anchor_value:, sort_direction:, anchor_id:|
models_path(
anchor_column: anchor_column,
anchor_value: anchor_value,
sort_direction: sort_direction,
anchor_id: anchor_id
)
end
)
In your views:
json.models @pagination.resources do |resource|
json.partial! 'attributes', model: resource
end
json.pagination do
json.next_page @pagination.next_page
end
API
#next_page
Returns the next page's path.
#resources
Returns the current page's collection.
Installation
Add this line to your application's Gemfile:
gem 'pageturner'
And then execute:
$ bundle
Or install it yourself as:
$ gem install pageturner
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/crzrcn/pageturner.