0.0
No commit activity in last 3 years
No release in over 3 years
Cursor-based pagination for Rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0

Runtime

~> 6
 Project Readme

page_cursor

gem 'page_cursor'

Cursor-based pagination for Rails.

  • Does not use OFFSET/LIMIT queries.
  • Cursors are primary keys, i.e. {before: "<pk>", after: "<pk>"} and are expected to be present as params[:after] and/or params[:before]. Primary keys must be unique and sortable.
  • Multiple columns can be used for ordering (see examples below).

Works great in combination with KSUIDs as primary keys, but any other sortable key will do.

Usage

@cursor, @users = paginate(User.where(active: true)) # in controller

<%= pagination_nav @cursor %> # in view

Please note that you'll have to create the pagination_nav helper yourself. Have a look at an example helper with its rendered example partial.

More examples

# Example 1
@users = User.where(active: true)
@cursor, @users = paginate(@users, limit: 25)

# Example 2
@cursor, @users = paginate(User)

# Example 3
@users = User.where(active: true)
@cursor, @users = paginate(@users, :desc) # order primary key descending (defaults to :asc)

# Example 4
@users = User.where(active: true).order(:city => :desc)
@cursor, @users = paginate(@users)

# Example 5
@users = User.where(active: true).order(:lastname => :asc, :firstname => :asc, :city => :desc)
@cursor, @users = paginate(@users, :desc) # :desc orders primary key descending

# Example 6 - change position of primary key in sort order
@users = User.where(active: true).order(:city => :asc, :id => :desc, :city => :asc)
@cursor, @users = paginate(@users)

# Example 7
@cursor, @users = paginate(User, primary_key: "custom")