Coder Social home page Coder Social logo

cursor's Introduction

Overview

Cursor is a fork of Kaminari that has been gutted to perform cursor pagination only for Rails and ActiveRecord

It’s currently a very simple implementation that pages by the id of the primary table.

Usage

Suppose we have users with ids 1..100

> User.page(before: 50).per(5)
=> [#<User id: 49>, #<User id: 48>, #<User id: 47>, #<User id: 46>, #<User id: 45>]  
> User.page(before: 50).per(5).next_cursor
=> 45
> User.page(before: 50).per(5).prev_cursor
=> 49

The before id is NOT included in the results. The next and previous cursors are the min and max ids of the returned set, respectively.

> User.page(after: 44).per(5)
=> [#<User id: 45>, #<User id: 46>, #<User id: 47>, #<User id: 48>, #<User id: 49>]  
> User.page(before: 50).per(5).next_cursor
=> 49
> User.page(before: 50).per(5).prev_cursor
=> 45

Note that the results are reversed from the before. Before implies order desc. After implies order asc. The cursors are similarly reversed. In both before and after, the specified cursor in the request is excluded from the result.

> User.page
=> [#<User id: 100>, #<User id: 99>, ... #<User id: 77>, #<User id: 76>]  

The default direction is before and the default per_page value is 25.

Configuration Options

You can configure the following default values by overriding these values using Cursor.configure method.

default_per_page  # 25 by default
max_per_page      # nil by default
page_method_name  # :page by default
before_param_name # :before by default
after_param_name  # :after by default

There's a handy generator that generates the default configuration file into config/initializers directory. Run the following generator command, then edit the generated file.

rails g cursor:config

Contributing

To run the test suite locally against all supported frameworks:

bundle install
rake spec:all

To target the test suite against one framework:

rake spec:active_record_40

You can find a list of supported spec tasks by running rake -T. You may also find it useful to run a specific test for a specific framework. To do so, you'll have to first make sure you have bundled everything for that configuration, then you can run the specific test:

BUNDLE_GEMFILE='gemfiles/active_record_40.gemfile' bundle install
BUNDLE_GEMFILE='gemfiles/active_record_40.gemfile' bundle exec rspec

cursor's People

Contributors

amatsuda avatar yuki24 avatar hsbt avatar wbyoung avatar udzura avatar l15n avatar eitoball avatar juno avatar bricker avatar apotonick avatar dblock avatar heel avatar plribeiro3000 avatar mrstif avatar ajrkerr avatar denysonique avatar ragmaanir avatar danhunter avatar daliborfilus avatar hasimo avatar zpieslak avatar tmilewski avatar timgremore avatar rrrene avatar petergoldstein avatar keiko713 avatar seejohnrun avatar joealba avatar hamin avatar vladimir-vg avatar

Watchers

Brian Thomas avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.