fast_page 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/licensing.yml +22 -0
- data/.github/workflows/main.yml +16 -0
- data/.gitignore +9 -0
- data/.rubocop.yml +22 -0
- data/.ruby-version +1 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +110 -0
- data/LICENSE.txt +21 -0
- data/README.md +105 -0
- data/Rakefile +13 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/doc/dependency_decisions.yml +37 -0
- data/fast_page.gemspec +40 -0
- data/lib/fast_page/active_record_extension.rb +17 -0
- data/lib/fast_page/active_record_methods.rb +25 -0
- data/lib/fast_page/version.rb +5 -0
- data/lib/fast_page.rb +15 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e3cd55bdec398b0a2e3f0b945dba439dcbb67af97ba90fb091f6513c4e8c4e98
|
4
|
+
data.tar.gz: 8d542c021838652fb77892d3b83c594cd50cab251a00c41acfb2297c8a72d969
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6f7aeafdb797a4e4b0fbb77f0aa9958ec710e976bd87239112c65eddbd1ff6fb7231983cc2d6c3477e68d8a15d3589bad35c87c51d92298193a0c927cc55755b
|
7
|
+
data.tar.gz: d2299e119aaf2b339faa207041cee7ccceed2425dc1c7d0c9d1e9cff62836051c1b4dfb4c6dc3bd71c47e84f87d15558a28af63ebed4bf1a7991547e5e0a4660
|
@@ -0,0 +1,22 @@
|
|
1
|
+
name: Verify dependency licenses
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- main
|
7
|
+
pull_request:
|
8
|
+
types:
|
9
|
+
- opened
|
10
|
+
- reopened
|
11
|
+
- synchronize
|
12
|
+
|
13
|
+
jobs:
|
14
|
+
licensing:
|
15
|
+
runs-on: ubuntu-latest
|
16
|
+
steps:
|
17
|
+
- uses: actions/checkout@v2
|
18
|
+
with:
|
19
|
+
fetch-depth: 0
|
20
|
+
- run: sudo gem install license_finder && sudo gem install bundler
|
21
|
+
- run: bundle install
|
22
|
+
- run: license_finder
|
@@ -0,0 +1,16 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push,pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
steps:
|
9
|
+
- uses: actions/checkout@v2
|
10
|
+
- name: Set up Ruby
|
11
|
+
uses: ruby/setup-ruby@v1
|
12
|
+
with:
|
13
|
+
ruby-version: 2.7.2
|
14
|
+
bundler-cache: true
|
15
|
+
- name: Run the default task
|
16
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.4
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
8
|
+
Style/StringLiteralsInInterpolation:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Layout/LineLength:
|
13
|
+
Max: 120
|
14
|
+
|
15
|
+
Metrics/MethodLength:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Style/Documentation:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Metrics/AbcSize:
|
22
|
+
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.1.2
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at coutermarsh.mike@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fast_page (0.1.0)
|
5
|
+
activerecord (~> 6.0.0)
|
6
|
+
activesupport (~> 6.0.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionview (6.0.5.1)
|
12
|
+
activesupport (= 6.0.5.1)
|
13
|
+
builder (~> 3.1)
|
14
|
+
erubi (~> 1.4)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
17
|
+
activemodel (6.0.5.1)
|
18
|
+
activesupport (= 6.0.5.1)
|
19
|
+
activerecord (6.0.5.1)
|
20
|
+
activemodel (= 6.0.5.1)
|
21
|
+
activesupport (= 6.0.5.1)
|
22
|
+
activesupport (6.0.5.1)
|
23
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
24
|
+
i18n (>= 0.7, < 2)
|
25
|
+
minitest (~> 5.1)
|
26
|
+
tzinfo (~> 1.1)
|
27
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
28
|
+
ast (2.4.2)
|
29
|
+
builder (3.2.4)
|
30
|
+
coderay (1.1.3)
|
31
|
+
concurrent-ruby (1.1.10)
|
32
|
+
crass (1.0.6)
|
33
|
+
erubi (1.11.0)
|
34
|
+
i18n (1.12.0)
|
35
|
+
concurrent-ruby (~> 1.0)
|
36
|
+
json (2.6.2)
|
37
|
+
kaminari (1.2.2)
|
38
|
+
activesupport (>= 4.1.0)
|
39
|
+
kaminari-actionview (= 1.2.2)
|
40
|
+
kaminari-activerecord (= 1.2.2)
|
41
|
+
kaminari-core (= 1.2.2)
|
42
|
+
kaminari-actionview (1.2.2)
|
43
|
+
actionview
|
44
|
+
kaminari-core (= 1.2.2)
|
45
|
+
kaminari-activerecord (1.2.2)
|
46
|
+
activerecord
|
47
|
+
kaminari-core (= 1.2.2)
|
48
|
+
kaminari-core (1.2.2)
|
49
|
+
loofah (2.18.0)
|
50
|
+
crass (~> 1.0.2)
|
51
|
+
nokogiri (>= 1.5.9)
|
52
|
+
method_source (1.0.0)
|
53
|
+
minitest (5.16.2)
|
54
|
+
nokogiri (1.13.8-x86_64-darwin)
|
55
|
+
racc (~> 1.4)
|
56
|
+
nokogiri (1.13.8-x86_64-linux)
|
57
|
+
racc (~> 1.4)
|
58
|
+
pagy (5.10.1)
|
59
|
+
activesupport
|
60
|
+
parallel (1.22.1)
|
61
|
+
parser (3.1.2.1)
|
62
|
+
ast (~> 2.4.1)
|
63
|
+
pry (0.13.1)
|
64
|
+
coderay (~> 1.1)
|
65
|
+
method_source (~> 1.0)
|
66
|
+
racc (1.6.0)
|
67
|
+
rails-dom-testing (2.0.3)
|
68
|
+
activesupport (>= 4.2.0)
|
69
|
+
nokogiri (>= 1.6)
|
70
|
+
rails-html-sanitizer (1.4.3)
|
71
|
+
loofah (~> 2.3)
|
72
|
+
rainbow (3.1.1)
|
73
|
+
rake (13.0.6)
|
74
|
+
regexp_parser (2.5.0)
|
75
|
+
rexml (3.2.5)
|
76
|
+
rubocop (1.33.0)
|
77
|
+
json (~> 2.3)
|
78
|
+
parallel (~> 1.10)
|
79
|
+
parser (>= 3.1.0.0)
|
80
|
+
rainbow (>= 2.2.2, < 4.0)
|
81
|
+
regexp_parser (>= 1.8, < 3.0)
|
82
|
+
rexml (>= 3.2.5, < 4.0)
|
83
|
+
rubocop-ast (>= 1.19.1, < 2.0)
|
84
|
+
ruby-progressbar (~> 1.7)
|
85
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
86
|
+
rubocop-ast (1.21.0)
|
87
|
+
parser (>= 3.1.1.0)
|
88
|
+
ruby-progressbar (1.11.0)
|
89
|
+
sqlite3 (1.4.4)
|
90
|
+
thread_safe (0.3.6)
|
91
|
+
tzinfo (1.2.10)
|
92
|
+
thread_safe (~> 0.1)
|
93
|
+
unicode-display_width (2.2.0)
|
94
|
+
zeitwerk (2.6.0)
|
95
|
+
|
96
|
+
PLATFORMS
|
97
|
+
x86_64-darwin-21
|
98
|
+
x86_64-linux
|
99
|
+
|
100
|
+
DEPENDENCIES
|
101
|
+
fast_page!
|
102
|
+
kaminari (~> 1.2)
|
103
|
+
pagy (~> 5.10)
|
104
|
+
pry
|
105
|
+
rake (~> 13.0)
|
106
|
+
rubocop (~> 1.7)
|
107
|
+
sqlite3
|
108
|
+
|
109
|
+
BUNDLED WITH
|
110
|
+
2.3.18
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Mike Coutermarsh
|
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,105 @@
|
|
1
|
+
# FastPage
|
2
|
+
`FastPage` applies the MySQL "deferred join" optimization to your ActiveRecord offset/limit queries. Potentially making your pagination much faster. ⚡️
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
|
6
|
+
Add `fast_page` to your Gemfile.
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem 'fast_page'
|
10
|
+
```
|
11
|
+
|
12
|
+
You can then use the `fast_page` method on any ActiveRecord::Relation that is using offset/limit.
|
13
|
+
|
14
|
+
### Example
|
15
|
+
Here is a slow pagination query.
|
16
|
+
```ruby
|
17
|
+
Post.all.order(created_at: :desc).limit(25).offset(100)
|
18
|
+
# Post Load (1228.7ms) SELECT `posts`.* FROM `posts` ORDER BY `posts`.`created_at` DESC LIMIT 25 OFFSET 100
|
19
|
+
```
|
20
|
+
|
21
|
+
Add `.fast_page` to your slow pagination query. It breaks it up into two, much faster queries.
|
22
|
+
```ruby
|
23
|
+
Post.all.order(created_at: :desc).limit(25).offset(100).fast_page
|
24
|
+
# Post Pluck (456.9ms) SELECT `posts`.`id` FROM `posts` ORDER BY `posts`.`created_at` DESC LIMIT 25 OFFSET 100
|
25
|
+
# Post Load (0.4ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` IN (1271528, 1271527, 1271526, 1271525, 1271524, 1271523, 1271522, 1271521, 1271520, 1271519, 1271518, 1271517, 1271516, 1271515, 1271514, 1271512, 1271513, 1271511, 1271510, 1271509, 1271508, 1271507, 1271506, 1271505, 1271504) ORDER BY `posts`.`created_at` DESC
|
26
|
+
```
|
27
|
+
|
28
|
+
## Compatible pagination libraries
|
29
|
+
`FastPage` has been tested and works with these existing popular pagination gems. If you try it with any other gems, please let us know!
|
30
|
+
|
31
|
+
### Kaminari
|
32
|
+
Add `.fast_page` to the end of your existing [Kaminari](https://github.com/kaminari/kaminari) pagination queries.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
Post.all.page(5).per(25).fast_page
|
36
|
+
```
|
37
|
+
|
38
|
+
### Pagy
|
39
|
+
In any controller that you want to use `fast_page`, add the following method. This will modify the query [Pagy](https://github.com/ddnexus/pagy) uses when retrieving the records.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
def pagy_get_items(collection, pagy)
|
43
|
+
collection.offset(pagy.offset).limit(pagy.items).fast_page
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
|
48
|
+
## How this works
|
49
|
+
|
50
|
+
The most common form of pagination is implemented using LIMIT and OFFSET.
|
51
|
+
|
52
|
+
In this example, each page returns 50 blog posts. For the first page, we grab the first 50 posts. On the 2nd page we grab 100 posts and throw away the first 50. As the `OFFSET` increases, each additional page becomes more expensive for the database to serve.
|
53
|
+
|
54
|
+
```sql
|
55
|
+
-- Page 1
|
56
|
+
SELECT * FROM posts ORDER BY created_at DESC LIMIT 50;
|
57
|
+
-- Page 2
|
58
|
+
SELECT * FROM posts ORDER BY created_at DESC LIMIT 50 OFFSET 50;
|
59
|
+
-- Page 3
|
60
|
+
SELECT * FROM posts ORDER BY created_at DESC LIMIT 50 OFFSET 100;
|
61
|
+
```
|
62
|
+
|
63
|
+
This method of pagination works well until you have a large number of records. The later pages become very expensive to serve. Because of this, applications will often have to limit the maximum number of pages they allow users to view or swap to cursor based pagination.
|
64
|
+
|
65
|
+
### Deferred join technique
|
66
|
+
|
67
|
+
[High Performance MySQL](https://learning.oreilly.com/library/view/high-performance-mysql/9781492080503/) recommends using a "deferred join" to increase the efficiency of LIMIT/OFFSET pagination for large tables.
|
68
|
+
|
69
|
+
```sql
|
70
|
+
SELECT * FROM posts
|
71
|
+
INNER JOIN(select id from posts ORDER BY created_at DESC LIMIT 50 OFFSET 10000)
|
72
|
+
AS lim USING(id);
|
73
|
+
```
|
74
|
+
|
75
|
+
Notice that we first select the ID of all the rows we want to show, then the data for those rows. This technique works "because it lets the server examine as little data as possible in an index without accessing rows."
|
76
|
+
|
77
|
+
The FastPage gem makes it easy to apply this optimization to any ActiveRecord::Relation using offset/limit.
|
78
|
+
|
79
|
+
To learn more, check out this blog post: [Efficient Pagination Using Deferred Joins](https://aaronfrancis.com/2022/efficient-pagination-using-deferred-joins)
|
80
|
+
|
81
|
+
## When should I use this?
|
82
|
+
`fast_page` works best on pagination queries that include an `ORDER BY`. It becomes more effective as the page number increases. You should test it on your application to see how it improves your query times.
|
83
|
+
|
84
|
+
Because `fast_page` runs 2 queries instead of 1. It is very likely a bit slower for early pages. The benefits begin as the user gets into deeper pages. It's worth testing to see at which page your application gets faster from using `fast_page` & only applying to your queries then.
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
posts = Post.all.page(params[:page]).per(25)
|
88
|
+
# Use fast page after page 5, improves query performance
|
89
|
+
posts = posts.fast_page if params[:page] > 5
|
90
|
+
```
|
91
|
+
|
92
|
+
## Thank you :heart:
|
93
|
+
This gem was inspired by Laravel's [`fast-paginate`](https://github.com/hammerstonedev/fast-paginate) and [@aarondfrancis](https://github.com/aarondfrancis)'s excellent blog post: [Efficient Pagination Using Deferred Joins](https://aaronfrancis.com/2022/efficient-pagination-using-deferred-joins).
|
94
|
+
|
95
|
+
## Contributing
|
96
|
+
|
97
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/planetscale/fast_page. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/planetscale/fast_page/blob/main/CODE_OF_CONDUCT.md).
|
98
|
+
|
99
|
+
## License
|
100
|
+
|
101
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
102
|
+
|
103
|
+
## Code of Conduct
|
104
|
+
|
105
|
+
Everyone interacting in the FastPage project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/planetscale/fast_page/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
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 "fast_page"
|
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,37 @@
|
|
1
|
+
---
|
2
|
+
- - :permit
|
3
|
+
- MIT
|
4
|
+
- :who:
|
5
|
+
:why:
|
6
|
+
:versions: []
|
7
|
+
:when: 2022-08-08 23:11:41.376081000 Z
|
8
|
+
- - :permit
|
9
|
+
- Apache 2.0
|
10
|
+
- :who:
|
11
|
+
:why:
|
12
|
+
:versions: []
|
13
|
+
:when: 2022-08-08 23:11:44.981485000 Z
|
14
|
+
- - :permit
|
15
|
+
- Simplified BSD
|
16
|
+
- :who:
|
17
|
+
:why:
|
18
|
+
:versions: []
|
19
|
+
:when: 2022-08-08 23:11:57.738120000 Z
|
20
|
+
- - :permit
|
21
|
+
- New BSD
|
22
|
+
- :who:
|
23
|
+
:why:
|
24
|
+
:versions: []
|
25
|
+
:when: 2022-08-08 23:12:02.144182000 Z
|
26
|
+
- - :approve
|
27
|
+
- json
|
28
|
+
- :who:
|
29
|
+
:why:
|
30
|
+
:versions: []
|
31
|
+
:when: 2022-08-08 23:13:04.483270000 Z
|
32
|
+
- - :approve
|
33
|
+
- json
|
34
|
+
- :who:
|
35
|
+
:why:
|
36
|
+
:versions: []
|
37
|
+
:when: 2022-08-08 23:13:55.801826000 Z
|
data/fast_page.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/fast_page/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "fast_page"
|
7
|
+
spec.version = FastPage::VERSION
|
8
|
+
spec.authors = ["Mike Coutermarsh"]
|
9
|
+
spec.email = ["coutermarsh.mike@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Blazing fast pagination for ActiveRecord with deferred joins "
|
12
|
+
spec.description = 'FastPage applies the MySQL "deferred join" optimization to your ActiveRecord offset/limit queries.'
|
13
|
+
spec.homepage = "https://github.com/planetscale/fast_page"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.4.0"
|
16
|
+
|
17
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/planetscale/fast_page"
|
21
|
+
spec.metadata["changelog_uri"] = "https://github.com/planetscale/fast_page"
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
# For more information and examples about making a new gem, checkout our
|
33
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
34
|
+
spec.add_dependency "activerecord", "~> 6.0.0"
|
35
|
+
spec.add_dependency "activesupport", "~> 6.0.0"
|
36
|
+
spec.add_development_dependency "kaminari", "~> 1.2"
|
37
|
+
spec.add_development_dependency "pagy", "~> 5.10"
|
38
|
+
spec.add_development_dependency "pry"
|
39
|
+
spec.add_development_dependency "sqlite3"
|
40
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "active_record_methods"
|
4
|
+
|
5
|
+
module FastPage
|
6
|
+
module ActiveRecordExtension
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
included do
|
10
|
+
def self.fast_page
|
11
|
+
extending do
|
12
|
+
include(FastPage::ActiveRecordMethods)
|
13
|
+
end.deferred_join_load
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module FastPage
|
4
|
+
module ActiveRecordMethods
|
5
|
+
def deferred_join_load
|
6
|
+
# Must have a limit or offset defined
|
7
|
+
raise ArgumentError, "You must specify a limit or offset to use fast_page" if !limit_value && !offset_value
|
8
|
+
|
9
|
+
id_scope = dup
|
10
|
+
id_scope = id_scope.except(:includes) unless references_eager_loaded_tables?
|
11
|
+
ids = id_scope.pluck(:id)
|
12
|
+
|
13
|
+
if ids.empty?
|
14
|
+
@records = []
|
15
|
+
@loaded = true
|
16
|
+
return self
|
17
|
+
end
|
18
|
+
|
19
|
+
@records = where(id: ids).unscope(:limit).unscope(:offset).load
|
20
|
+
@loaded = true
|
21
|
+
|
22
|
+
self
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/fast_page.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/lazy_load_hooks"
|
4
|
+
require_relative "fast_page/version"
|
5
|
+
require_relative "fast_page/active_record_extension"
|
6
|
+
|
7
|
+
ActiveSupport.on_load :active_record do
|
8
|
+
# ActiveRecord::QueryLogs.include(ActiveRecord::SqlCommenter::QueryLogsTagsFormat)
|
9
|
+
::ActiveRecord::Base.include FastPage::ActiveRecordExtension
|
10
|
+
end
|
11
|
+
|
12
|
+
module FastPage
|
13
|
+
class Error < StandardError; end
|
14
|
+
# Your code goes here...
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fast_page
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Coutermarsh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-08-11 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: 6.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 6.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 6.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 6.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: kaminari
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.2'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.2'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pagy
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.10'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.10'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: FastPage applies the MySQL "deferred join" optimization to your ActiveRecord
|
98
|
+
offset/limit queries.
|
99
|
+
email:
|
100
|
+
- coutermarsh.mike@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".github/workflows/licensing.yml"
|
106
|
+
- ".github/workflows/main.yml"
|
107
|
+
- ".gitignore"
|
108
|
+
- ".rubocop.yml"
|
109
|
+
- ".ruby-version"
|
110
|
+
- CODE_OF_CONDUCT.md
|
111
|
+
- Gemfile
|
112
|
+
- Gemfile.lock
|
113
|
+
- LICENSE.txt
|
114
|
+
- README.md
|
115
|
+
- Rakefile
|
116
|
+
- bin/console
|
117
|
+
- bin/setup
|
118
|
+
- doc/dependency_decisions.yml
|
119
|
+
- fast_page.gemspec
|
120
|
+
- lib/fast_page.rb
|
121
|
+
- lib/fast_page/active_record_extension.rb
|
122
|
+
- lib/fast_page/active_record_methods.rb
|
123
|
+
- lib/fast_page/version.rb
|
124
|
+
homepage: https://github.com/planetscale/fast_page
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
metadata:
|
128
|
+
homepage_uri: https://github.com/planetscale/fast_page
|
129
|
+
source_code_uri: https://github.com/planetscale/fast_page
|
130
|
+
changelog_uri: https://github.com/planetscale/fast_page
|
131
|
+
post_install_message:
|
132
|
+
rdoc_options: []
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: 2.4.0
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
requirements: []
|
146
|
+
rubygems_version: 3.3.7
|
147
|
+
signing_key:
|
148
|
+
specification_version: 4
|
149
|
+
summary: Blazing fast pagination for ActiveRecord with deferred joins
|
150
|
+
test_files: []
|