From 272c4c2afc21ebaf0e13d1bd0c39b0220503153a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Utka=C5=82a?= Date: Fri, 6 Sep 2024 07:40:43 +0200 Subject: [PATCH 1/4] Upgrade to pagy 9 --- CHANGELOG.md | 4 ++++ Gemfile.lock | 12 ++++++------ README.md | 12 ++++++------ grape-pagy.gemspec | 6 +++--- lib/grape/pagy.rb | 11 +++++------ spec/grape/pagy_spec.rb | 28 ++++++++++++++-------------- spec/spec_helper.rb | 10 +++++----- 7 files changed, 43 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88b87f6..8b05314 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +### 0.9.0 + +- Pin to Pagy 9.x. + ### 0.5.0 - Pin to Pagy 5.x. diff --git a/Gemfile.lock b/Gemfile.lock index 471df7f..7c6d1ea 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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/ @@ -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) @@ -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) diff --git a/README.md b/README.md index 367409b..4b5edfa 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 limit. + 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] @@ -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: @@ -74,7 +74,7 @@ Current-Page: 3 Page-Items: 5 Total-Count: 22 Total-Pages: 5 -Link: ; rel="first", ; rel="next", ; rel="prev", ; rel="last"), +Link: ; rel="first", ; rel="next", ; rel="prev", ; rel="last"), ``` ## Contributing diff --git a/grape-pagy.gemspec b/grape-pagy.gemspec index 83ec843..539a55a 100644 --- a/grape-pagy.gemspec +++ b/grape-pagy.gemspec @@ -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 = ['info@blacksquaremedia.com'] spec.description = 'Pagy paginator for grape API' @@ -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 diff --git a/lib/grape/pagy.rb b/lib/grape/pagy.rb index 3aa50cb..89d4d9e 100644 --- a/lib/grape/pagy.rb +++ b/lib/grape/pagy.rb @@ -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 @@ -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) @@ -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. diff --git a/spec/grape/pagy_spec.rb b/spec/grape/pagy_spec.rb index 3b4a5d3..607d181 100644 --- a/spec/grape/pagy_spec.rb +++ b/spec/grape/pagy_spec.rb @@ -10,9 +10,9 @@ expect(last_response.status).to eq(200) expect(last_response.headers).to include( 'current-page' => '1', - 'link' => '; rel="first", ' \ - '; rel="next", ' \ - '; rel="last"', + 'link' => '; rel="first", ' \ + '; rel="next", ' \ + '; rel="last"', 'page-items' => '5', 'total-count' => '12', 'total-pages' => '3', @@ -20,8 +20,8 @@ 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', @@ -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 @@ -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 @@ -69,9 +69,9 @@ 'current-page' => '2', 'page-items' => '3', 'link' => [ - %(; rel="first"), - %(; rel="prev"), - %(; rel="next"), + %(; rel="first"), + %(; rel="prev"), + %(; rel="next"), ].join(', '), ) expect(last_response.headers).not_to include( diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7ca1ed1..0da4aaf 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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) @@ -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 @@ -35,7 +35,7 @@ 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 @@ -43,7 +43,7 @@ class TestAPI < Grape::API 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 From 1315eaf8fe2748a49c0c3d5e4c084eb0763e1f06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Utka=C5=82a?= Date: Fri, 6 Sep 2024 07:44:51 +0200 Subject: [PATCH 2/4] Fix typo in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4b5edfa..8879077 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ class MyApi < Grape::API params do # Override defaults by setting Pagy::DEFAULT or by passing options. use :pagy, - limit_param: :per_page, # Accept per_page=N param to limit limit. + 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 From 8e0342226450d436bb382c19510927ff3b8a33d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Utka=C5=82a?= Date: Fri, 6 Sep 2024 13:29:13 +0200 Subject: [PATCH 3/4] Update CHANGELOG.md --- CHANGELOG.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b05314..929c838 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,19 @@ - Pin to Pagy 9.x. +Breaking changes based on [CHANGELOG.md](https://github.com/ddnexus/pagy/blob/master/CHANGELOG.md#version-900): +- BREAKING: Rename :max_limit > :limit_max +- BREAKING: Rename variable, param, accessor, extra and helper "items" to "limit" +- BREAKING: Transform the vars positional hash argument in keyword arguments (double splat); internal renaming of setup/assign methods +- Refactor pagy_get_vars in various backend extras +- BREAKING: Refactor the fragment url +- BREAKING: Refactor the anchor_string system +- BREAKING: Drop the support for 8+ deprecations + +Possibly breaking overrides: + +The internal **Pagy protected methods have been renamed and refactored**. If you use custom Pagy classes, you may need to search into the code. + ### 0.5.0 - Pin to Pagy 5.x. From 7cd54d9fb7bb4463c28cd9615b96eb4c82b00beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Utka=C5=82a?= Date: Wed, 11 Sep 2024 13:05:53 +0200 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 929c838..6dced18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,18 +2,9 @@ - Pin to Pagy 9.x. -Breaking changes based on [CHANGELOG.md](https://github.com/ddnexus/pagy/blob/master/CHANGELOG.md#version-900): -- BREAKING: Rename :max_limit > :limit_max -- BREAKING: Rename variable, param, accessor, extra and helper "items" to "limit" -- BREAKING: Transform the vars positional hash argument in keyword arguments (double splat); internal renaming of setup/assign methods -- Refactor pagy_get_vars in various backend extras -- BREAKING: Refactor the fragment url -- BREAKING: Refactor the anchor_string system -- BREAKING: Drop the support for 8+ deprecations - -Possibly breaking overrides: - -The internal **Pagy protected methods have been renamed and refactored**. If you use custom Pagy classes, you may need to search into the code. +BREAKING: +- Rename `items` parameter to `limit` to align with latest Pagy naming convention. +- Check other Pagy 9.x. breaking changes -> [CHANGELOG.md](https://github.com/ddnexus/pagy/blob/master/CHANGELOG.md#version-900) ### 0.5.0