Skip to content

Latest commit

 

History

History
122 lines (83 loc) · 3.71 KB

README.md

File metadata and controls

122 lines (83 loc) · 3.71 KB

CursorPaginator

Build

A ruby cursor pagination library inspired by https://jsonapi.org/profiles/ethanresnick/cursor-pagination

Installation

Add this line to your application's Gemfile:

gem 'cursor_paginator'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install cursor_paginator

Usage

Paginating in descending order (default)

By default CursorPaginator orders in descending order.

Post.create!(title: "old", id: 1)
Post.create!(title: "newer", id: 2)
Post.create!(title: "newest", id: 3)

pagination_result = CursorPaginator.paginate(Post.all)

pagination_result.map(&:title)       # => ["newest", "newer", "old"]
pagination_result.prev_cursor_params # => { before: 3 } (newest)
pagination_result.next_cursor_params # => { after: nil }

In order fetch older records use the after option.

Post.create!(title: "oldest", id: 1)
Post.create!(title: "old", id: 2)
Post.create!(title: "newer", id: 3)
Post.create!(title: "newest", id: 4)

pagination_result = CursorPaginator.paginate(Post.all, page_options: { after: 3 }) # after (newer)

pagination_result.map(&:title)       # => ["old", "oldest"]
pagination_result.prev_cursor_params # => { before: 2 } (old)
pagination_result.next_cursor_params # => { after: nil }

In order to fetch newer records use the before option.

Post.create!(title: "oldest", id: 1)
Post.create!(title: "old", id: 2)
Post.create!(title: "newer", id: 3)
Post.create!(title: "newest", id: 4)

pagination_result = CursorPaginator.paginate(Post.all, page_options: { before: 2 }) # after (old)

pagination_result.map(&:title)       # => ["newest", "newer"]
pagination_result.prev_cursor_params # => { before: nil }
pagination_result.next_cursor_params # => { after: 3 } (newer)

Paginating in ascending order

In order fetch newer records use the after option.

Post.create!(title: "oldest", id: 1)
Post.create!(title: "old", id: 2)
Post.create!(title: "newer", id: 3)
Post.create!(title: "newest", id: 4)

pagination_result = CursorPaginator.paginate(
  Post.all,
  page_options: { after: 2 }, # after (old)
  paginator_options: { sort_direction: :asc }
)

pagination_result.map(&:title)       # => ["newer", "newest"]
pagination_result.prev_cursor_params # => { before: 3 } (newer)
pagination_result.next_cursor_params # => { after: nil }

Configuration (default)

CursorPaginator.paginator = CursorPaginator::Paginator::ActiveRecord

pagination_result = CursorPaginator.paginate(
  Post.all,
  page_options: { size: 10 },
  paginator_options: {
    order_key: :id,
    primary_key: :id,
    sort_direction: :desc
  }
)

Development

After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/somleng/cursor_paginator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

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

Code of Conduct

Everyone interacting in the CursorPaginator project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.