Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to pagy 9 #23

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 0.9.0

- Pin to Pagy 9.x.
dim marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just say:

- BREAKING: Use Pagy 9.x. This contains breaking changes, see [CHANGELOG.md](https://github.com/ddnexus/pagy/blob/master/CHANGELOG.md#version-900)
- BREAKING: Rename `items` parameter to `limit` to align with latest Pagy naming convention.

Remove the rest, it's too verbose and doesn't tell much to the actual user :)


### 0.5.0

- Pin to Pagy 5.x.
Expand Down
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
PATH
remote: .
specs:
grape-pagy (0.6.0)
grape (>= 1.5)
pagy (>= 6.0)
grape-pagy (0.9.0)
grape (>= 2.0)
pagy (>= 9.0)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -32,8 +32,8 @@ GEM
dry-inflector (~> 1.0)
dry-logic (~> 1.4)
zeitwerk (~> 2.6)
grape (1.7.0)
activesupport
grape (2.0.0)
activesupport (>= 5)
builder
dry-types (>= 1.1)
mustermann-grape (~> 1.0.0)
Expand All @@ -48,7 +48,7 @@ GEM
ruby2_keywords (~> 0.0.1)
mustermann-grape (1.0.2)
mustermann (>= 1.0.0)
pagy (6.0.4)
pagy (9.0.8)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MyApi < Grape::API
resource :posts do
desc 'Return a list of posts.'
params do
# This will add two optional params: :page and :items.
# This will add two optional params: :page and :limit.
use :pagy
end
get do
Expand All @@ -49,9 +49,9 @@ class MyApi < Grape::API
params do
# Override defaults by setting Pagy::DEFAULT or by passing options.
use :pagy,
items_param: :per_page, # Accept per_page=N param to limit items.
items: 2, # If per_page param is blank, default to 2.
max_items: 10 # Restrict per_page to maximum 10.
limit_param: :per_page, # Accept per_page=N param to limit items.
limit: 2, # If per_page param is blank, default to 2.
limit_max: 10 # Restrict per_page to maximum 10.
end
get do
words = %w[this is a plain array of words]
Expand All @@ -64,7 +64,7 @@ end
Example request:

```shell
curl -si http://localhost:8080/api/posts?page=3&items=5
curl -si http://localhost:8080/api/posts?page=3&limit=5
```

The response will be paginated and also will include the following headers:
Expand All @@ -74,7 +74,7 @@ Current-Page: 3
Page-Items: 5
Total-Count: 22
Total-Pages: 5
Link: <http://localhost:8080/api/posts?page=1&items=5>; rel="first", <http://localhost:8080/api/posts?page=4&items=5>; rel="next", <http://localhost:8080/api/posts?page=2&items=5>; rel="prev", <http://localhost:8080/api/posts?page=5&items=5>; rel="last"),
Link: <http://localhost:8080/api/posts?page=1&limit=5>; rel="first", <http://localhost:8080/api/posts?page=4&limit=5>; rel="next", <http://localhost:8080/api/posts?page=2&limit=5>; rel="prev", <http://localhost:8080/api/posts?page=5&limit=5>; rel="last"),
```

## Contributing
Expand Down
6 changes: 3 additions & 3 deletions grape-pagy.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
spec.name = 'grape-pagy'
spec.version = '0.6.0'
spec.version = '0.9.0'
spec.authors = ['Black Square Media']
spec.email = ['[email protected]']
spec.description = 'Pagy paginator for grape API'
Expand All @@ -12,8 +12,8 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 3.0'

spec.add_runtime_dependency 'grape', '>= 1.5'
spec.add_runtime_dependency 'pagy', '>= 6.0'
spec.add_runtime_dependency 'grape', '>= 2.0'
spec.add_runtime_dependency 'pagy', '>= 9.0'

spec.metadata['rubygems_mfa_required'] = 'true'
end
11 changes: 5 additions & 6 deletions lib/grape/pagy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'pagy/extras/array'
require 'pagy/extras/countless'
require 'pagy/extras/headers'
require 'pagy/extras/items'
require 'pagy/extras/limit'
require 'pagy/extras/overflow'

module Grape
Expand All @@ -13,7 +13,6 @@ module Pagy
include ::Pagy::Backend

def paginate(collection, using: nil, **opts, &block)
opts = pagy_countless_get_vars(nil, opts)
using ||= if collection.respond_to?(:arel_table)
:arel
elsif collection.is_a?(Array)
Expand All @@ -32,15 +31,15 @@ module Helpers
extend Grape::API::Helpers

params :pagy do |**opts|
items = opts.delete(:items) || ::Pagy::DEFAULT[:items]
limit = opts.delete(:limit) || ::Pagy::DEFAULT[:limit]
page = opts.delete(:page) || ::Pagy::DEFAULT[:page]
page_param = opts[:page_param] || ::Pagy::DEFAULT[:page_param]
items_param = opts[:items_param] || ::Pagy::DEFAULT[:items_param]
max_items = opts[:max_items] || ::Pagy::DEFAULT[:max_items]
limit_param = opts[:limit_param] || ::Pagy::DEFAULT[:limit_param]
limit_max = opts[:limit_max] || ::Pagy::DEFAULT[:limit_max]

@api.route_setting(:pagy_options, opts)
optional page_param, type: Integer, default: page, desc: 'Page offset to fetch.'
optional items_param, type: Integer, default: items, desc: "Number of items to return per page. Maximum value: #{max_items}"
optional limit_param, type: Integer, default: limit, desc: "Number of items to return per page. Maximum value: #{limit_max}"
end

# @param [Array|ActiveRecord::Relation] collection the collection or relation.
Expand Down
28 changes: 14 additions & 14 deletions spec/grape/pagy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
expect(last_response.status).to eq(200)
expect(last_response.headers).to include(
'current-page' => '1',
'link' => '<http://example.org/?page=1&items=5>; rel="first", ' \
'<http://example.org/?page=2&items=5>; rel="next", ' \
'<http://example.org/?page=3&items=5>; rel="last"',
'link' => '<http://example.org/?page=1&limit=5>; rel="first", ' \
'<http://example.org/?page=2&limit=5>; rel="next", ' \
'<http://example.org/?page=3&limit=5>; rel="last"',
'page-items' => '5',
'total-count' => '12',
'total-pages' => '3',
)
expect(last_response.body).to eq(%([1, 2, 3, 4, 5]))
end

it 'accepts page and items parameters' do
get '/?page=2&items=3'
it 'accepts page and limit parameters' do
get '/?page=2&limit=3'
expect(last_response.status).to eq(200)
expect(last_response.headers).to include(
'current-page' => '2',
Expand All @@ -32,13 +32,13 @@
expect(last_response.body).to eq(%([4, 5, 6]))
end

it 'caps items' do
get '/?items=10'
expect(last_response.headers).to include('Page-Items' => '6')
it 'caps limit' do
get '/?limit=10'
expect(last_response.headers).to include('Page-items' => '6')
expect(last_response.body).to eq(%([1, 2, 3, 4, 5, 6]))

get '/?items=3'
expect(last_response.headers).to include('Page-Items' => '3')
get '/?limit=3'
expect(last_response.headers).to include('Page-items' => '3')
expect(last_response.body).to eq(%([1, 2, 3]))
end

Expand All @@ -49,7 +49,7 @@
'current-page' => '99',
'total-pages' => '3',
)
expect(last_response.body).to eq(%([]))
expect(last_response.body).to be_blank
end

it 'does not need options' do
Expand All @@ -69,9 +69,9 @@
'current-page' => '2',
'page-items' => '3',
'link' => [
%(<http://example.org/countless?page=1&items=3>; rel="first"),
%(<http://example.org/countless?page=1&items=3>; rel="prev"),
%(<http://example.org/countless?page=3&items=3>; rel="next"),
%(<http://example.org/countless?page=1&limit=3>; rel="first"),
%(<http://example.org/countless?page=1&limit=3>; rel="prev"),
%(<http://example.org/countless?page=3&limit=3>; rel="next"),
].join(', '),
)
expect(last_response.headers).not_to include(
Expand Down
10 changes: 5 additions & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
require 'grape/pagy'
require 'rack/test'

Pagy::DEFAULT[:items] = 10
Pagy::DEFAULT[:max_items] = 20
Pagy::DEFAULT[:limit] = 10
Pagy::DEFAULT[:limit_max] = 20

class TestArray < Array
def limit(num)
Expand All @@ -21,7 +21,7 @@ class TestAPI < Grape::API
helpers Grape::Pagy::Helpers

params do
use :pagy, items: 5, max_items: 6
use :pagy, limit: 5, limit_max: 6
end
get '' do
pagy (1..12).to_a
Expand All @@ -35,15 +35,15 @@ class TestAPI < Grape::API
end

params do
use :pagy, items: 3
use :pagy, limit: 3
end
get '/countless' do
pagy TestArray.new((1..12).to_a), using: :countless
end

resource :sub do
params do
use :pagy, items_param: :per_page
use :pagy, limit_param: :per_page
end
get '/' do
pagy (1..12).to_a, count: 13
Expand Down