grape-kaminari 0.3.0 → 0.4.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 +4 -4
- data/CHANGELOG.md +42 -0
- data/Gemfile.lock +13 -13
- data/README.md +3 -3
- data/lib/grape/kaminari.rb +24 -16
- data/lib/grape/kaminari/version.rb +1 -1
- data/spec/kaminari_spec.rb +91 -90
- data/spec/paginate_helper_spec.rb +9 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5be6c20ba0aef343949970af43c69d6781e945ed043b3baa3b6c258570f4300b
|
4
|
+
data.tar.gz: f9f31a5f31560e5cd751b31109d5720410ea4f8c523a1d0b4b050c1eef039247
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1205ab3b90b586436aaeeb2f94ce4443acdbd372b72a6e1acda2cb250081f976e4b9743eb4f36bdc92d82dc0174d16289202c61128536a1ca440c64f5c3d923
|
7
|
+
data.tar.gz: 3c8f039a3f9596fc78bb9fd79adf917e79a662b372f480da7aa0bf35a75a07d1b40cc20557c9038300b92e3f12f566ad512ad6a9aede7c4f18f11b3b778c8997
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
### 0.5.0 (Next)
|
2
|
+
|
3
|
+
#### Features
|
4
|
+
|
5
|
+
* Your contribution here.
|
6
|
+
|
7
|
+
#### Fixes
|
8
|
+
|
9
|
+
* Your contribution here.
|
10
|
+
|
11
|
+
### 0.4.0
|
12
|
+
|
13
|
+
#### Features
|
14
|
+
|
15
|
+
* [#57](https://github.com/bsm/grape-kaminari/pull/57): Introduce new params based API - [@dim](https://github.com/dim).
|
16
|
+
* [#56](https://github.com/bsm/grape-kaminari/pull/56): Introduce a CHANGELOG - [@dim](https://github.com/dim).
|
17
|
+
|
18
|
+
#### Fixes
|
19
|
+
|
20
|
+
* [#57](https://github.com/bsm/grape-kaminari/pull/57): Fix issues related to Grape v1.5 release - [@dim](https://github.com/dim).
|
21
|
+
|
22
|
+
### 0.3.0 (2020/08/10)
|
23
|
+
|
24
|
+
* [#54](https://github.com/bsm/grape-kaminari/pull/54): Inherit paginate helper - [@dim](https://github.com/dim).
|
25
|
+
|
26
|
+
### 0.2.1 (2020/06/18)
|
27
|
+
|
28
|
+
#### Fixes
|
29
|
+
|
30
|
+
* [#52](https://github.com/bsm/grape-kaminari/pull/52): Skip per-page range validation for non-integer values - [@dim](https://github.com/dim).
|
31
|
+
|
32
|
+
### 0.2.0 (2020/06/18)
|
33
|
+
|
34
|
+
Revived project, migrated to [github.com/bsm/grape-kaminari](github.com/bsm/grape-kaminari).
|
35
|
+
|
36
|
+
#### Features
|
37
|
+
|
38
|
+
* [#51](https://github.com/bsm/grape-kaminari/pull/51): Support for Grape 1.x - [@dim](https://github.com/dim).
|
39
|
+
|
40
|
+
#### Fixes
|
41
|
+
|
42
|
+
* [#51](https://github.com/bsm/grape-kaminari/pull/51): Fixed code style - [@dim](https://github.com/dim).
|
data/Gemfile.lock
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
grape-kaminari (0.
|
4
|
+
grape-kaminari (0.4.0)
|
5
5
|
grape (>= 1.0, != 1.4.0)
|
6
6
|
kaminari-grape
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
activesupport (6.0.3.
|
11
|
+
activesupport (6.0.3.4)
|
12
12
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
13
|
i18n (>= 0.7, < 2)
|
14
14
|
minitest (~> 5.1)
|
@@ -29,7 +29,7 @@ GEM
|
|
29
29
|
concurrent-ruby (~> 1.0)
|
30
30
|
dry-equalizer (0.3.0)
|
31
31
|
dry-inflector (0.2.0)
|
32
|
-
dry-logic (1.0.
|
32
|
+
dry-logic (1.0.8)
|
33
33
|
concurrent-ruby (~> 1.0)
|
34
34
|
dry-core (~> 0.2)
|
35
35
|
dry-equalizer (~> 0.2)
|
@@ -40,7 +40,7 @@ GEM
|
|
40
40
|
dry-equalizer (~> 0.3)
|
41
41
|
dry-inflector (~> 0.1, >= 0.1.2)
|
42
42
|
dry-logic (~> 1.0, >= 1.0.2)
|
43
|
-
grape (1.
|
43
|
+
grape (1.5.0)
|
44
44
|
activesupport
|
45
45
|
builder
|
46
46
|
dry-types (>= 1.1)
|
@@ -53,13 +53,13 @@ GEM
|
|
53
53
|
kaminari-grape (1.0.1)
|
54
54
|
grape
|
55
55
|
kaminari-core (~> 1.0)
|
56
|
-
minitest (5.14.
|
56
|
+
minitest (5.14.2)
|
57
57
|
mustermann (1.1.1)
|
58
58
|
ruby2_keywords (~> 0.0.1)
|
59
59
|
mustermann-grape (1.0.1)
|
60
60
|
mustermann (>= 1.0.0)
|
61
61
|
parallel (1.19.2)
|
62
|
-
parser (2.7.
|
62
|
+
parser (2.7.2.0)
|
63
63
|
ast (~> 2.4.1)
|
64
64
|
rack (2.2.3)
|
65
65
|
rack-accept (0.4.5)
|
@@ -68,13 +68,13 @@ GEM
|
|
68
68
|
rack (>= 1.0, < 3)
|
69
69
|
rainbow (3.0.0)
|
70
70
|
rake (13.0.1)
|
71
|
-
regexp_parser (1.
|
71
|
+
regexp_parser (1.8.1)
|
72
72
|
rexml (3.2.4)
|
73
73
|
rspec (3.9.0)
|
74
74
|
rspec-core (~> 3.9.0)
|
75
75
|
rspec-expectations (~> 3.9.0)
|
76
76
|
rspec-mocks (~> 3.9.0)
|
77
|
-
rspec-core (3.9.
|
77
|
+
rspec-core (3.9.3)
|
78
78
|
rspec-support (~> 3.9.3)
|
79
79
|
rspec-expectations (3.9.2)
|
80
80
|
diff-lcs (>= 1.2.0, < 2.0)
|
@@ -83,17 +83,17 @@ GEM
|
|
83
83
|
diff-lcs (>= 1.2.0, < 2.0)
|
84
84
|
rspec-support (~> 3.9.0)
|
85
85
|
rspec-support (3.9.3)
|
86
|
-
rubocop (0.
|
86
|
+
rubocop (0.92.0)
|
87
87
|
parallel (~> 1.10)
|
88
|
-
parser (>= 2.7.1.
|
88
|
+
parser (>= 2.7.1.5)
|
89
89
|
rainbow (>= 2.2.2, < 4.0)
|
90
90
|
regexp_parser (>= 1.7)
|
91
91
|
rexml
|
92
|
-
rubocop-ast (>= 0.
|
92
|
+
rubocop-ast (>= 0.5.0)
|
93
93
|
ruby-progressbar (~> 1.7)
|
94
94
|
unicode-display_width (>= 1.4.0, < 2.0)
|
95
|
-
rubocop-ast (0.
|
96
|
-
parser (>= 2.7.1.
|
95
|
+
rubocop-ast (0.7.1)
|
96
|
+
parser (>= 2.7.1.5)
|
97
97
|
ruby-progressbar (1.10.1)
|
98
98
|
ruby2_keywords (0.0.2)
|
99
99
|
thread_safe (0.3.6)
|
data/README.md
CHANGED
@@ -35,7 +35,6 @@ class MyApi < Grape::API
|
|
35
35
|
resource :posts do
|
36
36
|
desc 'Return a list of posts.'
|
37
37
|
|
38
|
-
# Annotate action with `paginate`.
|
39
38
|
# This will add three optional params: page, per_page, and offset
|
40
39
|
#
|
41
40
|
# You can optionally overwrite the default :per_page setting (10)
|
@@ -45,8 +44,9 @@ class MyApi < Grape::API
|
|
45
44
|
# You can disable the offset parameter from appearing in the API
|
46
45
|
# documentation by setting it to false.
|
47
46
|
#
|
48
|
-
|
49
|
-
|
47
|
+
params do
|
48
|
+
use :pagination, per_page: 20, max_per_page: 30, offset: 5
|
49
|
+
end
|
50
50
|
get do
|
51
51
|
posts = Post.where(...)
|
52
52
|
|
data/lib/grape/kaminari.rb
CHANGED
@@ -9,10 +9,30 @@ module Grape
|
|
9
9
|
|
10
10
|
included do
|
11
11
|
helpers HelperMethods
|
12
|
-
base_instance.extend DSLMethods
|
13
12
|
end
|
14
13
|
|
15
14
|
module HelperMethods # :nodoc:
|
15
|
+
extend Grape::API::Helpers
|
16
|
+
|
17
|
+
params :pagination do |opts = {}|
|
18
|
+
opts.reverse_merge!(
|
19
|
+
per_page: ::Kaminari.config.default_per_page || 10,
|
20
|
+
max_per_page: ::Kaminari.config.max_per_page,
|
21
|
+
offset: 0,
|
22
|
+
)
|
23
|
+
|
24
|
+
optional :page, type: Integer, default: 1,
|
25
|
+
desc: 'Page offset to fetch.'
|
26
|
+
optional :per_page, type: Integer, default: opts[:per_page],
|
27
|
+
desc: 'Number of results to return per page.',
|
28
|
+
max_value: opts[:max_per_page]
|
29
|
+
|
30
|
+
if opts[:offset].is_a?(Integer)
|
31
|
+
optional :offset, type: Integer, default: opts[:offset],
|
32
|
+
desc: 'Pad a number of results.'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
16
36
|
def paginate(collection)
|
17
37
|
collection.page(params[:page].to_i)
|
18
38
|
.per(params[:per_page].to_i)
|
@@ -30,24 +50,12 @@ module Grape
|
|
30
50
|
end
|
31
51
|
|
32
52
|
module DSLMethods # :nodoc:
|
33
|
-
def paginate(
|
34
|
-
options.reverse_merge!(
|
35
|
-
per_page: ::Kaminari.config.default_per_page || 10,
|
36
|
-
max_per_page: ::Kaminari.config.max_per_page,
|
37
|
-
offset: 0,
|
38
|
-
)
|
53
|
+
def paginate(opts = {})
|
39
54
|
params do
|
40
|
-
|
41
|
-
desc: 'Page offset to fetch.'
|
42
|
-
optional :per_page, type: Integer, default: options[:per_page],
|
43
|
-
desc: 'Number of results to return per page.',
|
44
|
-
max_value: options[:max_per_page]
|
45
|
-
if options[:offset].is_a? Numeric
|
46
|
-
optional :offset, type: Integer, default: options[:offset],
|
47
|
-
desc: 'Pad a number of results.'
|
48
|
-
end
|
55
|
+
use(:pagination, opts)
|
49
56
|
end
|
50
57
|
end
|
51
58
|
end
|
59
|
+
Grape::API::Instance.extend(DSLMethods)
|
52
60
|
end
|
53
61
|
end
|
data/spec/kaminari_spec.rb
CHANGED
@@ -1,130 +1,131 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class UnPaginatedAPI < Grape::API
|
4
|
-
# Intentionally not including Grape::Kaminari
|
5
|
-
end
|
6
|
-
|
7
3
|
class PaginatedAPI < Grape::API
|
8
4
|
include Grape::Kaminari
|
9
5
|
end
|
10
6
|
|
11
7
|
describe Grape::Kaminari do
|
12
|
-
|
13
|
-
subject { Class.new(UnPaginatedAPI) }
|
8
|
+
subject { Class.new(PaginatedAPI) }
|
14
9
|
|
15
|
-
|
16
|
-
|
17
|
-
end
|
10
|
+
def declared_params
|
11
|
+
subject.namespace_stackable(:declared_params).flatten
|
18
12
|
end
|
19
13
|
|
20
|
-
|
21
|
-
subject {
|
22
|
-
|
23
|
-
|
24
|
-
subject.paginate
|
25
|
-
expect(subject.inheritable_setting.route[:declared_params]).to eq(%i[page per_page offset])
|
26
|
-
end
|
27
|
-
|
28
|
-
describe 'descriptions, validation, and defaults' do
|
29
|
-
let(:params) { subject.routes.first.params }
|
30
|
-
|
31
|
-
before do
|
32
|
-
subject.paginate
|
33
|
-
subject.get('/') {}
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'does not require :page' do
|
37
|
-
expect(params['page'][:required]).to eq(false)
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'does not require :per_page' do
|
41
|
-
expect(params['per_page'][:required]).to eq(false)
|
42
|
-
end
|
14
|
+
it 'adds to declared parameters' do
|
15
|
+
subject.params { use :pagination }
|
16
|
+
expect(declared_params).to eq(%i[page per_page offset])
|
17
|
+
end
|
43
18
|
|
44
|
-
|
45
|
-
|
46
|
-
|
19
|
+
it 'may exclude :offset' do
|
20
|
+
subject.params { use :pagination, offset: false }
|
21
|
+
expect(declared_params).to eq(%i[page per_page])
|
22
|
+
end
|
47
23
|
|
48
|
-
|
49
|
-
|
50
|
-
|
24
|
+
it 'should support legacy declarations' do
|
25
|
+
subject.paginate
|
26
|
+
expect(declared_params).to eq(%i[page per_page offset])
|
27
|
+
end
|
51
28
|
|
52
|
-
|
53
|
-
|
54
|
-
|
29
|
+
it 'should not stumble across repeated declarations' do
|
30
|
+
subject.paginate offset: false
|
31
|
+
subject.params do
|
32
|
+
optional :extra
|
33
|
+
end
|
34
|
+
expect(declared_params).to eq(%i[page per_page extra])
|
35
|
+
end
|
55
36
|
|
56
|
-
|
57
|
-
|
58
|
-
end
|
37
|
+
describe 'descriptions, validation, and defaults' do
|
38
|
+
let(:params) { subject.routes.first.params }
|
59
39
|
|
60
|
-
|
61
|
-
|
62
|
-
|
40
|
+
before do
|
41
|
+
subject.params { use :pagination }
|
42
|
+
subject.get('/') {}
|
43
|
+
end
|
63
44
|
|
64
|
-
|
65
|
-
|
66
|
-
|
45
|
+
it 'does not require :page' do
|
46
|
+
expect(params['page'][:required]).to eq(false)
|
47
|
+
end
|
67
48
|
|
68
|
-
|
69
|
-
|
70
|
-
|
49
|
+
it 'does not require :per_page' do
|
50
|
+
expect(params['per_page'][:required]).to eq(false)
|
51
|
+
end
|
71
52
|
|
72
|
-
|
73
|
-
|
74
|
-
|
53
|
+
it 'does not require :offset' do
|
54
|
+
expect(params['offset'][:required]).to eq(false)
|
55
|
+
end
|
75
56
|
|
76
|
-
|
77
|
-
|
78
|
-
|
57
|
+
it 'describes :page' do
|
58
|
+
expect(params['page'][:desc]).to eq('Page offset to fetch.')
|
59
|
+
end
|
79
60
|
|
80
|
-
|
81
|
-
|
82
|
-
end
|
61
|
+
it 'describes :per_page' do
|
62
|
+
expect(params['per_page'][:desc]).to eq('Number of results to return per page.')
|
83
63
|
end
|
84
|
-
end
|
85
64
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
let(:params) { subject.routes.first.params }
|
65
|
+
it 'describes :offset' do
|
66
|
+
expect(params['offset'][:desc]).to eq('Pad a number of results.')
|
67
|
+
end
|
90
68
|
|
91
|
-
|
92
|
-
|
93
|
-
subject.get('/') {}
|
69
|
+
it 'validates :page as Integer' do
|
70
|
+
expect(params['page'][:type]).to eq('Integer')
|
94
71
|
end
|
95
72
|
|
96
|
-
it '
|
97
|
-
expect(params['per_page'][:
|
73
|
+
it 'validates :per_page as Integer' do
|
74
|
+
expect(params['per_page'][:type]).to eq('Integer')
|
98
75
|
end
|
99
76
|
|
100
|
-
it '
|
101
|
-
|
102
|
-
expect(last_response.status).to eq(200)
|
77
|
+
it 'validates :offset as Integer' do
|
78
|
+
expect(params['offset'][:type]).to eq('Integer')
|
103
79
|
end
|
104
80
|
|
105
|
-
it '
|
106
|
-
|
107
|
-
expect(last_response.status).to eq(400)
|
108
|
-
expect(last_response.body).to match(/per_page must be less than or equal 999/)
|
81
|
+
it 'defaults :page to 1' do
|
82
|
+
expect(params['page'][:default]).to eq(1)
|
109
83
|
end
|
110
84
|
|
111
|
-
it '
|
112
|
-
|
113
|
-
expect(last_response.status).to eq(400)
|
114
|
-
expect(last_response.body).to match(/per_page is invalid/)
|
85
|
+
it 'defaults :per_page to Kaminari.config.default_per_page' do
|
86
|
+
expect(params['per_page'][:default]).to eq(::Kaminari.config.default_per_page)
|
115
87
|
end
|
116
88
|
|
117
|
-
it 'defaults :offset to
|
118
|
-
expect(params['offset'][:default]).to eq(
|
89
|
+
it 'defaults :offset to 0' do
|
90
|
+
expect(params['offset'][:default]).to eq(0)
|
119
91
|
end
|
120
92
|
end
|
93
|
+
end
|
121
94
|
|
122
|
-
|
123
|
-
|
95
|
+
describe 'custom paginated api' do
|
96
|
+
subject { Class.new(PaginatedAPI) }
|
97
|
+
let(:app) { subject }
|
98
|
+
let(:params) { subject.routes.first.params }
|
124
99
|
|
125
|
-
|
126
|
-
|
127
|
-
|
100
|
+
before do
|
101
|
+
subject.params do
|
102
|
+
use :pagination, per_page: 99, max_per_page: 999, offset: 9
|
128
103
|
end
|
104
|
+
subject.get('/') {}
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'defaults :per_page to customized value' do
|
108
|
+
expect(params['per_page'][:default]).to eq(99)
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'succeeds when :per_page is within :max_value' do
|
112
|
+
get('/', page: 1, per_page: 999)
|
113
|
+
expect(last_response.status).to eq(200)
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'ensures :per_page is within :max_value' do
|
117
|
+
get('/', page: 1, per_page: 1_000)
|
118
|
+
expect(last_response.status).to eq(400)
|
119
|
+
expect(last_response.body).to match(/per_page must be less than or equal 999/)
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'ensures :per_page is numeric' do
|
123
|
+
get('/', page: 1, per_page: 'foo')
|
124
|
+
expect(last_response.status).to eq(400)
|
125
|
+
expect(last_response.body).to match(/per_page is invalid/)
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'defaults :offset to customized value' do
|
129
|
+
expect(params['offset'][:default]).to eq(9)
|
129
130
|
end
|
130
131
|
end
|
@@ -4,18 +4,24 @@ require 'json'
|
|
4
4
|
class PaginatedAPI < Grape::API
|
5
5
|
include Grape::Kaminari
|
6
6
|
|
7
|
-
|
7
|
+
params do
|
8
|
+
use :pagination
|
9
|
+
end
|
8
10
|
get '' do
|
9
11
|
paginate(Kaminari.paginate_array((1..10).to_a))
|
10
12
|
end
|
11
13
|
|
12
|
-
|
14
|
+
params do
|
15
|
+
use :pagination, offset: false
|
16
|
+
end
|
13
17
|
get 'no-offset' do
|
14
18
|
paginate(Kaminari.paginate_array((1..10).to_a))
|
15
19
|
end
|
16
20
|
|
17
21
|
resource :sub do
|
18
|
-
|
22
|
+
params do
|
23
|
+
use :pagination, per_page: 2
|
24
|
+
end
|
19
25
|
get '/' do
|
20
26
|
paginate(Kaminari.paginate_array((1..10).to_a))
|
21
27
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-kaminari
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tymon Tobolski
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-08
|
12
|
+
date: 2020-10-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: grape
|
@@ -125,6 +125,7 @@ files:
|
|
125
125
|
- ".gitignore"
|
126
126
|
- ".rubocop.yml"
|
127
127
|
- ".travis.yml"
|
128
|
+
- CHANGELOG.md
|
128
129
|
- Gemfile
|
129
130
|
- Gemfile.lock
|
130
131
|
- LICENSE.txt
|