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'
3
3
# Specify your gem's dependencies in api_pagination.gemspec
4
4
gemspec
5
5
6
+ gem 'kaminari' , require : false
7
+ gem 'will_paginate' , require : false
8
+
6
9
gem 'rake' , require : false
7
10
gem 'coveralls' , require : false
Original file line number Diff line number Diff line change @@ -18,5 +18,6 @@ Gem::Specification.new do |s|
18
18
19
19
s . add_development_dependency 'rspec'
20
20
s . add_development_dependency 'grape'
21
+ s . add_development_dependency 'railties' , '>= 3.0.0'
21
22
s . add_development_dependency 'actionpack' , '>= 3.0.0'
22
23
end
Original file line number Diff line number Diff line change @@ -40,5 +40,3 @@ def total_from(collection)
40
40
end
41
41
end
42
42
end
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
45
45
end
46
46
end
47
47
end
48
+
49
+ ApiPagination ::Hooks . init
Original file line number Diff line number Diff line change 10
10
let ( :total ) { last_response . headers [ 'Total' ] . to_i }
11
11
12
12
context 'without enough items to give more than one page' do
13
- before { get :numbers , :count => 20 }
13
+ before { get :numbers , :count => 10 }
14
14
15
15
it 'should not paginate' do
16
16
expect ( last_response . headers . keys ) . not_to include ( 'Link' )
17
17
end
18
18
19
19
it 'should give a Total header' do
20
- expect ( total ) . to eq ( 20 )
20
+ expect ( total ) . to eq ( 10 )
21
21
end
22
22
end
23
23
35
35
end
36
36
37
37
context 'when on the last page' do
38
- before { get :numbers , :count => 100 , :page => 4 }
38
+ before { get :numbers , :count => 100 , :page => 10 }
39
39
40
40
it_behaves_like 'an endpoint with a last page'
41
41
end
Original file line number Diff line number Diff line change 12
12
let ( :total ) { response . headers [ 'Total' ] . to_i }
13
13
14
14
context 'without enough items to give more than one page' do
15
- before { get :index , :count => 20 }
15
+ before { get :index , :count => 10 }
16
+
16
17
it 'should not paginate' do
17
18
expect ( response . headers . keys ) . not_to include ( 'Link' )
18
19
end
19
20
20
21
it 'should give a Total header' do
21
- expect ( total ) . to eq ( 20 )
22
+ expect ( total ) . to eq ( 10 )
22
23
end
23
24
end
24
25
36
37
end
37
38
38
39
context 'when on the last page' do
39
- before { get :index , :count => 100 , :page => 4 }
40
+ before { get :index , :count => 100 , :page => 10 }
40
41
41
42
it_behaves_like 'an endpoint with a last page'
42
43
end
Original file line number Diff line number Diff line change 5
5
require 'support/numbers_api'
6
6
require 'api-pagination'
7
7
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
-
34
8
if ENV [ 'PAGINATOR' ]
35
9
ApiPagination . instance_variable_set ( :@paginator , ENV [ 'PAGINATOR' ] . to_sym )
36
10
else
37
11
warn 'No PAGINATOR set. Defaulting to kaminari. To test against will_paginate, run `PAGINATOR=will_paginate bundle exec rspec`'
38
12
ApiPagination . instance_variable_set ( :@paginator , :kaminari )
39
13
end
40
14
15
+ if ApiPagination . paginator == :kaminari
16
+ Kaminari ::Hooks . init
17
+ elsif ApiPagination . paginator == :will_paginate
18
+ require 'will_paginate/array'
19
+ end
20
+
41
21
RSpec . configure do |config |
42
22
config . include Rack ::Test ::Methods
43
23
config . include ControllerExampleGroup , :type => :controller
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ class NumbersAPI < Grape::API
5
5
format :json
6
6
7
7
desc 'Return some paginated set of numbers'
8
- paginate :per_page => 25
8
+ paginate :per_page => 10
9
9
params do
10
10
requires :count , :type => Integer
11
11
optional :with_headers , :default => false , :type => Boolean
@@ -18,6 +18,6 @@ class NumbersAPI < Grape::API
18
18
header 'Link' , %(<#{ url } ?#{ query . to_query } >; rel="without")
19
19
end
20
20
21
- paginate PaginatedSet . new ( params [ :page ] , params [ :per_page ] , params [ : count] )
21
+ paginate ( 1 .. params [ :count ] ) . to_a
22
22
end
23
23
end
Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ def index
57
57
headers [ 'Link' ] = %(<#{ numbers_url } ?#{ query . to_param } >; rel="without")
58
58
end
59
59
60
- @numbers = PaginatedSet . new ( page , 25 , total )
60
+ @numbers = ( 1 .. total ) . to_a
61
61
render :json => @numbers
62
62
end
63
63
end
Original file line number Diff line number Diff line change 5
5
6
6
it 'should contain pagination Links' do
7
7
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"' )
9
9
end
10
10
11
11
it 'should give a Total header' do
Original file line number Diff line number Diff line change 8
8
end
9
9
10
10
it 'should give a link with rel "last"' do
11
- expect ( links ) . to include ( '<http://example.org/numbers?count=100&page=4 >; rel="last"' )
11
+ expect ( links ) . to include ( '<http://example.org/numbers?count=100&page=10 >; rel="last"' )
12
12
end
13
13
14
14
it 'should give a link with rel "next"' do
Original file line number Diff line number Diff line change 12
12
end
13
13
14
14
it 'should give a link with rel "prev"' do
15
- expect ( links ) . to include ( '<http://example.org/numbers?count=100&page=3 >; rel="prev"' )
15
+ expect ( links ) . to include ( '<http://example.org/numbers?count=100&page=9 >; rel="prev"' )
16
16
end
17
17
18
18
it 'should give a Total header' do
Original file line number Diff line number Diff line change 1
1
shared_examples 'an endpoint with a middle page' do
2
2
it 'should give all pagination links' do
3
3
expect ( links ) . to include ( '<http://example.org/numbers?count=100&page=1>; rel="first"' )
4
- expect ( links ) . to include ( '<http://example.org/numbers?count=100&page=4 >; rel="last"' )
4
+ expect ( links ) . to include ( '<http://example.org/numbers?count=100&page=10 >; rel="last"' )
5
5
expect ( links ) . to include ( '<http://example.org/numbers?count=100&page=3>; rel="next"' )
6
6
expect ( links ) . to include ( '<http://example.org/numbers?count=100&page=1>; rel="prev"' )
7
7
end
You can’t perform that action at this time.
0 commit comments