Skip to content

Commit d7daff5

Browse files
authored
Merge pull request davidcelis#69 from trev/add-rails5-support
Add Rails 5 support
2 parents 57c7efb + 34d0395 commit d7daff5

File tree

4 files changed

+35
-24
lines changed

4 files changed

+35
-24
lines changed

lib/api-pagination/hooks.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
begin; require 'rails'; rescue LoadError; end
2-
if defined?(ActionController::Base)
3-
require 'rails/pagination'
4-
ActionController::Base.send(:include, Rails::Pagination)
5-
end
6-
72
begin; require 'rails-api'; rescue LoadError; end
8-
if defined?(ActionController::API)
3+
if defined?(Rails)
94
require 'rails/pagination'
10-
ActionController::API.send(:include, Rails::Pagination)
5+
6+
ActiveSupport.on_load(:action_controller) do
7+
ApiPagination::Hooks.rails_parent_controller.send(:include, Rails::Pagination)
8+
end
119
end
1210

1311
begin; require 'grape'; rescue LoadError; end
@@ -28,3 +26,15 @@
2826
gem 'will_paginate'
2927
WARNING
3028
end
29+
30+
module ApiPagination
31+
module Hooks
32+
def self.rails_parent_controller
33+
if Gem::Version.new(Rails.version) >= Gem::Version.new('5') || defined?(ActionController::API)
34+
ActionController::API
35+
else
36+
ActionController::Base
37+
end
38+
end
39+
end
40+
end

lib/api-pagination/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module ApiPagination
22
class Version
33
MAJOR = 4
4-
MINOR = 4
4+
MINOR = 5
55
PATCH = 0
66

77
def self.to_s

spec/rails_spec.rb

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
let(:per_page) { response.headers['Per-Page'].to_i }
1414

1515
context 'without enough items to give more than one page' do
16-
before { get :index, :count => 10 }
16+
before { get :index, params: {count: 10} }
1717

1818
it 'should not paginate' do
1919
expect(response.headers.keys).not_to include('Link')
@@ -34,34 +34,34 @@
3434
end
3535

3636
context 'with existing Link headers' do
37-
before { get :index, :count => 30, :with_headers => true }
37+
before { get :index, params: {count: 30, with_headers: true} }
3838

3939
it_behaves_like 'an endpoint with existing Link headers'
4040
end
4141

4242
context 'with enough items to paginate' do
4343
context 'when on the first page' do
44-
before { get :index, :count => 100 }
44+
before { get :index, params: {count: 100} }
4545

4646
it_behaves_like 'an endpoint with a first page'
4747
end
4848

4949
context 'when on the last page' do
50-
before { get :index, :count => 100, :page => 10 }
50+
before { get :index, params: {count: 100, page: 10} }
5151

5252
it_behaves_like 'an endpoint with a last page'
5353
end
5454

5555
context 'when somewhere comfortably in the middle' do
56-
before { get :index, :count => 100, :page => 2 }
56+
before { get :index, params: {count: 100, page: 2} }
5757

5858
it_behaves_like 'an endpoint with a middle page'
5959
end
6060
end
6161

6262
context 'providing a block' do
6363
it 'yields to the block instead of implicitly rendering' do
64-
get :index_with_custom_render, :count => 100
64+
get :index_with_custom_render, params: {count: 100}
6565

6666
json = { numbers: (1..10).map { |n| { number: n } } }.to_json
6767

@@ -75,7 +75,7 @@
7575
ApiPagination.config.per_page_header = 'X-Per-Page'
7676
ApiPagination.config.page_header = 'X-Page'
7777

78-
get :index, count: 10
78+
get :index, params: {count: 10}
7979
end
8080

8181
after do
@@ -116,7 +116,7 @@
116116
before { ApiPagination.config.include_total = false }
117117

118118
it 'should not include a Total header' do
119-
get :index, count: 10
119+
get :index, params: {count: 10}
120120

121121
expect(response.header['Total']).to be_nil
122122
end
@@ -137,7 +137,7 @@
137137
end
138138

139139
it 'should work' do
140-
get :index, :foo => 2, :count => 100
140+
get :index, params: {foo: 2, count: 100}
141141

142142
expect(response.header['Page']).to eq('2')
143143
end
@@ -158,7 +158,7 @@
158158
end
159159

160160
it 'should work' do
161-
get :index, :foo => { :bar => 2 }, :count => 100
161+
get :index, params: {foo: {bar: 2}, count: 100}
162162

163163
expect(response.header['Page']).to eq('2')
164164
end
@@ -176,7 +176,7 @@
176176
end
177177

178178
it 'should work' do
179-
get :index_with_no_per_page, :foo => 2, :count => 100
179+
get :index_with_no_per_page, params: {foo: 2, count: 100}
180180

181181
expect(response.header['Per-Page']).to eq('2')
182182
end
@@ -194,7 +194,7 @@
194194
end
195195

196196
it 'should work' do
197-
get :index_with_no_per_page, :foo => { :bar => 2 }, :count => 100
197+
get :index_with_no_per_page, params: {foo: {bar: 2}, count: 100}
198198

199199
expect(response.header['Per-Page']).to eq('2')
200200
end
@@ -211,7 +211,7 @@
211211
end
212212

213213
specify do
214-
get :index_with_paginate_array_options, params
214+
get :index_with_paginate_array_options, params: params
215215
expect(response.header['Total'].to_i).to eq total_header
216216
end
217217
end
@@ -267,7 +267,7 @@ class Fixnum
267267
end
268268

269269
it 'should use default per page from model' do
270-
get :index_with_no_per_page, count: 100
270+
get :index_with_no_per_page, params: {count: 100}
271271

272272
expect(response.header['Per-Page']).to eq('6')
273273
end
@@ -278,7 +278,7 @@ class Fixnum
278278
@per_page = nil
279279
end
280280

281-
get :index_with_no_per_page, count: 100
281+
get :index_with_no_per_page, params: {count: 100}
282282

283283
expect(response.header['Per-Page']).to eq(
284284
case ApiPagination.config.paginator

spec/support/numbers_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'action_controller'
2+
require 'api-pagination/hooks'
23
require 'ostruct'
34

45
module Rails
@@ -58,7 +59,7 @@ def to_json(options = {})
5859
end
5960
end
6061

61-
class NumbersController < ActionController::Base
62+
class NumbersController < ApiPagination::Hooks.rails_parent_controller
6263
include Rails.application.routes.url_helpers
6364

6465
def index

0 commit comments

Comments
 (0)