0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
cursor-paginate is a cursor based pagination library for Ruby on Rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 5.2.3
 Project Readme

cursor-paginate Gem Version CircleCI

Cursor based pagination library for Rails.

Usage

# Get the newest 100 posts where id <= cursor
# If cursor is nil, get the newest 100 posts.
@posts = Post.before(id: params[:cursor]).limit(100)

# Get the oldest 100 posts where id >= cursor
# If cursor is nil, get the oldest 100 posts.
@posts = Post.after(id: params[:cursor]).limit(100)

# Also you can use other colum as cursor instead of id
@posts = Post.before(created_at: params[:cursor]).limit(100)

@posts.has_next?   # => true/false
@posts.next_cursor # => value of cursor column/nil

Note that if multiple data have the same cursor value (like created_at at the same time), they appear in duplicate.

In the template

If you use Bootstrap 4.x, pagers are expressed as below:

<nav aria-label="pagination">
  <ul class="pagination">
    <% if @posts.has_next? %>
      <li class="page-item">
        <%= link_to "Next &gt;", { controller: :posts, action: :index, cursor: @posts.next_cursor }, class: "page-link" %>
      </li>
    <% else %>
      <li class="page-item disabled"><a href="#" class="page-link">Next &gt;</a></li>
    <% end %>
  </ul>
</nav>

Installation

Add this line to your application's Gemfile:

gem 'cursor-paginate'

Contributing

Feel free to send PR.

License

The gem is available as open source under the terms of the MIT License.