jsom-pagination 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/gem-push.yml +7 -3
- data/.github/workflows/gem-test.yml +13 -9
- data/.gitignore +5 -0
- data/.rubocop.disabled.yml +12 -0
- data/.rubocop.yml +9 -7
- data/README.md +83 -0
- data/jsom-pagination.gemspec +5 -3
- data/lib/jsom/pagination/links.rb +23 -5
- data/lib/jsom/pagination/paginator.rb +1 -1
- data/lib/jsom/pagination/version.rb +1 -1
- metadata +42 -8
- data/Gemfile.lock +0 -75
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27b6274de4e4cdc7a40b3b729be4b541648402b89a5d1226339df372c6d5926f
|
4
|
+
data.tar.gz: c19be09cfa6e72aae8cd725d102999f5b40fafd56d2973405d4c9b40615342bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb5be3723a23a0c13769a65aa9f0bdc73106348e14c3a970823435641764a2748837f0e8cb282830f652dc7dcc5ddb35c4f0c3aaf3d9e3408f9b52082d4340ad
|
7
|
+
data.tar.gz: e1a3c291ca065a5fad11ae3705d1a60117ca36d82cfe4803f2f16d52d6f276396bc67860e56793f46a75be7527b488ecfa54733d25f1802829099f8a9e97eece
|
@@ -8,13 +8,17 @@ jobs:
|
|
8
8
|
build:
|
9
9
|
name: Build + Publish
|
10
10
|
runs-on: ubuntu-latest
|
11
|
+
strategy:
|
12
|
+
matrix:
|
13
|
+
ruby-version: ['2.7', '3.0']
|
11
14
|
|
12
15
|
steps:
|
13
16
|
- uses: actions/checkout@v2
|
14
|
-
- name: Set up Ruby
|
15
|
-
uses:
|
17
|
+
- name: Set up Ruby
|
18
|
+
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
|
16
19
|
with:
|
17
|
-
ruby-version:
|
20
|
+
ruby-version: ${{ matrix.ruby-version }}
|
21
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
18
22
|
- name: Run tests
|
19
23
|
run: |
|
20
24
|
bundle install
|
@@ -10,20 +10,24 @@ jobs:
|
|
10
10
|
build:
|
11
11
|
name: Build + Test
|
12
12
|
runs-on: ubuntu-latest
|
13
|
+
strategy:
|
14
|
+
matrix:
|
15
|
+
ruby-version: ['2.7', '3.0']
|
13
16
|
|
14
17
|
steps:
|
15
18
|
- uses: actions/checkout@v2
|
16
|
-
- name: Set up Ruby
|
17
|
-
uses:
|
19
|
+
- name: Set up Ruby
|
20
|
+
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
|
18
21
|
with:
|
19
|
-
ruby-version:
|
22
|
+
ruby-version: ${{ matrix.ruby-version }}
|
23
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
20
24
|
- name: Run tests
|
21
25
|
run: |
|
22
26
|
bundle install
|
23
27
|
CI=true bundle exec rspec
|
24
|
-
- name: Upload report
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
# - name: Upload report
|
29
|
+
# run: |
|
30
|
+
# mv ./coverage/coverage.xml cobertura.xml
|
31
|
+
# bash <(curl -Ls https://coverage.codacy.com/get.sh) report
|
32
|
+
# env:
|
33
|
+
# CODACY_PROJECT_TOKEN: ${{secrets.CODACY_PROJECT_TOKEN}}
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
inherit_from:
|
1
|
+
inherit_from:
|
2
|
+
- https://raw.githubusercontent.com/yousty/qa-tools/master/rubocop/base/.rubocop.yml # common enabled rules
|
3
|
+
- https://raw.githubusercontent.com/yousty/qa-tools/master/rubocop/base/.rubocop.disabled.yml # common disabled rules
|
4
|
+
- .rubocop.disabled.yml # project specific disabled rules
|
2
5
|
|
3
|
-
|
4
|
-
Exclude:
|
5
|
-
- "spec/**/*_spec.rb"
|
6
|
+
# project specific overwritten rules
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
# example:
|
9
|
+
# Layout/LineLength:
|
10
|
+
# Max: 100
|
11
|
+
# AllowHeredoc: true
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# JSOM::Pagination
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/jsom-pagination.svg)](https://badge.fury.io/rb/jsom-pagination)
|
4
|
+
![Tests](https://github.com/useo-pl/jsom-pagination/workflows/Run%20tests/badge.svg?branch=master&event=push)
|
4
5
|
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/bc98c8c017c64b70a11ca79aea6c3e3c)](https://app.codacy.com/gh/useo-pl/jsom-pagination?utm_source=github.com&utm_medium=referral&utm_content=useo-pl/jsom-pagination&utm_campaign=Badge_Grade_Dashboard)
|
5
6
|
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/eab277bd1f694da88691a2e645adbe96)](https://www.codacy.com/gh/useo-pl/jsom-pagination?utm_source=github.com&utm_medium=referral&utm_content=useo-pl/jsom-pagination&utm_campaign=Badge_Coverage)
|
6
7
|
|
@@ -81,6 +82,88 @@ options = { meta: paginated.meta.to_h, links: paginated.links.to_h }
|
|
81
82
|
render json: serializer.new(paginated.items, options)
|
82
83
|
```
|
83
84
|
|
85
|
+
### Summary
|
86
|
+
|
87
|
+
Using the information above, you can put everything all together by using [jsom-pagination](https://github.com/useo-pl/jsom-pagination) in a ruby framework of your choice.
|
88
|
+
|
89
|
+
Below is an illustration in [Rails](https://rubyonrails.org/):
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
# app/controllers/concerns/paginable.rb
|
93
|
+
module Paginable
|
94
|
+
extend ActiveSupport::Concern
|
95
|
+
|
96
|
+
def paginator
|
97
|
+
JSOM::Pagination::Paginator.new
|
98
|
+
end
|
99
|
+
|
100
|
+
def pagination_params
|
101
|
+
params.permit![:page] # defaults to 20 pages
|
102
|
+
end
|
103
|
+
|
104
|
+
def paginate(collection)
|
105
|
+
paginator.call(collection, params: pagination_params, base_url: request.url)
|
106
|
+
end
|
107
|
+
|
108
|
+
def render_collection(paginated)
|
109
|
+
options = {
|
110
|
+
# meta: paginated.meta.to_h, # Will get total pages, total count, etc.
|
111
|
+
links: paginated.links.to_h
|
112
|
+
}
|
113
|
+
paginated_result = serializer.new(paginated.items, options)
|
114
|
+
|
115
|
+
render json: paginated_result
|
116
|
+
end
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
# app/controllers/articles_controllers.rb
|
122
|
+
class ArtclesController < ApplicationController
|
123
|
+
include Paginable
|
124
|
+
|
125
|
+
def index
|
126
|
+
articles = articles = Article.order('created_at DESC')
|
127
|
+
paginated = paginate(articles)
|
128
|
+
|
129
|
+
articles.present? ? render_collection(paginated) : :not_found
|
130
|
+
end
|
131
|
+
|
132
|
+
private
|
133
|
+
|
134
|
+
def serializer
|
135
|
+
ArticleSerializer
|
136
|
+
end
|
137
|
+
end
|
138
|
+
```
|
139
|
+
|
140
|
+
The response from the paginated json will look like below:
|
141
|
+
|
142
|
+
```json
|
143
|
+
{
|
144
|
+
"data": [
|
145
|
+
{
|
146
|
+
"id": "5404329",
|
147
|
+
"type": "article",
|
148
|
+
"attributes": {
|
149
|
+
"author": "Canan Ercan",
|
150
|
+
"copies": 10000,
|
151
|
+
"publisher": "Webster & Canan Publishers",
|
152
|
+
"price": "700"
|
153
|
+
}
|
154
|
+
}
|
155
|
+
],
|
156
|
+
"links": {
|
157
|
+
"first": "http://127.0.0.1:3000/api/v1/articles",
|
158
|
+
"prev": "http://127.0.0.1:3000/api/v1/articles?page[number]=349",
|
159
|
+
"self": "http://127.0.0.1:3000/api/v1/articles?page[number]=350",
|
160
|
+
"next": "http://127.0.0.1:3000/api/v1/articles?page[number]=351",
|
161
|
+
"last": "http://127.0.0.1:3000/api/v1/articles?page[number]=352"
|
162
|
+
}
|
163
|
+
}
|
164
|
+
```
|
165
|
+
|
166
|
+
|
84
167
|
## Development
|
85
168
|
|
86
169
|
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.
|
data/jsom-pagination.gemspec
CHANGED
@@ -30,15 +30,17 @@ Gem::Specification.new do |spec|
|
|
30
30
|
# that have been added into git.
|
31
31
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
32
32
|
`git ls-files -z`.split("\x0").reject do |f|
|
33
|
-
f.match(%r{^(test|spec|features)
|
33
|
+
f.match(%r{^(test|spec|features|Gemfile\.lock)/?})
|
34
34
|
end
|
35
35
|
end
|
36
36
|
spec.bindir = 'exe'
|
37
37
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
38
38
|
spec.require_paths = ['lib']
|
39
39
|
|
40
|
-
spec.add_dependency 'dry-struct', '~>1.3
|
41
|
-
spec.add_dependency 'pagy', '~>
|
40
|
+
spec.add_dependency 'dry-struct', '~>1.3'
|
41
|
+
spec.add_dependency 'pagy', '~>4.0'
|
42
|
+
spec.add_dependency 'rack'
|
43
|
+
spec.add_dependency 'addressable', '~> 2.3', '>= 2.3.7'
|
42
44
|
|
43
45
|
spec.add_development_dependency 'simplecov'
|
44
46
|
spec.add_development_dependency 'simplecov-cobertura'
|
@@ -1,5 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'rack/utils'
|
4
|
+
require 'addressable'
|
3
5
|
module JSOM
|
4
6
|
module Pagination
|
5
7
|
class Links
|
@@ -30,12 +32,23 @@ module JSOM
|
|
30
32
|
attr_reader :page, :total_pages, :url
|
31
33
|
|
32
34
|
def initialize(page:, url:, total_pages:)
|
33
|
-
@url = url
|
35
|
+
@url = parse_url(url)
|
34
36
|
@page = page
|
35
37
|
@total_pages = total_pages
|
36
38
|
generate_links
|
37
39
|
end
|
38
40
|
|
41
|
+
def parse_url(url)
|
42
|
+
uri = Addressable::URI.parse(url)
|
43
|
+
|
44
|
+
url_params = Rack::Utils.parse_nested_query(
|
45
|
+
uri.query
|
46
|
+
).delete_if { |key, _value| key == 'page' }
|
47
|
+
uri.query = to_query(url_params)
|
48
|
+
uri.query = nil if uri.query.empty?
|
49
|
+
uri
|
50
|
+
end
|
51
|
+
|
39
52
|
def generate_links
|
40
53
|
if page.number > 1
|
41
54
|
@first = generate_url(1)
|
@@ -50,7 +63,8 @@ module JSOM
|
|
50
63
|
end
|
51
64
|
|
52
65
|
def generate_url(page_number)
|
53
|
-
|
66
|
+
separator = url.to_s.include?('?') ? '&' : '?'
|
67
|
+
[url.to_s, url_params(page_number)].reject(&:empty?).join(separator)
|
54
68
|
end
|
55
69
|
|
56
70
|
def url_params(page_number)
|
@@ -61,12 +75,16 @@ module JSOM
|
|
61
75
|
to_query(url_params)
|
62
76
|
end
|
63
77
|
|
64
|
-
def to_query(obj, namespace =
|
78
|
+
def to_query(obj, namespace = '')
|
65
79
|
query = obj.collect do |key, value|
|
66
80
|
if value.is_a?(Hash)
|
67
|
-
to_query(value, namespace ? "#{namespace}[#{key}]"
|
81
|
+
to_query(value, namespace.empty? ? key : "#{namespace}[#{key}]")
|
68
82
|
else
|
69
|
-
|
83
|
+
if namespace.empty?
|
84
|
+
[key, value].join('=')
|
85
|
+
else
|
86
|
+
["#{namespace}[#{key}]", value].join('=')
|
87
|
+
end
|
70
88
|
end
|
71
89
|
end.compact
|
72
90
|
|
@@ -19,7 +19,7 @@ module JSOM
|
|
19
19
|
attr_reader :page
|
20
20
|
|
21
21
|
def pagy_custom(collection, page:)
|
22
|
-
pagy = Pagy.new(count: collection.
|
22
|
+
pagy = Pagy.new(count: collection.size, page: page.number, items: page.size)
|
23
23
|
paginated =
|
24
24
|
if collection.is_a?(Array)
|
25
25
|
collection[pagy.offset, pagy.items]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsom-pagination
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sebastian Wilgosz
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-struct
|
@@ -16,28 +16,62 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 1.3
|
19
|
+
version: '1.3'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 1.3
|
26
|
+
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: pagy
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: '4.0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: '4.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rack
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: addressable
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.3'
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 2.3.7
|
65
|
+
type: :runtime
|
66
|
+
prerelease: false
|
67
|
+
version_requirements: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - "~>"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '2.3'
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 2.3.7
|
41
75
|
- !ruby/object:Gem::Dependency
|
42
76
|
name: simplecov
|
43
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -79,13 +113,13 @@ files:
|
|
79
113
|
- ".github/workflows/gem-test.yml"
|
80
114
|
- ".gitignore"
|
81
115
|
- ".rspec"
|
116
|
+
- ".rubocop.disabled.yml"
|
82
117
|
- ".rubocop.yml"
|
83
118
|
- ".ruby-gemset"
|
84
119
|
- ".ruby-version"
|
85
120
|
- ".travis.yml"
|
86
121
|
- CODE_OF_CONDUCT.md
|
87
122
|
- Gemfile
|
88
|
-
- Gemfile.lock
|
89
123
|
- LICENSE.txt
|
90
124
|
- README.md
|
91
125
|
- Rakefile
|
@@ -124,7 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
158
|
- !ruby/object:Gem::Version
|
125
159
|
version: '0'
|
126
160
|
requirements: []
|
127
|
-
rubygems_version: 3.
|
161
|
+
rubygems_version: 3.2.3
|
128
162
|
signing_key:
|
129
163
|
specification_version: 4
|
130
164
|
summary: A pagination support for JSON:API based web applications.
|
data/Gemfile.lock
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
jsom-pagination (0.1.1)
|
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
|
-
docile (1.3.2)
|
14
|
-
dry-configurable (0.11.6)
|
15
|
-
concurrent-ruby (~> 1.0)
|
16
|
-
dry-core (~> 0.4, >= 0.4.7)
|
17
|
-
dry-equalizer (~> 0.2)
|
18
|
-
dry-container (0.7.2)
|
19
|
-
concurrent-ruby (~> 1.0)
|
20
|
-
dry-configurable (~> 0.1, >= 0.1.3)
|
21
|
-
dry-core (0.4.9)
|
22
|
-
concurrent-ruby (~> 1.0)
|
23
|
-
dry-equalizer (0.3.0)
|
24
|
-
dry-inflector (0.2.0)
|
25
|
-
dry-logic (1.0.7)
|
26
|
-
concurrent-ruby (~> 1.0)
|
27
|
-
dry-core (~> 0.2)
|
28
|
-
dry-equalizer (~> 0.2)
|
29
|
-
dry-struct (1.3.0)
|
30
|
-
dry-core (~> 0.4, >= 0.4.4)
|
31
|
-
dry-equalizer (~> 0.3)
|
32
|
-
dry-types (~> 1.3)
|
33
|
-
ice_nine (~> 0.11)
|
34
|
-
dry-types (1.4.0)
|
35
|
-
concurrent-ruby (~> 1.0)
|
36
|
-
dry-container (~> 0.3)
|
37
|
-
dry-core (~> 0.4, >= 0.4.4)
|
38
|
-
dry-equalizer (~> 0.3)
|
39
|
-
dry-inflector (~> 0.1, >= 0.1.2)
|
40
|
-
dry-logic (~> 1.0, >= 1.0.2)
|
41
|
-
ice_nine (0.11.2)
|
42
|
-
pagy (3.8.3)
|
43
|
-
rake (12.3.3)
|
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.2)
|
49
|
-
rspec-support (~> 3.9.3)
|
50
|
-
rspec-expectations (3.9.2)
|
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.3)
|
57
|
-
simplecov (0.19.0)
|
58
|
-
docile (~> 1.1)
|
59
|
-
simplecov-html (~> 0.11)
|
60
|
-
simplecov-cobertura (1.4.0)
|
61
|
-
simplecov (~> 0.8)
|
62
|
-
simplecov-html (0.12.2)
|
63
|
-
|
64
|
-
PLATFORMS
|
65
|
-
ruby
|
66
|
-
|
67
|
-
DEPENDENCIES
|
68
|
-
jsom-pagination!
|
69
|
-
rake (~> 12.0)
|
70
|
-
rspec (~> 3.0)
|
71
|
-
simplecov
|
72
|
-
simplecov-cobertura
|
73
|
-
|
74
|
-
BUNDLED WITH
|
75
|
-
2.1.4
|