Skip to content

Commit

Permalink
Grape v1.5 fixes + shared pagination parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dim committed Oct 8, 2020
1 parent 00f789c commit 7861727
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 128 deletions.
14 changes: 12 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
### 0.4.0 (Next)
### 0.5.0 (Next)

#### Features

* Your contribution here.
* [#56](https://github.com/bsm/grape-kaminari/pull/56): Introduce a CHANGELOG - [@dim](https://github.com/dim).

#### Fixes

* Your contribution here.

### 0.4.0

#### Features

* [#57](https://github.com/bsm/grape-kaminari/pull/57): Introduce new params based API - [@dim](https://github.com/dim).
* [#56](https://github.com/bsm/grape-kaminari/pull/56): Introduce a CHANGELOG - [@dim](https://github.com/dim).

#### Fixes

* [#57](https://github.com/bsm/grape-kaminari/pull/57): Fix issues related to Grape v1.5 release - [@dim](https://github.com/dim).

### 0.3.0 (2020/08/10)

* [#54](https://github.com/bsm/grape-kaminari/pull/54): Inherit paginate helper - [@dim](https://github.com/dim).
Expand Down
26 changes: 13 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
PATH
remote: .
specs:
grape-kaminari (0.3.0)
grape-kaminari (0.4.0)
grape (>= 1.0, != 1.4.0)
kaminari-grape

GEM
remote: https://rubygems.org/
specs:
activesupport (6.0.3.2)
activesupport (6.0.3.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
Expand All @@ -29,7 +29,7 @@ GEM
concurrent-ruby (~> 1.0)
dry-equalizer (0.3.0)
dry-inflector (0.2.0)
dry-logic (1.0.6)
dry-logic (1.0.8)
concurrent-ruby (~> 1.0)
dry-core (~> 0.2)
dry-equalizer (~> 0.2)
Expand All @@ -40,7 +40,7 @@ GEM
dry-equalizer (~> 0.3)
dry-inflector (~> 0.1, >= 0.1.2)
dry-logic (~> 1.0, >= 1.0.2)
grape (1.3.3)
grape (1.5.0)
activesupport
builder
dry-types (>= 1.1)
Expand All @@ -53,13 +53,13 @@ GEM
kaminari-grape (1.0.1)
grape
kaminari-core (~> 1.0)
minitest (5.14.1)
minitest (5.14.2)
mustermann (1.1.1)
ruby2_keywords (~> 0.0.1)
mustermann-grape (1.0.1)
mustermann (>= 1.0.0)
parallel (1.19.2)
parser (2.7.1.4)
parser (2.7.2.0)
ast (~> 2.4.1)
rack (2.2.3)
rack-accept (0.4.5)
Expand All @@ -68,13 +68,13 @@ GEM
rack (>= 1.0, < 3)
rainbow (3.0.0)
rake (13.0.1)
regexp_parser (1.7.1)
regexp_parser (1.8.1)
rexml (3.2.4)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.9.0)
rspec-core (3.9.2)
rspec-core (3.9.3)
rspec-support (~> 3.9.3)
rspec-expectations (3.9.2)
diff-lcs (>= 1.2.0, < 2.0)
Expand All @@ -83,17 +83,17 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.3)
rubocop (0.89.0)
rubocop (0.92.0)
parallel (~> 1.10)
parser (>= 2.7.1.1)
parser (>= 2.7.1.5)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.7)
rexml
rubocop-ast (>= 0.1.0, < 1.0)
rubocop-ast (>= 0.5.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (0.3.0)
parser (>= 2.7.1.4)
rubocop-ast (0.7.1)
parser (>= 2.7.1.5)
ruby-progressbar (1.10.1)
ruby2_keywords (0.0.2)
thread_safe (0.3.6)
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class MyApi < Grape::API
resource :posts do
desc 'Return a list of posts.'

# Annotate action with `paginate`.
# This will add three optional params: page, per_page, and offset
#
# You can optionally overwrite the default :per_page setting (10)
Expand All @@ -45,8 +44,9 @@ class MyApi < Grape::API
# You can disable the offset parameter from appearing in the API
# documentation by setting it to false.
#
paginate per_page: 20, max_per_page: 30, offset: 5

params do
use :pagination, per_page: 20, max_per_page: 30, offset: 5
end
get do
posts = Post.where(...)

Expand Down
40 changes: 24 additions & 16 deletions lib/grape/kaminari.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,30 @@ module Kaminari

included do
helpers HelperMethods
base_instance.extend DSLMethods
end

module HelperMethods # :nodoc:
extend Grape::API::Helpers

params :pagination do |opts = {}|
opts.reverse_merge!(
per_page: ::Kaminari.config.default_per_page || 10,
max_per_page: ::Kaminari.config.max_per_page,
offset: 0,
)

optional :page, type: Integer, default: 1,
desc: 'Page offset to fetch.'
optional :per_page, type: Integer, default: opts[:per_page],
desc: 'Number of results to return per page.',
max_value: opts[:max_per_page]

if opts[:offset].is_a?(Integer)
optional :offset, type: Integer, default: opts[:offset],
desc: 'Pad a number of results.'
end
end

def paginate(collection)
collection.page(params[:page].to_i)
.per(params[:per_page].to_i)
Expand All @@ -30,24 +50,12 @@ def paginate(collection)
end

module DSLMethods # :nodoc:
def paginate(**options)
options.reverse_merge!(
per_page: ::Kaminari.config.default_per_page || 10,
max_per_page: ::Kaminari.config.max_per_page,
offset: 0,
)
def paginate(opts = {})
params do
optional :page, type: Integer, default: 1,
desc: 'Page offset to fetch.'
optional :per_page, type: Integer, default: options[:per_page],
desc: 'Number of results to return per page.',
max_value: options[:max_per_page]
if options[:offset].is_a? Numeric
optional :offset, type: Integer, default: options[:offset],
desc: 'Pad a number of results.'
end
use(:pagination, opts)
end
end
end
Grape::API::Instance.extend(DSLMethods)
end
end
2 changes: 1 addition & 1 deletion lib/grape/kaminari/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Grape
module Kaminari
VERSION = '0.3.0'.freeze
VERSION = '0.4.0'.freeze
end
end
Loading

0 comments on commit 7861727

Please sign in to comment.