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

Inherit endpoint paginate helper #54

Merged
merged 2 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 14 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: .
specs:
grape-kaminari (0.2.1)
grape (>= 1.0)
grape-kaminari (0.3.0)
grape (>= 1.0, != 1.4.0)
kaminari-grape

GEM
Expand All @@ -16,9 +16,9 @@ GEM
zeitwerk (~> 2.2, >= 2.2.2)
ast (2.4.1)
builder (3.2.4)
concurrent-ruby (1.1.6)
diff-lcs (1.3)
dry-configurable (0.11.5)
concurrent-ruby (1.1.7)
diff-lcs (1.4.4)
dry-configurable (0.11.6)
concurrent-ruby (~> 1.0)
dry-core (~> 0.4, >= 0.4.7)
dry-equalizer (~> 0.2)
Expand Down Expand Up @@ -47,7 +47,7 @@ GEM
mustermann-grape (~> 1.0.0)
rack (>= 1.3.0)
rack-accept
i18n (1.8.3)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
kaminari-core (1.2.1)
kaminari-grape (1.0.1)
Expand All @@ -59,8 +59,8 @@ GEM
mustermann-grape (1.0.1)
mustermann (>= 1.0.0)
parallel (1.19.2)
parser (2.7.1.3)
ast (~> 2.4.0)
parser (2.7.1.4)
ast (~> 2.4.1)
rack (2.2.3)
rack-accept (0.4.5)
rack (>= 0.4)
Expand All @@ -83,24 +83,24 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.3)
rubocop (0.85.1)
rubocop (0.89.0)
parallel (~> 1.10)
parser (>= 2.7.0.1)
parser (>= 2.7.1.1)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.7)
rexml
rubocop-ast (>= 0.0.3)
rubocop-ast (>= 0.1.0, < 1.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (0.0.3)
parser (>= 2.7.0.1)
rubocop-ast (0.3.0)
parser (>= 2.7.1.4)
ruby-progressbar (1.10.1)
ruby2_keywords (0.0.2)
thread_safe (0.3.6)
tzinfo (1.2.7)
thread_safe (~> 0.1)
unicode-display_width (1.7.0)
zeitwerk (2.3.0)
zeitwerk (2.4.0)

PLATFORMS
ruby
Expand Down
2 changes: 1 addition & 1 deletion grape-kaminari.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 2.5'

spec.add_runtime_dependency 'grape', '>= 1.0'
spec.add_runtime_dependency 'grape', '>= 1.0', '!= 1.4.0'
spec.add_runtime_dependency 'kaminari-grape'

spec.add_development_dependency 'bundler'
Expand Down
14 changes: 8 additions & 6 deletions lib/grape/kaminari.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

module Grape
module Kaminari
extend ActiveSupport::Concern

included do
helpers HelperMethods
base_instance.extend DSLMethods
end

module HelperMethods # :nodoc:
def paginate(collection)
collection.page(params[:page].to_i)
Expand All @@ -22,7 +29,7 @@ def paginate(collection)
end
end

module ClassMethods # :nodoc:
module DSLMethods # :nodoc:
def paginate(**options)
options.reverse_merge!(
per_page: ::Kaminari.config.default_per_page || 10,
Expand All @@ -42,10 +49,5 @@ def paginate(**options)
end
end
end

def self.included(base)
base.helpers HelperMethods
base.extend ClassMethods
end
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.2.1'.freeze
VERSION = '0.3.0'.freeze
end
end
16 changes: 13 additions & 3 deletions spec/paginate_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ class PaginatedAPI < Grape::API
get 'no-offset' do
paginate(Kaminari.paginate_array((1..10).to_a))
end

resource :sub do
paginate per_page: 2
get '/' do
paginate(Kaminari.paginate_array((1..10).to_a))
end
end
end

describe Grape::Kaminari do
subject { PaginatedAPI.new }
def app
subject
end
let(:app) { subject }
let(:json) { JSON.parse(last_response.body) }
let(:header) { last_response.header }

Expand Down Expand Up @@ -51,5 +56,10 @@ def app
expect(header['X-Prev-Page']).to eq '2'
expect(header['X-Offset']).to eq '1'
end

it 'can be inherited' do
get '/sub', page: 1
expect(json).to eq [1, 2]
end
end
end