An open API service indexing awesome lists of open source software.

https://github.com/tsuwatch/activerecord-cursor

cursor pagination for ActiveRecord
https://github.com/tsuwatch/activerecord-cursor

Last synced: 2 months ago
JSON representation

cursor pagination for ActiveRecord

Awesome Lists containing this project

README

        

# ActiveRecord::Cursor

cursor pagination for ActiveRecord

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'activerecord-cursor'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install activerecord-cursor

## Usage

```ruby
Post.all
=> [
#,
#,
#,
#
]
Post.cursor(key: :created_at)
=> [#]
Post.where(published: true).cursor(key: :created_at, reverse: true)
=> [#]
Post.where(published: true).cursor(key: :created_at, reverse: true, start: Post.next_cursor) # Get next page
=> [#]
Post.where(published: true).cursor(key: :created_at, reverse: true, stop: Post.prev_cursor) # Get previous page
=> [#]
```

### Options

- `key`: Cursor key. Defult: 'id'
- `reverse`: Set it to true, if your set are ordered descendingly (DESC). Default: false
- `size`: Page size. Default: 1
- `start`: Cursor value
- `stop`: Cursor value

## 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.

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](https://rubygems.org).

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/tsuwatch/activerecord-cursor.