grape-pagy 0.1.0 → 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/CHANGELOG.md +5 -1
- data/Gemfile.lock +2 -2
- data/README.md +10 -11
- data/grape-pagy.gemspec +1 -1
- data/lib/grape/pagy.rb +14 -6
- data/spec/grape/pagy_spec.rb +3 -3
- data/spec/spec_helper.rb +4 -4
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ae85f7bb6472d19bcbedad2d0e03e0960a75615960819251b1a35ed793a0cb65
|
4
|
+
data.tar.gz: 3fa36e9bcd909ae16e54c44c0c9b65499fa8f4ad7dbf94c75b8d7192897932b5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 74772e142b4277e609bdca9ce59e5a3de4ed35e940fbbf3a8a0dd79122bdacf9327acf832177b9ce92cb074a81be34c1fae30ea9a2871e36800ba6d19fd68905
|
7
|
+
data.tar.gz: 65407c449ede7a7cfa7a3e1d7428530bc761b5c776a30a0db90baeacd234596e7b5fc933e34d50aa8cbf2011359a65a68bd9e9f29819c637edb1986f2a66a36e
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
grape-pagy (0.
|
4
|
+
grape-pagy (0.2.0)
|
5
5
|
grape (>= 1.5)
|
6
6
|
pagy
|
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)
|
data/README.md
CHANGED
@@ -36,27 +36,26 @@ class MyApi < Grape::API
|
|
36
36
|
desc 'Return a list of posts.'
|
37
37
|
params do
|
38
38
|
# This will add two optional params: :page and :items.
|
39
|
-
|
40
|
-
# global Pagy::VARS[:page_param] and Pagy::VARS[:item_param]
|
41
|
-
# settings.
|
42
|
-
use :pagination
|
39
|
+
use :pagy
|
43
40
|
end
|
44
41
|
get do
|
45
42
|
posts = Post.all.order(created_at: :desc)
|
46
|
-
|
43
|
+
pagy(posts)
|
47
44
|
end
|
48
45
|
end
|
49
46
|
|
50
47
|
resource :strings do
|
48
|
+
desc 'Supports arrays as well as relations.'
|
51
49
|
params do
|
52
|
-
# Override
|
53
|
-
use :
|
50
|
+
# Override defaults by setting Pagy::VARS or by passing options.
|
51
|
+
use :pagy,
|
52
|
+
items_param: :per_page, # Accept per_page=N param to limit items.
|
53
|
+
items: 2, # If per_page param is blank, default to 2.
|
54
|
+
max_items: 10 # Restrict per_page to maximum 10.
|
54
55
|
end
|
55
56
|
get do
|
56
|
-
words = %w[
|
57
|
-
|
58
|
-
# Allows to override global Pagy::VARS settings.
|
59
|
-
paginate(words, max_items: )
|
57
|
+
words = %w[this is a plain array of words]
|
58
|
+
pagy(words)
|
60
59
|
end
|
61
60
|
end
|
62
61
|
end
|
data/grape-pagy.gemspec
CHANGED
data/lib/grape/pagy.rb
CHANGED
@@ -8,7 +8,7 @@ require 'pagy/extras/overflow'
|
|
8
8
|
|
9
9
|
module Grape
|
10
10
|
module Pagy
|
11
|
-
|
11
|
+
Wrapper = Struct.new :request, :params do
|
12
12
|
include ::Pagy::Backend
|
13
13
|
|
14
14
|
def paginate(collection, via: nil, **opts, &block)
|
@@ -30,13 +30,21 @@ module Grape
|
|
30
30
|
module Helpers
|
31
31
|
extend Grape::API::Helpers
|
32
32
|
|
33
|
-
params :
|
34
|
-
|
35
|
-
|
33
|
+
params :pagy do |items: nil, page: nil, **opts|
|
34
|
+
items ||= ::Pagy::VARS[:items]
|
35
|
+
page ||= ::Pagy::VARS[:page]
|
36
|
+
page_param = opts[:page_param] || ::Pagy::VARS[:page_param]
|
37
|
+
items_param = opts[:items_param] || ::Pagy::VARS[:items_param]
|
38
|
+
|
39
|
+
@api.route_setting(:pagy_options, opts)
|
40
|
+
optional page_param, type: Integer, default: page, desc: 'Page offset to fetch.'
|
41
|
+
optional items_param, type: Integer, default: items, desc: 'Number of items to return per page.'
|
36
42
|
end
|
37
43
|
|
38
|
-
|
39
|
-
|
44
|
+
# @param [Array|ActiveRecord::Relation] collection the collection or relation.
|
45
|
+
def pagy(collection, **opts)
|
46
|
+
defaults = route_setting(:pagy_options) || {}
|
47
|
+
Wrapper.new(request, params).paginate(collection, **defaults, **opts) do |key, value|
|
40
48
|
header key, value
|
41
49
|
end
|
42
50
|
end
|
data/spec/grape/pagy_spec.rb
CHANGED
@@ -56,17 +56,17 @@ describe Grape::Pagy do
|
|
56
56
|
expect(last_response.headers).to include(
|
57
57
|
'Current-Page' => '1',
|
58
58
|
'Page-Items' => '10',
|
59
|
-
'Total-Count' => '
|
59
|
+
'Total-Count' => '13',
|
60
60
|
'Total-Pages' => '2',
|
61
61
|
)
|
62
62
|
expect(last_response.body).to eq(%([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
|
63
63
|
|
64
|
-
get '/sub?
|
64
|
+
get '/sub?per_page=20'
|
65
65
|
expect(last_response.status).to eq(200)
|
66
66
|
expect(last_response.headers).to include(
|
67
67
|
'Current-Page' => '1',
|
68
68
|
'Page-Items' => '20',
|
69
|
-
'Total-Count' => '
|
69
|
+
'Total-Count' => '13',
|
70
70
|
'Total-Pages' => '1',
|
71
71
|
)
|
72
72
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -11,18 +11,18 @@ class TestAPI < Grape::API
|
|
11
11
|
helpers Grape::Pagy::Helpers
|
12
12
|
|
13
13
|
params do
|
14
|
-
use :
|
14
|
+
use :pagy, items: 5, max_items: 6
|
15
15
|
end
|
16
16
|
get '' do
|
17
|
-
|
17
|
+
pagy (1..12).to_a
|
18
18
|
end
|
19
19
|
|
20
20
|
resource :sub do
|
21
21
|
params do
|
22
|
-
use :
|
22
|
+
use :pagy, items_param: :per_page
|
23
23
|
end
|
24
24
|
get '/' do
|
25
|
-
|
25
|
+
pagy (1..12).to_a, count: 13
|
26
26
|
end
|
27
27
|
end
|
28
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-pagy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Black Square Media
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|