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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 52b012ea708cc341716fae9dc594335ef2b2de57498dca9643e89cb38caf97e2
4
- data.tar.gz: 264bcfea9ce1e8e7310c12e2e68e9e70a5482d6a7d2fd6f506c5d8ed9bac6715
3
+ metadata.gz: ae85f7bb6472d19bcbedad2d0e03e0960a75615960819251b1a35ed793a0cb65
4
+ data.tar.gz: 3fa36e9bcd909ae16e54c44c0c9b65499fa8f4ad7dbf94c75b8d7192897932b5
5
5
  SHA512:
6
- metadata.gz: ac1d11277bb770f7a56f9b88d966ba2e948d593fec46f28140a1b7961c2a14df95b2c7e48f48a59a4dafa8008c4498e89ea7d1050956398a54a9a1816bdfd9fc
7
- data.tar.gz: 8e23241b263e2fa2164674847b915517d93831ede7da6a49add2b4f5252ddb2a3621d215922799649335d79459a2727c8372fdfd563e8a0b64f4300fd29f6a26
6
+ metadata.gz: 74772e142b4277e609bdca9ce59e5a3de4ed35e940fbbf3a8a0dd79122bdacf9327acf832177b9ce92cb074a81be34c1fae30ea9a2871e36800ba6d19fd68905
7
+ data.tar.gz: 65407c449ede7a7cfa7a3e1d7428530bc761b5c776a30a0db90baeacd234596e7b5fc933e34d50aa8cbf2011359a65a68bd9e9f29819c637edb1986f2a66a36e
@@ -1,3 +1,7 @@
1
+ ### 0.2.0
2
+
3
+ * Improved, simplified and stabilised API [#1](https://github.com/bsm/grape-pagy/pull/1).
4
+
1
5
  ### 0.1.0
2
6
 
3
- * Initial release
7
+ * Initial release.
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- grape-pagy (0.1.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.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
- # The parameter names can be globally configured by adjusting
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
- paginate(posts)
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 the number items returned, defaults to Pagy::VARS[:items].
53
- use :pagination, items: 2
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[an array of words]
57
- # This supports array as well as relations.
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = 'grape-pagy'
3
- spec.version = '0.1.0'
3
+ spec.version = '0.2.0'
4
4
  spec.authors = ['Black Square Media']
5
5
  spec.email = ['info@blacksquaremedia.com']
6
6
  spec.description = 'Pagy paginator for grape API'
@@ -8,7 +8,7 @@ require 'pagy/extras/overflow'
8
8
 
9
9
  module Grape
10
10
  module Pagy
11
- Pager = Struct.new :request, :params do
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 :pagination do |items: ::Pagy::VARS[:items]|
34
- optional ::Pagy::VARS[:page_param], type: Integer, default: 1, desc: 'Page offset to fetch.'
35
- optional ::Pagy::VARS[:items_param], type: Integer, default: items, desc: 'Number of items to return per page.'
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
- def paginate(collection, via: nil, **opts)
39
- Pager.new(request, params).paginate(collection, via: via, **opts) do |key, value|
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
@@ -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' => '12',
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?items=20'
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' => '12',
69
+ 'Total-Count' => '13',
70
70
  'Total-Pages' => '1',
71
71
  )
72
72
  end
@@ -11,18 +11,18 @@ class TestAPI < Grape::API
11
11
  helpers Grape::Pagy::Helpers
12
12
 
13
13
  params do
14
- use :pagination, items: 5
14
+ use :pagy, items: 5, max_items: 6
15
15
  end
16
16
  get '' do
17
- paginate (1..12).to_a, max_items: 6
17
+ pagy (1..12).to_a
18
18
  end
19
19
 
20
20
  resource :sub do
21
21
  params do
22
- use :pagination
22
+ use :pagy, items_param: :per_page
23
23
  end
24
24
  get '/' do
25
- paginate (1..12).to_a
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.1.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-07 00:00:00.000000000 Z
11
+ date: 2020-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: grape