Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

studyplus/jinrai

Repository files navigation

Jinrai

Jinrai is a awesome cursor based paginator

Usage

# config/initializers/jinrai.rb

Jinrai.configure do |config|
  config.default_cursor_per = 20 #=> User.cursor.count == 20
  config.default_cursor_format = :id, :name #=> cursor format will be "#{user.id}_#{user.name}"
  config.default_cursor_sort_order = :desc
end
# app/model/user.rb

class User < ApplicationRecord
  cursor_per 100
  cursor_format :name, :age
  cursor_sort_order :asc # default: :desc
end

User.cursor.count #=> 100
User.cursor.since_format #=> generate cursor fomatted "#{user.name}_#{user.age}"
User.cursor #=> get latest 20 records.
User.cursor.count #=> 20

.cursor has two arguments, till and since, and by passing them we can get record collection of arbitrary interval.

since_cursor = User.cursor.till_cursor
User.cursor(since: since_cursor) # return records older than the record pointed by the cursor

till_cursor = User.cursor.since_cursor
User.cursor(till: till_cursor) # return records newer than the record pointed by the cursor

User.cursor(since: since_cursor, till: till_cursor) # return records newer than the record pointed by the since cursor and older than the record pointed by the till cursor.

Get cursor by calling since_cursor or till_cursor.

users = User.cursor
users.since_cursor # this cursor points first record of User collection
users.till_cursor # this cursor points last record of User collection

.cursor allows to specify order with order argument. (sort_at is an old argument, please use order.)

User.cursor(order: { age: :desc, name: :asc })

Installation

Add this line to your application's Gemfile:

gem 'jinrai'

And then execute:

$ bundle

Or install it yourself as:

$ gem install jinrai

Contributing

  1. Fork then clone this repo:
git clone [email protected]:YOUR_USERNAME/jinrai.git
  1. Create database
$ mysql --host 127.0.0.1  -uroot -e "create database jinrai_test"
  1. setup dependencies via bundler:
bundle install
  1. Make sure the spec pass:
bundle exec rspec
  1. Make your change, and write spec, make sure test pass:
bundle exec rspec
  1. write a good commit message, push to your fork, then submit PullRequest.

License

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