cursor_pager 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ci.yml +56 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.rubocop.yml +22 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/Appraisals +9 -0
- data/CHANGELOG.md +8 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +85 -0
- data/LICENSE.txt +21 -0
- data/README.md +107 -0
- data/Rakefile +22 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/cursor_pager.gemspec +29 -0
- data/gemfiles/.bundle/config +2 -0
- data/gemfiles/activerecord_5.2.gemfile +13 -0
- data/gemfiles/activerecord_5.2.gemfile.lock +86 -0
- data/gemfiles/activerecord_6.0.gemfile +13 -0
- data/gemfiles/activerecord_6.0.gemfile.lock +86 -0
- data/lib/cursor_pager.rb +21 -0
- data/lib/cursor_pager/base64_encoder.rb +20 -0
- data/lib/cursor_pager/configuration.rb +30 -0
- data/lib/cursor_pager/conflicting_orders_error.rb +16 -0
- data/lib/cursor_pager/cursor_not_found_error.rb +12 -0
- data/lib/cursor_pager/invalid_cursor_error.rb +12 -0
- data/lib/cursor_pager/limit_relation.rb +65 -0
- data/lib/cursor_pager/order_value.rb +63 -0
- data/lib/cursor_pager/order_value_error.rb +6 -0
- data/lib/cursor_pager/order_values.rb +41 -0
- data/lib/cursor_pager/page.rb +110 -0
- data/lib/cursor_pager/slice_relation.rb +52 -0
- data/lib/cursor_pager/version.rb +5 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1cfa8e486f765f437f2737c1778ea9f70c22849d8e0a6912bee1ec20440487a2
|
4
|
+
data.tar.gz: 99bbe1f36e20d8574217ca27f6bec0ef431487679f048ce7e6fb55fb8c73de5e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fe4e2e39e9d28cb318b6da7d09368f2d866a2f5386e796f8f816af22fdada3539748c46e59259c51673eee30ddb5d6059d0e02a8f6800da936be6fe1db7af6ae
|
7
|
+
data.tar.gz: 0e861720ee32d06b9d55c58a0f0a4ff6ba316435ae619186b8f762c42a6df40db87c79e7d968517d899fe3be3777dc146641dfa79dcada4f66883bc6e6686c48
|
@@ -0,0 +1,56 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [master]
|
6
|
+
pull_request:
|
7
|
+
branches: [master]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
strategy:
|
13
|
+
matrix:
|
14
|
+
ruby-version: ["2.6", "2.7"]
|
15
|
+
activerecord-version: ["5.2", "6.0"]
|
16
|
+
|
17
|
+
services:
|
18
|
+
db:
|
19
|
+
image: postgres:12
|
20
|
+
env:
|
21
|
+
POSTGRES_PASSWORD: password
|
22
|
+
POSTGRES_DB: test
|
23
|
+
ports: ['5432:5432']
|
24
|
+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
|
25
|
+
|
26
|
+
steps:
|
27
|
+
- name: Checkout
|
28
|
+
uses: actions/checkout@v2
|
29
|
+
|
30
|
+
- name: Set up bundler cache
|
31
|
+
uses: actions/cache@v1
|
32
|
+
with:
|
33
|
+
path: vendor/bundle
|
34
|
+
key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
|
35
|
+
restore-keys: |
|
36
|
+
${{ runner.os }}-gems-
|
37
|
+
|
38
|
+
- name: Set up Ruby ${{ matrix.ruby-version }}
|
39
|
+
uses: ruby/setup-ruby@v1
|
40
|
+
with:
|
41
|
+
ruby-version: ${{ matrix.ruby-version }}
|
42
|
+
|
43
|
+
- name: Install dependencies (bundler)
|
44
|
+
run: |
|
45
|
+
export BUNDLE_GEMFILE="${GITHUB_WORKSPACE}/gemfiles/activerecord_${{ matrix.activerecord-version }}.gemfile"
|
46
|
+
gem install bundler
|
47
|
+
bundle config path "vendor/bundle"
|
48
|
+
bundle config jobs "$(nproc)"
|
49
|
+
bundle install
|
50
|
+
|
51
|
+
- name: Run tests
|
52
|
+
env:
|
53
|
+
DATABASE_URL: postgres://postgres:password@localhost:5432/test
|
54
|
+
run: |
|
55
|
+
export BUNDLE_GEMFILE="${GITHUB_WORKSPACE}/gemfiles/activerecord_${{ matrix.activerecord-version }}.gemfile"
|
56
|
+
bundle exec rspec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
AllCops:
|
2
|
+
NewCops: enable
|
3
|
+
Exclude:
|
4
|
+
- /**/bin/*
|
5
|
+
- /**/gemfiles/**
|
6
|
+
|
7
|
+
Layout/ArgumentAlignment:
|
8
|
+
EnforcedStyle: with_fixed_indentation
|
9
|
+
|
10
|
+
Layout/MultilineMethodCallIndentation:
|
11
|
+
EnforcedStyle: indented
|
12
|
+
|
13
|
+
Metrics:
|
14
|
+
Exclude:
|
15
|
+
- /**/spec/**/*
|
16
|
+
|
17
|
+
Metrics/BlockLength:
|
18
|
+
Exclude:
|
19
|
+
- /**/spec/**/*
|
20
|
+
|
21
|
+
Style/StringLiterals:
|
22
|
+
EnforcedStyle: double_quotes
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.1
|
data/.travis.yml
ADDED
data/Appraisals
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## Unreleased
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at bastian.bartmann@check24.de. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in cursor_pager.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "appraisal"
|
9
|
+
gem "database_cleaner-active_record"
|
10
|
+
gem "pg"
|
11
|
+
gem "rake", "~> 12.0"
|
12
|
+
gem "rspec", "~> 3.0"
|
13
|
+
gem "rubocop"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
cursor_pager (0.1.0)
|
5
|
+
activerecord (>= 5.2.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (6.0.2.2)
|
11
|
+
activesupport (= 6.0.2.2)
|
12
|
+
activerecord (6.0.2.2)
|
13
|
+
activemodel (= 6.0.2.2)
|
14
|
+
activesupport (= 6.0.2.2)
|
15
|
+
activesupport (6.0.2.2)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 0.7, < 2)
|
18
|
+
minitest (~> 5.1)
|
19
|
+
tzinfo (~> 1.1)
|
20
|
+
zeitwerk (~> 2.2)
|
21
|
+
appraisal (2.2.0)
|
22
|
+
bundler
|
23
|
+
rake
|
24
|
+
thor (>= 0.14.0)
|
25
|
+
ast (2.4.0)
|
26
|
+
concurrent-ruby (1.1.6)
|
27
|
+
database_cleaner (1.8.4)
|
28
|
+
database_cleaner-active_record (1.8.0)
|
29
|
+
activerecord
|
30
|
+
database_cleaner (~> 1.8.0)
|
31
|
+
diff-lcs (1.3)
|
32
|
+
i18n (1.8.2)
|
33
|
+
concurrent-ruby (~> 1.0)
|
34
|
+
jaro_winkler (1.5.4)
|
35
|
+
minitest (5.14.0)
|
36
|
+
parallel (1.19.1)
|
37
|
+
parser (2.7.1.1)
|
38
|
+
ast (~> 2.4.0)
|
39
|
+
pg (1.2.3)
|
40
|
+
rainbow (3.0.0)
|
41
|
+
rake (12.3.3)
|
42
|
+
rexml (3.2.4)
|
43
|
+
rspec (3.9.0)
|
44
|
+
rspec-core (~> 3.9.0)
|
45
|
+
rspec-expectations (~> 3.9.0)
|
46
|
+
rspec-mocks (~> 3.9.0)
|
47
|
+
rspec-core (3.9.1)
|
48
|
+
rspec-support (~> 3.9.1)
|
49
|
+
rspec-expectations (3.9.1)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.9.0)
|
52
|
+
rspec-mocks (3.9.1)
|
53
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
54
|
+
rspec-support (~> 3.9.0)
|
55
|
+
rspec-support (3.9.2)
|
56
|
+
rubocop (0.82.0)
|
57
|
+
jaro_winkler (~> 1.5.1)
|
58
|
+
parallel (~> 1.10)
|
59
|
+
parser (>= 2.7.0.1)
|
60
|
+
rainbow (>= 2.2.2, < 4.0)
|
61
|
+
rexml
|
62
|
+
ruby-progressbar (~> 1.7)
|
63
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
64
|
+
ruby-progressbar (1.10.1)
|
65
|
+
thor (1.0.1)
|
66
|
+
thread_safe (0.3.6)
|
67
|
+
tzinfo (1.2.7)
|
68
|
+
thread_safe (~> 0.1)
|
69
|
+
unicode-display_width (1.7.0)
|
70
|
+
zeitwerk (2.3.0)
|
71
|
+
|
72
|
+
PLATFORMS
|
73
|
+
ruby
|
74
|
+
|
75
|
+
DEPENDENCIES
|
76
|
+
appraisal
|
77
|
+
cursor_pager!
|
78
|
+
database_cleaner-active_record
|
79
|
+
pg
|
80
|
+
rake (~> 12.0)
|
81
|
+
rspec (~> 3.0)
|
82
|
+
rubocop
|
83
|
+
|
84
|
+
BUNDLED WITH
|
85
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 CHECK24 Vergleichsportal Profis GmbH
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# CursorPager
|
2
|
+
|
3
|
+
![build](https://github.com/askcharlie/cursor_pager/workflows/CI/badge.svg)
|
4
|
+
|
5
|
+
A small and easy-to-use library that aims to make it easy to build Rails APIs
|
6
|
+
with cursor-based pagination (aka keyset pagination).
|
7
|
+
|
8
|
+
It aims to support the [JSON API Cursor Pagination] and [Relay's GraphQL Cursor
|
9
|
+
Connection] specs.
|
10
|
+
|
11
|
+
Stable ordering is essential for cursor-based pagination. The gem will
|
12
|
+
automatically add additional sorting by primary key if the relation passed
|
13
|
+
into the page isn't already ordered by the primary key or a datetime column.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
CursorPager is compatible with ActiveRecord 6.0 and 5.2 on Ruby 2.6 and later.
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem "cursor_pager"
|
23
|
+
```
|
24
|
+
|
25
|
+
And then execute:
|
26
|
+
|
27
|
+
$ bundle install
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
$ gem install cursor_pager
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
To fetch the first ten users:
|
36
|
+
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
page = CursorPager::Page.new(User.all, first: 10)
|
40
|
+
page.records
|
41
|
+
```
|
42
|
+
|
43
|
+
To get information about the page or generate a cursor
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
page.previous_page? # Tells you if there is a previous page
|
47
|
+
page.next_page? # Tells you if there is a next page
|
48
|
+
page.cursor_for(page.records.last) # Will generate a cursor for a specific item
|
49
|
+
```
|
50
|
+
|
51
|
+
To fetch the first then usres after a certain cursor
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
page = CursorPager::Page.new(User.all, first: 10, after: "MTA=")
|
55
|
+
page.records
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
To paginate backwards
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
page = CursorPager::Page.new(User.all, last: 10, before: "NTA=")
|
63
|
+
page.records
|
64
|
+
```
|
65
|
+
|
66
|
+
## Configuration
|
67
|
+
|
68
|
+
By default cursors are base64 encoded primary keys of the records. If you wish
|
69
|
+
to change that because you want to add encryption or something similiar, you
|
70
|
+
can provide your own encoder class.
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
class CustomEncoder
|
74
|
+
def encode(data)
|
75
|
+
# Your encoding logic
|
76
|
+
end
|
77
|
+
|
78
|
+
def decode(data)
|
79
|
+
# Your decoding logic
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
CursorPager.configure do |config|
|
84
|
+
config.encoder = CustomEncoder
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
## Not (yet) supported
|
89
|
+
|
90
|
+
* Ordering by SQL alias or function
|
91
|
+
* Ordering by multiple columns in different directions
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
Bug reports and pull requests are welcome on GitHub. This project is intended
|
96
|
+
to be a safe, welcoming space for collaboration, and contributors are expected
|
97
|
+
to adhere to the [code of conduct].
|
98
|
+
|
99
|
+
|
100
|
+
## License
|
101
|
+
|
102
|
+
The gem is available as open source under the terms of the [MIT License].
|
103
|
+
|
104
|
+
[JSON API Cursor Pagination]: https://jsonapi.org/profiles/ethanresnick/cursor-pagination/
|
105
|
+
[Relay's GraphQL Cursor Connection]: https://relay.dev/graphql/connections.htm
|
106
|
+
[code of conduct]: https://github.com/askcharlie/cursor_pager/blob/master/CODE_OF_CONDUCT.md
|
107
|
+
[MIT License]: https://opensource.org/licenses/MIT
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
require "rubocop/rake_task"
|
6
|
+
|
7
|
+
RuboCop::RakeTask.new
|
8
|
+
RSpec::Core::RakeTask.new(:spec)
|
9
|
+
|
10
|
+
task default: %i[rubocop spec]
|
11
|
+
|
12
|
+
namespace :db do
|
13
|
+
desc "Create the test DB"
|
14
|
+
task :create do
|
15
|
+
`createdb cursor_pager_test`
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Drop the test DB"
|
19
|
+
task :drop do
|
20
|
+
`dropdb cursor_pager_test`
|
21
|
+
end
|
22
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'cursor_pager'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/cursor_pager/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "cursor_pager"
|
7
|
+
spec.version = CursorPager::VERSION
|
8
|
+
spec.authors = ["Bastian Bartmann"]
|
9
|
+
spec.email = ["bastian.bartmann@check24.de"]
|
10
|
+
|
11
|
+
spec.summary = "Cursor-based pagination for ActiveRecord relations."
|
12
|
+
spec.homepage = "https://github.com/askcharlie/cursor_pager"
|
13
|
+
spec.license = "MIT"
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
spec.metadata["changelog_uri"] =
|
19
|
+
"https://github.com/askcharlie/cursor_pager/blob/master/CHANGELOG.md"
|
20
|
+
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
22
|
+
`git ls-files -z`
|
23
|
+
.split("\x0")
|
24
|
+
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.require_paths = ["lib"]
|
27
|
+
|
28
|
+
spec.add_dependency("activerecord", ">= 5.2.0")
|
29
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "appraisal"
|
6
|
+
gem "database_cleaner-active_record"
|
7
|
+
gem "pg"
|
8
|
+
gem "rake", "~> 12.0"
|
9
|
+
gem "rspec", "~> 3.0"
|
10
|
+
gem "rubocop"
|
11
|
+
gem "activerecord", "~> 5.2.0"
|
12
|
+
|
13
|
+
gemspec path: "../"
|
@@ -0,0 +1,86 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ..
|
3
|
+
specs:
|
4
|
+
cursor_pager (0.1.0)
|
5
|
+
activerecord (>= 5.2.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (5.2.4.2)
|
11
|
+
activesupport (= 5.2.4.2)
|
12
|
+
activerecord (5.2.4.2)
|
13
|
+
activemodel (= 5.2.4.2)
|
14
|
+
activesupport (= 5.2.4.2)
|
15
|
+
arel (>= 9.0)
|
16
|
+
activesupport (5.2.4.2)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 0.7, < 2)
|
19
|
+
minitest (~> 5.1)
|
20
|
+
tzinfo (~> 1.1)
|
21
|
+
appraisal (2.2.0)
|
22
|
+
bundler
|
23
|
+
rake
|
24
|
+
thor (>= 0.14.0)
|
25
|
+
arel (9.0.0)
|
26
|
+
ast (2.4.0)
|
27
|
+
concurrent-ruby (1.1.6)
|
28
|
+
database_cleaner (1.8.4)
|
29
|
+
database_cleaner-active_record (1.8.0)
|
30
|
+
activerecord
|
31
|
+
database_cleaner (~> 1.8.0)
|
32
|
+
diff-lcs (1.3)
|
33
|
+
i18n (1.8.2)
|
34
|
+
concurrent-ruby (~> 1.0)
|
35
|
+
jaro_winkler (1.5.4)
|
36
|
+
minitest (5.14.0)
|
37
|
+
parallel (1.19.1)
|
38
|
+
parser (2.7.1.1)
|
39
|
+
ast (~> 2.4.0)
|
40
|
+
pg (1.2.3)
|
41
|
+
rainbow (3.0.0)
|
42
|
+
rake (12.3.3)
|
43
|
+
rexml (3.2.4)
|
44
|
+
rspec (3.9.0)
|
45
|
+
rspec-core (~> 3.9.0)
|
46
|
+
rspec-expectations (~> 3.9.0)
|
47
|
+
rspec-mocks (~> 3.9.0)
|
48
|
+
rspec-core (3.9.1)
|
49
|
+
rspec-support (~> 3.9.1)
|
50
|
+
rspec-expectations (3.9.1)
|
51
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
52
|
+
rspec-support (~> 3.9.0)
|
53
|
+
rspec-mocks (3.9.1)
|
54
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
55
|
+
rspec-support (~> 3.9.0)
|
56
|
+
rspec-support (3.9.2)
|
57
|
+
rubocop (0.81.0)
|
58
|
+
jaro_winkler (~> 1.5.1)
|
59
|
+
parallel (~> 1.10)
|
60
|
+
parser (>= 2.7.0.1)
|
61
|
+
rainbow (>= 2.2.2, < 4.0)
|
62
|
+
rexml
|
63
|
+
ruby-progressbar (~> 1.7)
|
64
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
65
|
+
ruby-progressbar (1.10.1)
|
66
|
+
thor (1.0.1)
|
67
|
+
thread_safe (0.3.6)
|
68
|
+
tzinfo (1.2.7)
|
69
|
+
thread_safe (~> 0.1)
|
70
|
+
unicode-display_width (1.7.0)
|
71
|
+
|
72
|
+
PLATFORMS
|
73
|
+
ruby
|
74
|
+
|
75
|
+
DEPENDENCIES
|
76
|
+
activerecord (~> 5.2.0)
|
77
|
+
appraisal
|
78
|
+
cursor_pager!
|
79
|
+
database_cleaner-active_record
|
80
|
+
pg
|
81
|
+
rake (~> 12.0)
|
82
|
+
rspec (~> 3.0)
|
83
|
+
rubocop
|
84
|
+
|
85
|
+
BUNDLED WITH
|
86
|
+
2.1.4
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# This file was generated by Appraisal
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "appraisal"
|
6
|
+
gem "database_cleaner-active_record"
|
7
|
+
gem "pg"
|
8
|
+
gem "rake", "~> 12.0"
|
9
|
+
gem "rspec", "~> 3.0"
|
10
|
+
gem "rubocop"
|
11
|
+
gem "activerecord", "~> 6.0.0"
|
12
|
+
|
13
|
+
gemspec path: "../"
|
@@ -0,0 +1,86 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ..
|
3
|
+
specs:
|
4
|
+
cursor_pager (0.1.0)
|
5
|
+
activerecord (>= 5.2.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (6.0.2.2)
|
11
|
+
activesupport (= 6.0.2.2)
|
12
|
+
activerecord (6.0.2.2)
|
13
|
+
activemodel (= 6.0.2.2)
|
14
|
+
activesupport (= 6.0.2.2)
|
15
|
+
activesupport (6.0.2.2)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 0.7, < 2)
|
18
|
+
minitest (~> 5.1)
|
19
|
+
tzinfo (~> 1.1)
|
20
|
+
zeitwerk (~> 2.2)
|
21
|
+
appraisal (2.2.0)
|
22
|
+
bundler
|
23
|
+
rake
|
24
|
+
thor (>= 0.14.0)
|
25
|
+
ast (2.4.0)
|
26
|
+
concurrent-ruby (1.1.6)
|
27
|
+
database_cleaner (1.8.4)
|
28
|
+
database_cleaner-active_record (1.8.0)
|
29
|
+
activerecord
|
30
|
+
database_cleaner (~> 1.8.0)
|
31
|
+
diff-lcs (1.3)
|
32
|
+
i18n (1.8.2)
|
33
|
+
concurrent-ruby (~> 1.0)
|
34
|
+
jaro_winkler (1.5.4)
|
35
|
+
minitest (5.14.0)
|
36
|
+
parallel (1.19.1)
|
37
|
+
parser (2.7.1.1)
|
38
|
+
ast (~> 2.4.0)
|
39
|
+
pg (1.2.3)
|
40
|
+
rainbow (3.0.0)
|
41
|
+
rake (12.3.3)
|
42
|
+
rexml (3.2.4)
|
43
|
+
rspec (3.9.0)
|
44
|
+
rspec-core (~> 3.9.0)
|
45
|
+
rspec-expectations (~> 3.9.0)
|
46
|
+
rspec-mocks (~> 3.9.0)
|
47
|
+
rspec-core (3.9.1)
|
48
|
+
rspec-support (~> 3.9.1)
|
49
|
+
rspec-expectations (3.9.1)
|
50
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
51
|
+
rspec-support (~> 3.9.0)
|
52
|
+
rspec-mocks (3.9.1)
|
53
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
54
|
+
rspec-support (~> 3.9.0)
|
55
|
+
rspec-support (3.9.2)
|
56
|
+
rubocop (0.81.0)
|
57
|
+
jaro_winkler (~> 1.5.1)
|
58
|
+
parallel (~> 1.10)
|
59
|
+
parser (>= 2.7.0.1)
|
60
|
+
rainbow (>= 2.2.2, < 4.0)
|
61
|
+
rexml
|
62
|
+
ruby-progressbar (~> 1.7)
|
63
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
64
|
+
ruby-progressbar (1.10.1)
|
65
|
+
thor (1.0.1)
|
66
|
+
thread_safe (0.3.6)
|
67
|
+
tzinfo (1.2.7)
|
68
|
+
thread_safe (~> 0.1)
|
69
|
+
unicode-display_width (1.7.0)
|
70
|
+
zeitwerk (2.3.0)
|
71
|
+
|
72
|
+
PLATFORMS
|
73
|
+
ruby
|
74
|
+
|
75
|
+
DEPENDENCIES
|
76
|
+
activerecord (~> 6.0.0)
|
77
|
+
appraisal
|
78
|
+
cursor_pager!
|
79
|
+
database_cleaner-active_record
|
80
|
+
pg
|
81
|
+
rake (~> 12.0)
|
82
|
+
rspec (~> 3.0)
|
83
|
+
rubocop
|
84
|
+
|
85
|
+
BUNDLED WITH
|
86
|
+
2.1.4
|
data/lib/cursor_pager.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "cursor_pager/version"
|
4
|
+
|
5
|
+
module CursorPager
|
6
|
+
class Error < StandardError
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
require "cursor_pager/conflicting_orders_error"
|
11
|
+
require "cursor_pager/cursor_not_found_error"
|
12
|
+
require "cursor_pager/invalid_cursor_error"
|
13
|
+
require "cursor_pager/order_value_error"
|
14
|
+
|
15
|
+
require "cursor_pager/configuration"
|
16
|
+
require "cursor_pager/base64_encoder"
|
17
|
+
require "cursor_pager/order_value"
|
18
|
+
require "cursor_pager/order_values"
|
19
|
+
require "cursor_pager/limit_relation"
|
20
|
+
require "cursor_pager/slice_relation"
|
21
|
+
require "cursor_pager/page"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "base64"
|
4
|
+
|
5
|
+
module CursorPager
|
6
|
+
# Default encoder used to encode & decode cursors.
|
7
|
+
class Base64Encoder
|
8
|
+
class << self
|
9
|
+
def encode(data)
|
10
|
+
Base64.urlsafe_encode64(data)
|
11
|
+
end
|
12
|
+
|
13
|
+
def decode(data)
|
14
|
+
Base64.urlsafe_decode64(data)
|
15
|
+
rescue ArgumentError
|
16
|
+
raise InvalidCursorError, data
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Top level module on which the configuration will be applied.
|
4
|
+
module CursorPager
|
5
|
+
# Encapulates all the configuration for the library.
|
6
|
+
class Configuration
|
7
|
+
# The encoder that will be used to encode & decode cursors.
|
8
|
+
attr_accessor :encoder
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@encoder = Base64Encoder
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configuration
|
16
|
+
@configuration ||= Configuration.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configuration=(config)
|
20
|
+
@configuration = config
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.reset
|
24
|
+
@configuration = Configuration.new
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.configure
|
28
|
+
yield(configuration)
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CursorPager
|
4
|
+
# Will be raised when the relation is ordered by multiple attributes but in
|
5
|
+
# different directions.
|
6
|
+
class ConflictingOrdersError < Error
|
7
|
+
MESSAGE = <<~MESSAGE
|
8
|
+
Ordering by multiple attributes requires they are all ordered in the
|
9
|
+
same direction.
|
10
|
+
MESSAGE
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
super(MESSAGE)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CursorPager
|
4
|
+
# Will be raised when the cursor's record couldn't be found in the relation.
|
5
|
+
class CursorNotFoundError < Error
|
6
|
+
def initialize(cursor)
|
7
|
+
message = "Couldn't find item for cursor: #{cursor}."
|
8
|
+
|
9
|
+
super(message)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CursorPager
|
4
|
+
# Applies first and last limits to a relation.
|
5
|
+
class LimitRelation
|
6
|
+
attr_reader :base_relation, :first, :last
|
7
|
+
|
8
|
+
def initialize(base_relation, first, last)
|
9
|
+
@base_relation = base_relation
|
10
|
+
@first = first
|
11
|
+
@last = last
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
relation = base_relation
|
16
|
+
|
17
|
+
relation = apply_first(relation) if first.present?
|
18
|
+
relation = apply_last(relation) if last.present?
|
19
|
+
|
20
|
+
relation
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# Apply first if it sets a stricter limit than the one already applied
|
26
|
+
def apply_first(relation)
|
27
|
+
previous_limit = relation.limit_value
|
28
|
+
|
29
|
+
if previous_limit.nil? || previous_limit > first
|
30
|
+
relation.limit(first)
|
31
|
+
else
|
32
|
+
relation
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Apply last if it's a smaller slice than the previous limit
|
37
|
+
def apply_last(relation)
|
38
|
+
previous_limit = relation.limit_value
|
39
|
+
|
40
|
+
if previous_limit.present?
|
41
|
+
if last <= previous_limit
|
42
|
+
relation = apply_stricter_last(relation, previous_limit)
|
43
|
+
end
|
44
|
+
else
|
45
|
+
relation = apply_last_withouth_previous_limit(relation)
|
46
|
+
end
|
47
|
+
|
48
|
+
relation
|
49
|
+
end
|
50
|
+
|
51
|
+
def apply_stricter_last(relation, previous_limit)
|
52
|
+
offset = (relation.offset_value || 0) + (previous_limit - last)
|
53
|
+
|
54
|
+
relation.offset(offset).limit(last)
|
55
|
+
end
|
56
|
+
|
57
|
+
def apply_last_withouth_previous_limit(relation)
|
58
|
+
count = base_relation.size
|
59
|
+
previous_offset = relation.offset_value || 0
|
60
|
+
offset = previous_offset + count - [last, count].min
|
61
|
+
|
62
|
+
relation.offset(offset).limit(last)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CursorPager
|
4
|
+
# Wraps ActiveRecord's order values.
|
5
|
+
# Depending on how the order of the relation is defined, ActiveRecord will
|
6
|
+
# give you multiple arel nodes or just one string. This deals with the
|
7
|
+
# differences so we don't have to do it anywhere else.
|
8
|
+
class OrderValue
|
9
|
+
PARENTHESIS_REGEX = /[\(\)]/.freeze
|
10
|
+
|
11
|
+
attr_reader :attribute, :direction
|
12
|
+
|
13
|
+
def self.from_arel_node(relation, node)
|
14
|
+
new(relation, node.value.name, node.direction)
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.from_order_string(relation, value)
|
18
|
+
if value.match?(PARENTHESIS_REGEX)
|
19
|
+
raise OrderValueError, "Order values can't include functions."
|
20
|
+
end
|
21
|
+
|
22
|
+
value.split(",").map do |split_value|
|
23
|
+
new(relation, *split_value.squish.split)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(relation, attribute, direction = :asc)
|
28
|
+
@relation = relation
|
29
|
+
@attribute = attribute
|
30
|
+
@direction = direction.downcase.to_sym
|
31
|
+
end
|
32
|
+
|
33
|
+
def primary_key?
|
34
|
+
relation.primary_key == attribute
|
35
|
+
end
|
36
|
+
|
37
|
+
def type
|
38
|
+
relation.type_for_attribute(attribute).type
|
39
|
+
end
|
40
|
+
|
41
|
+
def prefixed_attribute
|
42
|
+
return attribute if attribute.include?(".")
|
43
|
+
|
44
|
+
"#{relation.table_name}.#{attribute}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def select_alias
|
48
|
+
prefixed_attribute.tr(".", "_")
|
49
|
+
end
|
50
|
+
|
51
|
+
def select_string
|
52
|
+
"#{prefixed_attribute} AS #{select_alias}"
|
53
|
+
end
|
54
|
+
|
55
|
+
def order_string
|
56
|
+
"#{prefixed_attribute} #{direction}"
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
attr_reader :relation
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "forwardable"
|
4
|
+
|
5
|
+
module CursorPager
|
6
|
+
# OrderValue collection wrapper.
|
7
|
+
class OrderValues
|
8
|
+
extend Forwardable
|
9
|
+
include Enumerable
|
10
|
+
|
11
|
+
def_delegators :@collection, :each, :<<, :size, :present?
|
12
|
+
|
13
|
+
# A relation's order_values can either be an empty array, an array including
|
14
|
+
# just one string, or an array of arel ordering nodes.
|
15
|
+
def self.from_relation(relation)
|
16
|
+
arel_order_values = relation.order_values.uniq.reject(&:blank?)
|
17
|
+
collection = arel_order_values.flat_map do |value|
|
18
|
+
case value
|
19
|
+
when Arel::Nodes::Ordering
|
20
|
+
OrderValue.from_arel_node(relation, value)
|
21
|
+
when String
|
22
|
+
OrderValue.from_order_string(relation, value)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
new(collection)
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize(collection = [])
|
30
|
+
@collection = collection
|
31
|
+
end
|
32
|
+
|
33
|
+
def direction
|
34
|
+
@collection.first&.direction
|
35
|
+
end
|
36
|
+
|
37
|
+
def order_string
|
38
|
+
@collection.map(&:order_string).join(", ")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CursorPager
|
4
|
+
# The main class that coordinates the whole pagination.
|
5
|
+
class Page
|
6
|
+
attr_reader :relation, :first, :last, :after, :before, :order_values
|
7
|
+
|
8
|
+
def initialize(relation, first: nil, last: nil, after: nil, before: nil)
|
9
|
+
@relation = relation
|
10
|
+
@first = first
|
11
|
+
@last = last
|
12
|
+
@after = after
|
13
|
+
@before = before
|
14
|
+
@order_values = OrderValues.from_relation(relation)
|
15
|
+
|
16
|
+
add_default_order
|
17
|
+
verify_order_directions!
|
18
|
+
end
|
19
|
+
|
20
|
+
def previous_page?
|
21
|
+
@previous_page ||= if after_limit_value.present?
|
22
|
+
true
|
23
|
+
elsif last
|
24
|
+
limited_relation.offset_value.to_i.positive?
|
25
|
+
else
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def next_page?
|
31
|
+
@next_page ||= if before_limit_value.present?
|
32
|
+
true
|
33
|
+
elsif first
|
34
|
+
sliced_relation.limit(first + 1).count == first + 1
|
35
|
+
else
|
36
|
+
false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def cursor_for(item)
|
41
|
+
encoder.encode(item.id.to_s)
|
42
|
+
end
|
43
|
+
|
44
|
+
def records
|
45
|
+
@records ||= limited_relation.to_a
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def add_default_order
|
51
|
+
return if sufficiently_ordered?
|
52
|
+
|
53
|
+
direction = order_values.direction || :asc
|
54
|
+
|
55
|
+
@order_values << OrderValue.new(relation, relation.primary_key, direction)
|
56
|
+
end
|
57
|
+
|
58
|
+
def sufficiently_ordered?
|
59
|
+
order_values.present? && order_values.all? do |value|
|
60
|
+
value.primary_key? || value.type == :datetime
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def verify_order_directions!
|
65
|
+
return if order_values.map(&:direction).uniq.size == 1
|
66
|
+
|
67
|
+
raise ConflictingOrdersError
|
68
|
+
end
|
69
|
+
|
70
|
+
def limited_relation
|
71
|
+
@limited_relation ||= LimitRelation.new(sliced_relation, first, last).call
|
72
|
+
end
|
73
|
+
|
74
|
+
def sliced_relation
|
75
|
+
@sliced_relation ||= SliceRelation.new(
|
76
|
+
ordered_relation,
|
77
|
+
order_values,
|
78
|
+
after_limit_value,
|
79
|
+
before_limit_value
|
80
|
+
).call
|
81
|
+
end
|
82
|
+
|
83
|
+
def ordered_relation
|
84
|
+
@ordered_relation ||= relation.reorder(order_values.order_string)
|
85
|
+
end
|
86
|
+
|
87
|
+
def before_limit_value
|
88
|
+
@before_limit_value ||= before.present? && limit_value_for(before)
|
89
|
+
end
|
90
|
+
|
91
|
+
def after_limit_value
|
92
|
+
@after_limit_value ||= after.present? && limit_value_for(after)
|
93
|
+
end
|
94
|
+
|
95
|
+
def limit_value_for(cursor)
|
96
|
+
id = encoder.decode(cursor)
|
97
|
+
|
98
|
+
selects = order_values.map(&:select_string)
|
99
|
+
item = relation.where(id: id).select(selects).first
|
100
|
+
|
101
|
+
raise CursorNotFoundError, cursor if item.blank?
|
102
|
+
|
103
|
+
order_values.map { |value| item[value.select_alias] }
|
104
|
+
end
|
105
|
+
|
106
|
+
def encoder
|
107
|
+
CursorPager.configuration.encoder
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CursorPager
|
4
|
+
# Applies after and before cursors to a relation.
|
5
|
+
class SliceRelation
|
6
|
+
attr_reader :base_relation, :order_values, :after_values, :before_values
|
7
|
+
|
8
|
+
def initialize(base_relation, order_values, after_values, before_values)
|
9
|
+
@base_relation = base_relation
|
10
|
+
@order_values = order_values
|
11
|
+
@after_values = after_values
|
12
|
+
@before_values = before_values
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
relation = base_relation
|
17
|
+
|
18
|
+
relation = apply_after(relation)
|
19
|
+
relation = apply_before(relation)
|
20
|
+
|
21
|
+
relation
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def apply_after(relation)
|
27
|
+
return relation if after_values.blank?
|
28
|
+
|
29
|
+
if order_values.direction == :asc
|
30
|
+
slice_relation(relation, ">", after_values)
|
31
|
+
else
|
32
|
+
slice_relation(relation, "<", after_values)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def apply_before(relation)
|
37
|
+
return relation if before_values.blank?
|
38
|
+
|
39
|
+
if order_values.direction == :asc
|
40
|
+
slice_relation(relation, "<", before_values)
|
41
|
+
else
|
42
|
+
slice_relation(relation, ">", before_values)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def slice_relation(relation, operator, value)
|
47
|
+
slice_attribute = order_values.map(&:prefixed_attribute).join(", ")
|
48
|
+
|
49
|
+
relation.where("(#{slice_attribute}) #{operator} (?)", value)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cursor_pager
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bastian Bartmann
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.2.0
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- bastian.bartmann@check24.de
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".github/workflows/ci.yml"
|
35
|
+
- ".gitignore"
|
36
|
+
- ".rspec"
|
37
|
+
- ".rubocop.yml"
|
38
|
+
- ".ruby-version"
|
39
|
+
- ".travis.yml"
|
40
|
+
- Appraisals
|
41
|
+
- CHANGELOG.md
|
42
|
+
- CODE_OF_CONDUCT.md
|
43
|
+
- Gemfile
|
44
|
+
- Gemfile.lock
|
45
|
+
- LICENSE.txt
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- bin/console
|
49
|
+
- bin/setup
|
50
|
+
- cursor_pager.gemspec
|
51
|
+
- gemfiles/.bundle/config
|
52
|
+
- gemfiles/activerecord_5.2.gemfile
|
53
|
+
- gemfiles/activerecord_5.2.gemfile.lock
|
54
|
+
- gemfiles/activerecord_6.0.gemfile
|
55
|
+
- gemfiles/activerecord_6.0.gemfile.lock
|
56
|
+
- lib/cursor_pager.rb
|
57
|
+
- lib/cursor_pager/base64_encoder.rb
|
58
|
+
- lib/cursor_pager/configuration.rb
|
59
|
+
- lib/cursor_pager/conflicting_orders_error.rb
|
60
|
+
- lib/cursor_pager/cursor_not_found_error.rb
|
61
|
+
- lib/cursor_pager/invalid_cursor_error.rb
|
62
|
+
- lib/cursor_pager/limit_relation.rb
|
63
|
+
- lib/cursor_pager/order_value.rb
|
64
|
+
- lib/cursor_pager/order_value_error.rb
|
65
|
+
- lib/cursor_pager/order_values.rb
|
66
|
+
- lib/cursor_pager/page.rb
|
67
|
+
- lib/cursor_pager/slice_relation.rb
|
68
|
+
- lib/cursor_pager/version.rb
|
69
|
+
homepage: https://github.com/askcharlie/cursor_pager
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata:
|
73
|
+
homepage_uri: https://github.com/askcharlie/cursor_pager
|
74
|
+
source_code_uri: https://github.com/askcharlie/cursor_pager
|
75
|
+
changelog_uri: https://github.com/askcharlie/cursor_pager/blob/master/CHANGELOG.md
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options: []
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: 2.6.0
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
requirements: []
|
91
|
+
rubygems_version: 3.1.2
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Cursor-based pagination for ActiveRecord relations.
|
95
|
+
test_files: []
|