File tree Expand file tree Collapse file tree 13 files changed +26
-41
lines changed
Expand file tree Collapse file tree 13 files changed +26
-41
lines changed Original file line number Diff line number Diff line change @@ -3,5 +3,8 @@ source 'https://rubygems.org'
33# Specify your gem's dependencies in api_pagination.gemspec
44gemspec
55
6+ gem 'kaminari' , require : false
7+ gem 'will_paginate' , require : false
8+
69gem 'rake' , require : false
710gem 'coveralls' , require : false
Original file line number Diff line number Diff line change @@ -18,5 +18,6 @@ Gem::Specification.new do |s|
1818
1919 s . add_development_dependency 'rspec'
2020 s . add_development_dependency 'grape'
21+ s . add_development_dependency 'railties' , '>= 3.0.0'
2122 s . add_development_dependency 'actionpack' , '>= 3.0.0'
2223end
Original file line number Diff line number Diff line change @@ -40,5 +40,3 @@ def total_from(collection)
4040 end
4141 end
4242end
43-
44- ApiPagination ::Hooks . init
Original file line number Diff line number Diff line change @@ -45,3 +45,5 @@ def last_page?() !next_page end
4545 end
4646 end
4747end
48+
49+ ApiPagination ::Hooks . init
Original file line number Diff line number Diff line change 1010 let ( :total ) { last_response . headers [ 'Total' ] . to_i }
1111
1212 context 'without enough items to give more than one page' do
13- before { get :numbers , :count => 20 }
13+ before { get :numbers , :count => 10 }
1414
1515 it 'should not paginate' do
1616 expect ( last_response . headers . keys ) . not_to include ( 'Link' )
1717 end
1818
1919 it 'should give a Total header' do
20- expect ( total ) . to eq ( 20 )
20+ expect ( total ) . to eq ( 10 )
2121 end
2222 end
2323
3535 end
3636
3737 context 'when on the last page' do
38- before { get :numbers , :count => 100 , :page => 4 }
38+ before { get :numbers , :count => 100 , :page => 10 }
3939
4040 it_behaves_like 'an endpoint with a last page'
4141 end
Original file line number Diff line number Diff line change 1212 let ( :total ) { response . headers [ 'Total' ] . to_i }
1313
1414 context 'without enough items to give more than one page' do
15- before { get :index , :count => 20 }
15+ before { get :index , :count => 10 }
16+
1617 it 'should not paginate' do
1718 expect ( response . headers . keys ) . not_to include ( 'Link' )
1819 end
1920
2021 it 'should give a Total header' do
21- expect ( total ) . to eq ( 20 )
22+ expect ( total ) . to eq ( 10 )
2223 end
2324 end
2425
3637 end
3738
3839 context 'when on the last page' do
39- before { get :index , :count => 100 , :page => 4 }
40+ before { get :index , :count => 100 , :page => 10 }
4041
4142 it_behaves_like 'an endpoint with a last page'
4243 end
Original file line number Diff line number Diff line change 55require 'support/numbers_api'
66require 'api-pagination'
77
8- # Quacks like Kaminari and will_paginate
9- PaginatedSet = Struct . new ( :current_page , :per_page , :total_count ) do
10- def total_pages
11- total_count . zero? ? 1 : ( total_count . to_f / per_page ) . ceil
12- end
13-
14- def first_page? ( ) current_page == 1 end
15- def last_page? ( ) current_page == total_pages end
16-
17- def page ( page )
18- current_page = page
19- self
20- end
21-
22- def per ( per )
23- per_page = per
24- self
25- end
26-
27- def paginate ( options = { } )
28- page ( options [ :page ] ) . per ( options [ :per_page ] )
29- end
30-
31- alias :total_entries :total_count
32- end
33-
348if ENV [ 'PAGINATOR' ]
359 ApiPagination . instance_variable_set ( :@paginator , ENV [ 'PAGINATOR' ] . to_sym )
3610else
3711 warn 'No PAGINATOR set. Defaulting to kaminari. To test against will_paginate, run `PAGINATOR=will_paginate bundle exec rspec`'
3812 ApiPagination . instance_variable_set ( :@paginator , :kaminari )
3913end
4014
15+ if ApiPagination . paginator == :kaminari
16+ Kaminari ::Hooks . init
17+ elsif ApiPagination . paginator == :will_paginate
18+ require 'will_paginate/array'
19+ end
20+
4121RSpec . configure do |config |
4222 config . include Rack ::Test ::Methods
4323 config . include ControllerExampleGroup , :type => :controller
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ class NumbersAPI < Grape::API
55 format :json
66
77 desc 'Return some paginated set of numbers'
8- paginate :per_page => 25
8+ paginate :per_page => 10
99 params do
1010 requires :count , :type => Integer
1111 optional :with_headers , :default => false , :type => Boolean
@@ -18,6 +18,6 @@ class NumbersAPI < Grape::API
1818 header 'Link' , %(<#{ url } ?#{ query . to_query } >; rel="without")
1919 end
2020
21- paginate PaginatedSet . new ( params [ :page ] , params [ :per_page ] , params [ : count] )
21+ paginate ( 1 .. params [ :count ] ) . to_a
2222 end
2323end
Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ def index
5757 headers [ 'Link' ] = %(<#{ numbers_url } ?#{ query . to_param } >; rel="without")
5858 end
5959
60- @numbers = PaginatedSet . new ( page , 25 , total )
60+ @numbers = ( 1 .. total ) . to_a
6161 render :json => @numbers
6262 end
6363end
Original file line number Diff line number Diff line change 55
66 it 'should contain pagination Links' do
77 expect ( links ) . to include ( '<http://example.org/numbers?count=30&page=2&with_headers=true>; rel="next"' )
8- expect ( links ) . to include ( '<http://example.org/numbers?count=30&page=2 &with_headers=true>; rel="last"' )
8+ expect ( links ) . to include ( '<http://example.org/numbers?count=30&page=3 &with_headers=true>; rel="last"' )
99 end
1010
1111 it 'should give a Total header' do
You can’t perform that action at this time.
0 commit comments