Skip to content

Commit c3f408e

Browse files
committed
Change Spec suite to actually paginate over an Array
Signed-off-by: David Celis <[email protected]>
1 parent 8b3ef20 commit c3f408e

File tree

13 files changed

+26
-41
lines changed

13 files changed

+26
-41
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@ source 'https://rubygems.org'
33
# Specify your gem's dependencies in api_pagination.gemspec
44
gemspec
55

6+
gem 'kaminari', require: false
7+
gem 'will_paginate', require: false
8+
69
gem 'rake', require: false
710
gem 'coveralls', require: false

api-pagination.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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'
2223
end

lib/api-pagination.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,3 @@ def total_from(collection)
4040
end
4141
end
4242
end
43-
44-
ApiPagination::Hooks.init

lib/api-pagination/hooks.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ def last_page?() !next_page end
4545
end
4646
end
4747
end
48+
49+
ApiPagination::Hooks.init

spec/grape_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
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

@@ -35,7 +35,7 @@
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

spec/rails_spec.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
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

@@ -36,7 +37,7 @@
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

spec/spec_helper.rb

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,19 @@
55
require 'support/numbers_api'
66
require '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-
348
if ENV['PAGINATOR']
359
ApiPagination.instance_variable_set(:@paginator, ENV['PAGINATOR'].to_sym)
3610
else
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)
3913
end
4014

15+
if ApiPagination.paginator == :kaminari
16+
Kaminari::Hooks.init
17+
elsif ApiPagination.paginator == :will_paginate
18+
require 'will_paginate/array'
19+
end
20+
4121
RSpec.configure do |config|
4222
config.include Rack::Test::Methods
4323
config.include ControllerExampleGroup, :type => :controller

spec/support/numbers_api.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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
2323
end

spec/support/numbers_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
6363
end

spec/support/shared_examples/existing_headers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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

spec/support/shared_examples/first_page.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
end
99

1010
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"')
1212
end
1313

1414
it 'should give a link with rel "next"' do

spec/support/shared_examples/last_page.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
end
1313

1414
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"')
1616
end
1717

1818
it 'should give a Total header' do

spec/support/shared_examples/middle_page.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
shared_examples 'an endpoint with a middle page' do
22
it 'should give all pagination links' do
33
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"')
55
expect(links).to include('<http://example.org/numbers?count=100&page=3>; rel="next"')
66
expect(links).to include('<http://example.org/numbers?count=100&page=1>; rel="prev"')
77
end

0 commit comments

Comments
 (0)