jsom-pagination 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +66 -0
- data/LICENSE.txt +21 -0
- data/README.md +94 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/jsom-pagination.gemspec +40 -0
- data/lib/jsom/pagination.rb +16 -0
- data/lib/jsom/pagination/collection.rb +11 -0
- data/lib/jsom/pagination/links.rb +85 -0
- data/lib/jsom/pagination/meta_data.rb +15 -0
- data/lib/jsom/pagination/page.rb +37 -0
- data/lib/jsom/pagination/paginator.rb +33 -0
- data/lib/jsom/pagination/pagy.rb +170 -0
- data/lib/jsom/pagination/types.rb +10 -0
- data/lib/jsom/pagination/version.rb +7 -0
- metadata +100 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 659a0357cf67703a54dff3cfb06a965e91313102e5a1b9a313828cca8bb6515a
|
|
4
|
+
data.tar.gz: 4c7257c42ebf3809cfb3656257b5c79492aee30e983fd2da1dc2653fba4ff5d0
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 83ec1de4a1a1d9337b83cd3e51c40b2d41ca5cbaca7e098c6cdce9141148cdd21416c98eabbe8673fc32c6631656e290a338e2c3962e6ba70d3e85ef1034c967
|
|
7
|
+
data.tar.gz: '051196bfc9e1b8ad9bae79857cdfff9d915209b1e0e3a66f5786bcfd2962c18f673ea500fb25d7d70312f51013715640b0fd84044a7f22a56b46b0418435d728'
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
jsom-pagination
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ruby-2.7.1
|
data/.travis.yml
ADDED
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 sebastian@driggl.com. 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
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
jsom-pagination (0.1.0)
|
|
5
|
+
dry-struct (~> 1.3.0)
|
|
6
|
+
pagy (~> 3.8.3)
|
|
7
|
+
|
|
8
|
+
GEM
|
|
9
|
+
remote: https://rubygems.org/
|
|
10
|
+
specs:
|
|
11
|
+
concurrent-ruby (1.1.7)
|
|
12
|
+
diff-lcs (1.4.4)
|
|
13
|
+
dry-configurable (0.11.6)
|
|
14
|
+
concurrent-ruby (~> 1.0)
|
|
15
|
+
dry-core (~> 0.4, >= 0.4.7)
|
|
16
|
+
dry-equalizer (~> 0.2)
|
|
17
|
+
dry-container (0.7.2)
|
|
18
|
+
concurrent-ruby (~> 1.0)
|
|
19
|
+
dry-configurable (~> 0.1, >= 0.1.3)
|
|
20
|
+
dry-core (0.4.9)
|
|
21
|
+
concurrent-ruby (~> 1.0)
|
|
22
|
+
dry-equalizer (0.3.0)
|
|
23
|
+
dry-inflector (0.2.0)
|
|
24
|
+
dry-logic (1.0.7)
|
|
25
|
+
concurrent-ruby (~> 1.0)
|
|
26
|
+
dry-core (~> 0.2)
|
|
27
|
+
dry-equalizer (~> 0.2)
|
|
28
|
+
dry-struct (1.3.0)
|
|
29
|
+
dry-core (~> 0.4, >= 0.4.4)
|
|
30
|
+
dry-equalizer (~> 0.3)
|
|
31
|
+
dry-types (~> 1.3)
|
|
32
|
+
ice_nine (~> 0.11)
|
|
33
|
+
dry-types (1.4.0)
|
|
34
|
+
concurrent-ruby (~> 1.0)
|
|
35
|
+
dry-container (~> 0.3)
|
|
36
|
+
dry-core (~> 0.4, >= 0.4.4)
|
|
37
|
+
dry-equalizer (~> 0.3)
|
|
38
|
+
dry-inflector (~> 0.1, >= 0.1.2)
|
|
39
|
+
dry-logic (~> 1.0, >= 1.0.2)
|
|
40
|
+
ice_nine (0.11.2)
|
|
41
|
+
pagy (3.8.3)
|
|
42
|
+
rake (12.3.3)
|
|
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.2)
|
|
48
|
+
rspec-support (~> 3.9.3)
|
|
49
|
+
rspec-expectations (3.9.2)
|
|
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.3)
|
|
56
|
+
|
|
57
|
+
PLATFORMS
|
|
58
|
+
ruby
|
|
59
|
+
|
|
60
|
+
DEPENDENCIES
|
|
61
|
+
jsom-pagination!
|
|
62
|
+
rake (~> 12.0)
|
|
63
|
+
rspec (~> 3.0)
|
|
64
|
+
|
|
65
|
+
BUNDLED WITH
|
|
66
|
+
2.1.4
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Sebastian Wilgosz
|
|
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,94 @@
|
|
|
1
|
+
# JSOM::Pagination
|
|
2
|
+
|
|
3
|
+
An easy to use JSON:API support for web applications.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'jsom-pagination'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle install
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install jsom-pagination
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
**For arrays**
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
paginator = JSOM::Pagination::Paginator.new
|
|
27
|
+
collection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
28
|
+
paginated = paginator.call(collection, params: { number: 2, size: 3 }, base_ur: 'https://example.com')
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**For ActiveRecord collections**
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
paginator = JSOM::Pagination::Paginator.new
|
|
35
|
+
collection = Article.published
|
|
36
|
+
paginated = paginator.call(collection, params: { number: 2, size: 3 }, base_ur: 'https://example.com')
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
**Meta data object**
|
|
40
|
+
|
|
41
|
+
You can call `meta` on the paginated collection to easily get meta information about the paginated results
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
paginated.meta
|
|
45
|
+
# => #<JSOM::Pagination::MetaData total=10 pages=4>
|
|
46
|
+
|
|
47
|
+
paginated.meta.to_h
|
|
48
|
+
# => {:total=>10, :pages=>4}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
**Links object**
|
|
52
|
+
|
|
53
|
+
You can call `links` on the paginated collection to easily get collection of pagination links for the client
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
paginated.links
|
|
57
|
+
# => #<JSOM::Pagination::Links:0x00007fdf5d0dd2b8 @url="https://example.com", @page=#<JSOM::Pagination::Page number=2 size=3>, @total_pages=4, @first="https://example.com?page[size]=3", @prev="https://example.com?page[size]=3", @self="https://example.com?page[number]=2&page[size]=3", @next="https://example.com?page[number]=3&page[size]=3", @last="https://example.com?page[number]=4&page[size]=3">
|
|
58
|
+
|
|
59
|
+
paginated.links.to_h
|
|
60
|
+
# => {
|
|
61
|
+
# :first=>"https://example.com?page[size]=3",
|
|
62
|
+
# :prev=>"https://example.com?page[size]=3",
|
|
63
|
+
# :self=>"https://example.com?page[number]=2&page[size]=3",
|
|
64
|
+
# :next=>"https://example.com?page[number]=3&page[size]=3",
|
|
65
|
+
# :last=>"https://example.com?page[number]=4&page[size]=3"
|
|
66
|
+
# }
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
### Rendering using fast_jsonapi
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
options = { meta: paginated.meta.to_h, links: paginated.links.to_h }
|
|
74
|
+
render json: serializer.new(paginated.items, options)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Development
|
|
78
|
+
|
|
79
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
80
|
+
|
|
81
|
+
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).
|
|
82
|
+
|
|
83
|
+
## Contributing
|
|
84
|
+
|
|
85
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/useo-pl/jsom-pagination. 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/useo-pl/jsom-pagination/blob/master/CODE_OF_CONDUCT.md).
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
## License
|
|
89
|
+
|
|
90
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
91
|
+
|
|
92
|
+
## Code of Conduct
|
|
93
|
+
|
|
94
|
+
Everyone interacting in the JSOM::Pagination project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/useo-pl/jsom-pagination/blob/master/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 'jsom/pagination'
|
|
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,40 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'lib/jsom/pagination/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = 'jsom-pagination'
|
|
7
|
+
spec.version = JSOM::Pagination::VERSION
|
|
8
|
+
spec.authors = ['Sebastian Wilgosz']
|
|
9
|
+
spec.email = ['sebastian@driggl.com']
|
|
10
|
+
|
|
11
|
+
spec.summary = 'A pagination support for JSON:API based web applications.'
|
|
12
|
+
spec.description = <<-STRING
|
|
13
|
+
Easy to use, framework-agnostic set of methods useful for
|
|
14
|
+
integrating JSON:API in your project.
|
|
15
|
+
STRING
|
|
16
|
+
spec.homepage = 'https://github.com/useo-pl/jsom-pagination'
|
|
17
|
+
spec.license = 'MIT'
|
|
18
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
|
19
|
+
|
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
|
21
|
+
|
|
22
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
23
|
+
spec.metadata['source_code_uri'] = 'https://github.com/useo-pl/jsom-pagination'
|
|
24
|
+
spec.metadata['changelog_uri'] = 'https://github.com/useo-pl/jsom-pagination/releases'
|
|
25
|
+
|
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem
|
|
28
|
+
# that have been added into git.
|
|
29
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
30
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
|
31
|
+
f.match(%r{^(test|spec|features)/})
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
spec.bindir = 'exe'
|
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
36
|
+
spec.require_paths = ['lib']
|
|
37
|
+
|
|
38
|
+
spec.add_dependency 'dry-struct', '~>1.3.0'
|
|
39
|
+
spec.add_dependency 'pagy', '~>3.8.3'
|
|
40
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'jsom/pagination/version'
|
|
4
|
+
require 'jsom/pagination/pagy'
|
|
5
|
+
|
|
6
|
+
require 'jsom/pagination/page'
|
|
7
|
+
require 'jsom/pagination/meta_data'
|
|
8
|
+
require 'jsom/pagination/links'
|
|
9
|
+
require 'jsom/pagination/collection'
|
|
10
|
+
require 'jsom/pagination/paginator'
|
|
11
|
+
|
|
12
|
+
module JSOM
|
|
13
|
+
module Pagination
|
|
14
|
+
class Error < StandardError; end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JSOM
|
|
4
|
+
module Pagination
|
|
5
|
+
class Links
|
|
6
|
+
DEFAULT_PAGE_SIZE = 20
|
|
7
|
+
|
|
8
|
+
def to_h
|
|
9
|
+
{
|
|
10
|
+
first: @first,
|
|
11
|
+
prev: @prev,
|
|
12
|
+
self: @self,
|
|
13
|
+
next: @next,
|
|
14
|
+
last: @last
|
|
15
|
+
}.reject { |_key, value| value.nil? }
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def first
|
|
19
|
+
@first || generate_url(1)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def last
|
|
23
|
+
@last || generate_url(total_pages)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
attr_reader :prev, :next, :self
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
attr_reader :page, :total_pages, :url
|
|
31
|
+
|
|
32
|
+
def initialize(page:, url:, total_pages:)
|
|
33
|
+
@url = url
|
|
34
|
+
@page = page
|
|
35
|
+
@total_pages = total_pages
|
|
36
|
+
generate_links
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def generate_links
|
|
40
|
+
if page.number > 1
|
|
41
|
+
@first = generate_url(1)
|
|
42
|
+
@prev = generate_url(page.number - 1)
|
|
43
|
+
end
|
|
44
|
+
@self = generate_url(page.number)
|
|
45
|
+
|
|
46
|
+
return unless page.number < total_pages
|
|
47
|
+
|
|
48
|
+
@next = generate_url(page.number + 1)
|
|
49
|
+
@last = generate_url(total_pages)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def generate_url(page_number)
|
|
53
|
+
[url, url_params(page_number)].reject(&:empty?).join('?')
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def url_params(page_number)
|
|
57
|
+
url_params = {}
|
|
58
|
+
url_params[:page] = {} if include_per_page? || include_page?(page_number)
|
|
59
|
+
url_params[:page][:number] = page_number if include_page?(page_number)
|
|
60
|
+
url_params[:page][:size] = page.size if include_per_page?
|
|
61
|
+
to_query(url_params)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def to_query(obj, namespace = nil)
|
|
65
|
+
query = obj.collect do |key, value|
|
|
66
|
+
if value.is_a?(Hash)
|
|
67
|
+
to_query(value, namespace ? "#{namespace}[#{key}]" : key)
|
|
68
|
+
else
|
|
69
|
+
["#{namespace}[#{key}]", value].join('=')
|
|
70
|
+
end
|
|
71
|
+
end.compact
|
|
72
|
+
|
|
73
|
+
query.join('&')
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def include_per_page?
|
|
77
|
+
page.size != DEFAULT_PAGE_SIZE
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def include_page?(page_number)
|
|
81
|
+
page_number > 1
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'dry-struct'
|
|
4
|
+
require 'jsom/pagination/types'
|
|
5
|
+
|
|
6
|
+
module JSOM
|
|
7
|
+
module Pagination
|
|
8
|
+
class MetaData < Dry::Struct
|
|
9
|
+
transform_keys(&:to_sym)
|
|
10
|
+
|
|
11
|
+
attribute :total, Types::Params::Integer
|
|
12
|
+
attribute :pages, Types::Params::Integer
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'dry-struct'
|
|
4
|
+
require 'jsom/pagination/types'
|
|
5
|
+
|
|
6
|
+
module JSOM
|
|
7
|
+
module Pagination
|
|
8
|
+
# A value object for requested page. Usage:
|
|
9
|
+
# JSOM::Pagination::Page.new(number: 1, size: 20)
|
|
10
|
+
# Works for string values and symbols, supports defaults.
|
|
11
|
+
# Ignores extra keys
|
|
12
|
+
# Sets 1 for negative values
|
|
13
|
+
#
|
|
14
|
+
class Page < Dry::Struct
|
|
15
|
+
transform_keys(&:to_sym)
|
|
16
|
+
|
|
17
|
+
transform_types do |type|
|
|
18
|
+
return type unless type.default?
|
|
19
|
+
|
|
20
|
+
type.constructor do |value|
|
|
21
|
+
if value.nil?
|
|
22
|
+
Dry::Types::Undefined
|
|
23
|
+
elsif value == Dry::Types::Undefined
|
|
24
|
+
value
|
|
25
|
+
elsif Dry::Types['params.integer'][value].negative?
|
|
26
|
+
1
|
|
27
|
+
else
|
|
28
|
+
value
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
attribute :number, Types::Params::Integer.default(1)
|
|
34
|
+
attribute :size, Types::Params::Integer.default(20)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module JSOM
|
|
4
|
+
module Pagination
|
|
5
|
+
class Paginator
|
|
6
|
+
def call(collection, params:, base_url: '')
|
|
7
|
+
@page = Page.new(params)
|
|
8
|
+
|
|
9
|
+
pagy, records = pagy_custom(collection, page: page)
|
|
10
|
+
|
|
11
|
+
meta = MetaData.new(total: pagy.count, pages: pagy.pages)
|
|
12
|
+
# pagy methods: :count, :page, :items, :vars, :pages, :last, :offset, :from, :to, :prev, :next
|
|
13
|
+
links = Links.new(page: page, total_pages: pagy.pages, url: base_url)
|
|
14
|
+
Collection.new(items: records, links: links, meta: meta)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
attr_reader :page
|
|
20
|
+
|
|
21
|
+
def pagy_custom(collection, page:)
|
|
22
|
+
pagy = Pagy.new(count: collection.count, page: page.number, items: page.size)
|
|
23
|
+
paginated =
|
|
24
|
+
if collection.is_a?(Array)
|
|
25
|
+
collection[pagy.offset, pagy.items]
|
|
26
|
+
else
|
|
27
|
+
collection.offset(pagy.offset).limit(pagy.items).to_a
|
|
28
|
+
end
|
|
29
|
+
return pagy, paginated
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'pagy'
|
|
4
|
+
|
|
5
|
+
# Pagy initializer file (3.8.3)
|
|
6
|
+
# Customize only what you really need and notice that Pagy works also without any of the following lines.
|
|
7
|
+
# Should you just cherry pick part of this file, please maintain the require-order of the extras
|
|
8
|
+
|
|
9
|
+
# Extras
|
|
10
|
+
# See https://ddnexus.github.io/pagy/extras
|
|
11
|
+
|
|
12
|
+
# Backend Extras
|
|
13
|
+
|
|
14
|
+
# Array extra: Paginate arrays efficiently, avoiding expensive array-wrapping and without overriding
|
|
15
|
+
# See https://ddnexus.github.io/pagy/extras/array
|
|
16
|
+
require 'pagy/extras/array'
|
|
17
|
+
|
|
18
|
+
# Countless extra: Paginate without any count, saving one query per rendering
|
|
19
|
+
# See https://ddnexus.github.io/pagy/extras/countless
|
|
20
|
+
# require 'pagy/extras/countless'
|
|
21
|
+
# Pagy::VARS[:cycle] = false # default
|
|
22
|
+
|
|
23
|
+
# Elasticsearch Rails extra: Paginate `ElasticsearchRails::Results` objects
|
|
24
|
+
# See https://ddnexus.github.io/pagy/extras/elasticsearch_rails
|
|
25
|
+
# require 'pagy/extras/elasticsearch_rails'
|
|
26
|
+
|
|
27
|
+
# Searchkick extra: Paginate `Searchkick::Results` objects
|
|
28
|
+
# See https://ddnexus.github.io/pagy/extras/searchkick
|
|
29
|
+
# require 'pagy/extras/searchkick'
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
# Frontend Extras
|
|
33
|
+
|
|
34
|
+
# Bootstrap extra: Add nav, nav_js and combo_nav_js helpers and templates for Bootstrap pagination
|
|
35
|
+
# See https://ddnexus.github.io/pagy/extras/bootstrap
|
|
36
|
+
# require 'pagy/extras/bootstrap'
|
|
37
|
+
|
|
38
|
+
# Bulma extra: Add nav, nav_js and combo_nav_js helpers and templates for Bulma pagination
|
|
39
|
+
# See https://ddnexus.github.io/pagy/extras/bulma
|
|
40
|
+
# require 'pagy/extras/bulma'
|
|
41
|
+
|
|
42
|
+
# Foundation extra: Add nav, nav_js and combo_nav_js helpers and templates for Foundation pagination
|
|
43
|
+
# See https://ddnexus.github.io/pagy/extras/foundation
|
|
44
|
+
# require 'pagy/extras/foundation'
|
|
45
|
+
|
|
46
|
+
# Materialize extra: Add nav, nav_js and combo_nav_js helpers for Materialize pagination
|
|
47
|
+
# See https://ddnexus.github.io/pagy/extras/materialize
|
|
48
|
+
# require 'pagy/extras/materialize'
|
|
49
|
+
|
|
50
|
+
# Navs extra: Add nav_js and combo_nav_js javascript helpers
|
|
51
|
+
# Notice: the other frontend extras add their own framework-styled versions,
|
|
52
|
+
# so require this extra only if you need the unstyled version
|
|
53
|
+
# See https://ddnexus.github.io/pagy/extras/navs
|
|
54
|
+
# require 'pagy/extras/navs'
|
|
55
|
+
|
|
56
|
+
# Semantic extra: Add nav, nav_js and combo_nav_js helpers for Semantic UI pagination
|
|
57
|
+
# See https://ddnexus.github.io/pagy/extras/semantic
|
|
58
|
+
# require 'pagy/extras/semantic'
|
|
59
|
+
|
|
60
|
+
# UIkit extra: Add nav helper and templates for UIkit pagination
|
|
61
|
+
# See https://ddnexus.github.io/pagy/extras/uikit
|
|
62
|
+
# require 'pagy/extras/uikit'
|
|
63
|
+
|
|
64
|
+
# Multi size var used by the *_nav_js helpers
|
|
65
|
+
# See https://ddnexus.github.io/pagy/extras/navs#steps
|
|
66
|
+
# Pagy::VARS[:steps] = { 0 => [2,3,3,2], 540 => [3,5,5,3], 720 => [5,7,7,5] } # example
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
# Feature Extras
|
|
70
|
+
|
|
71
|
+
# Headers extra: http response headers (and other helpers) useful for API pagination
|
|
72
|
+
# See http://ddnexus.github.io/pagy/extras/headers
|
|
73
|
+
# require 'pagy/extras/headers'
|
|
74
|
+
# Pagy::VARS[:headers] = { page: 'Current-Page', items: 'Page-Items', count: 'Total-Count', pages: 'Total-Pages' } # default
|
|
75
|
+
|
|
76
|
+
# Support extra: Extra support for features like: incremental, infinite, auto-scroll pagination
|
|
77
|
+
# See https://ddnexus.github.io/pagy/extras/support
|
|
78
|
+
# require 'pagy/extras/support'
|
|
79
|
+
|
|
80
|
+
# Items extra: Allow the client to request a custom number of items per page with an optional selector UI
|
|
81
|
+
# See https://ddnexus.github.io/pagy/extras/items
|
|
82
|
+
# require 'pagy/extras/items'
|
|
83
|
+
# Pagy::VARS[:items_param] = :items # default
|
|
84
|
+
# Pagy::VARS[:max_items] = 100 # default
|
|
85
|
+
|
|
86
|
+
# Overflow extra: Allow for easy handling of overflowing pages
|
|
87
|
+
# See https://ddnexus.github.io/pagy/extras/overflow
|
|
88
|
+
require 'pagy/extras/overflow'
|
|
89
|
+
|
|
90
|
+
Pagy::VARS[:overflow] = :empty_page
|
|
91
|
+
|
|
92
|
+
# Metadata extra: Provides the pagination metadata to Javascript frameworks like Vue.js, react.js, etc.
|
|
93
|
+
# See https://ddnexus.github.io/pagy/extras/metadata
|
|
94
|
+
# you must require the shared internal extra (BEFORE the metadata extra) ONLY if you need also the :sequels
|
|
95
|
+
# require 'pagy/extras/shared'
|
|
96
|
+
# require 'pagy/extras/metadata'
|
|
97
|
+
# For performance reason, you should explicitly set ONLY the metadata you use in the frontend
|
|
98
|
+
# Pagy::VARS[:metadata] = [:scaffold_url, :count, :page, :prev, :next, :last] # example
|
|
99
|
+
|
|
100
|
+
# Trim extra: Remove the page=1 param from links
|
|
101
|
+
# See https://ddnexus.github.io/pagy/extras/trim
|
|
102
|
+
# require 'pagy/extras/trim'
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
# Pagy Variables
|
|
107
|
+
# See https://ddnexus.github.io/pagy/api/pagy#variables
|
|
108
|
+
# All the Pagy::VARS are set for all the Pagy instances but can be overridden
|
|
109
|
+
# per instance by just passing them to Pagy.new or the #pagy controller method
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
# Instance variables
|
|
113
|
+
# See https://ddnexus.github.io/pagy/api/pagy#instance-variables
|
|
114
|
+
# Pagy::VARS[:items] = 20 # default
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
# Other Variables
|
|
118
|
+
# See https://ddnexus.github.io/pagy/api/pagy#other-variables
|
|
119
|
+
# Pagy::VARS[:size] = [1,4,4,1] # default
|
|
120
|
+
# Pagy::VARS[:page_param] = :page # default
|
|
121
|
+
# Pagy::VARS[:params] = {} # default
|
|
122
|
+
# Pagy::VARS[:anchor] = '#anchor' # example
|
|
123
|
+
# Pagy::VARS[:link_extra] = 'data-remote="true"' # example
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
# Rails
|
|
127
|
+
|
|
128
|
+
# Rails: extras assets path required by the helpers that use javascript
|
|
129
|
+
# (pagy*_nav_js, pagy*_combo_nav_js, and pagy_items_selector_js)
|
|
130
|
+
# See https://ddnexus.github.io/pagy/extras#javascript
|
|
131
|
+
# Rails.application.config.assets.paths << Pagy.root.join('javascripts')
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
# I18n
|
|
135
|
+
|
|
136
|
+
# Pagy internal I18n: ~18x faster using ~10x less memory than the i18n gem
|
|
137
|
+
# See https://ddnexus.github.io/pagy/api/frontend#i18n
|
|
138
|
+
# Notice: No need to configure anything in this section if your app uses only "en"
|
|
139
|
+
# or if you use the i18n extra below
|
|
140
|
+
#
|
|
141
|
+
# Examples:
|
|
142
|
+
# load the "de" built-in locale:
|
|
143
|
+
# Pagy::I18n.load(locale: 'de')
|
|
144
|
+
#
|
|
145
|
+
# load the "de" locale defined in the custom file at :filepath:
|
|
146
|
+
# Pagy::I18n.load(locale: 'de', filepath: 'path/to/pagy-de.yml')
|
|
147
|
+
#
|
|
148
|
+
# load the "de", "en" and "es" built-in locales:
|
|
149
|
+
# (the first passed :locale will be used also as the default_locale)
|
|
150
|
+
# Pagy::I18n.load({locale: 'de'},
|
|
151
|
+
# {locale: 'en'},
|
|
152
|
+
# {locale: 'es'})
|
|
153
|
+
#
|
|
154
|
+
# load the "en" built-in locale, a custom "es" locale,
|
|
155
|
+
# and a totally custom locale complete with a custom :pluralize proc:
|
|
156
|
+
# (the first passed :locale will be used also as the default_locale)
|
|
157
|
+
# Pagy::I18n.load({locale: 'en'},
|
|
158
|
+
# {locale: 'es', filepath: 'path/to/pagy-es.yml'},
|
|
159
|
+
# {locale: 'xyz', # not built-in
|
|
160
|
+
# filepath: 'path/to/pagy-xyz.yml',
|
|
161
|
+
# pluralize: lambda{|count| ... } )
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
# I18n extra: uses the standard i18n gem which is ~18x slower using ~10x more memory
|
|
165
|
+
# than the default pagy internal i18n (see above)
|
|
166
|
+
# See https://ddnexus.github.io/pagy/extras/i18n
|
|
167
|
+
# require 'pagy/extras/i18n'
|
|
168
|
+
|
|
169
|
+
# Default i18n key
|
|
170
|
+
# Pagy::VARS[:i18n_key] = 'pagy.item_name' # default
|
metadata
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: jsom-pagination
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Sebastian Wilgosz
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2020-09-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: dry-struct
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 1.3.0
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 1.3.0
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: pagy
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 3.8.3
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 3.8.3
|
|
41
|
+
description: |2
|
|
42
|
+
Easy to use, framework-agnostic set of methods useful for
|
|
43
|
+
integrating JSON:API in your project.
|
|
44
|
+
email:
|
|
45
|
+
- sebastian@driggl.com
|
|
46
|
+
executables: []
|
|
47
|
+
extensions: []
|
|
48
|
+
extra_rdoc_files: []
|
|
49
|
+
files:
|
|
50
|
+
- ".gitignore"
|
|
51
|
+
- ".rspec"
|
|
52
|
+
- ".ruby-gemset"
|
|
53
|
+
- ".ruby-version"
|
|
54
|
+
- ".travis.yml"
|
|
55
|
+
- CODE_OF_CONDUCT.md
|
|
56
|
+
- Gemfile
|
|
57
|
+
- Gemfile.lock
|
|
58
|
+
- LICENSE.txt
|
|
59
|
+
- README.md
|
|
60
|
+
- Rakefile
|
|
61
|
+
- bin/console
|
|
62
|
+
- bin/setup
|
|
63
|
+
- jsom-pagination.gemspec
|
|
64
|
+
- lib/jsom/pagination.rb
|
|
65
|
+
- lib/jsom/pagination/collection.rb
|
|
66
|
+
- lib/jsom/pagination/links.rb
|
|
67
|
+
- lib/jsom/pagination/meta_data.rb
|
|
68
|
+
- lib/jsom/pagination/page.rb
|
|
69
|
+
- lib/jsom/pagination/paginator.rb
|
|
70
|
+
- lib/jsom/pagination/pagy.rb
|
|
71
|
+
- lib/jsom/pagination/types.rb
|
|
72
|
+
- lib/jsom/pagination/version.rb
|
|
73
|
+
homepage: https://github.com/useo-pl/jsom-pagination
|
|
74
|
+
licenses:
|
|
75
|
+
- MIT
|
|
76
|
+
metadata:
|
|
77
|
+
allowed_push_host: https://rubygems.org
|
|
78
|
+
homepage_uri: https://github.com/useo-pl/jsom-pagination
|
|
79
|
+
source_code_uri: https://github.com/useo-pl/jsom-pagination
|
|
80
|
+
changelog_uri: https://github.com/useo-pl/jsom-pagination/releases
|
|
81
|
+
post_install_message:
|
|
82
|
+
rdoc_options: []
|
|
83
|
+
require_paths:
|
|
84
|
+
- lib
|
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 2.3.0
|
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
|
+
requirements:
|
|
92
|
+
- - ">="
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: '0'
|
|
95
|
+
requirements: []
|
|
96
|
+
rubygems_version: 3.1.2
|
|
97
|
+
signing_key:
|
|
98
|
+
specification_version: 4
|
|
99
|
+
summary: A pagination support for JSON:API based web applications.
|
|
100
|
+
test_files: []
|