Skip to content

Commit a5bd689

Browse files
authored
Merge pull request #91 from digitaltom/master
add config option for base_url
2 parents c9fea97 + 58626b1 commit a5bd689

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

lib/api-pagination/configuration.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ class Configuration
88

99
attr_accessor :include_total
1010

11+
attr_accessor :base_url
12+
1113
def configure(&block)
1214
yield self
1315
end
@@ -17,6 +19,7 @@ def initialize
1719
@per_page_header = 'Per-Page'
1820
@page_header = nil
1921
@include_total = true
22+
@base_url = nil
2023
end
2124

2225
['page', 'per_page'].each do |param_name|

lib/rails/pagination.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def _paginate_collection(collection, options={})
2929

3030
collection = ApiPagination.paginate(collection, options)
3131

32-
links = (headers['Link'] || "").split(',').map(&:strip)
33-
url = request.original_url.sub(/\?.*$/, '')
32+
links = (headers['Link'] || '').split(',').map(&:strip)
33+
url = base_url + request.path_info
3434
pages = ApiPagination.pages_from(collection)
3535

3636
pages.each do |k, v|
@@ -58,5 +58,10 @@ def total_count(collection, options)
5858
end
5959
total_count || ApiPagination.total_from(collection)
6060
end
61+
62+
def base_url
63+
ApiPagination.config.base_url || request.base_url
64+
end
65+
6166
end
6267
end

spec/rails_spec.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,23 @@
7474
ApiPagination.config.total_header = 'X-Total-Count'
7575
ApiPagination.config.per_page_header = 'X-Per-Page'
7676
ApiPagination.config.page_header = 'X-Page'
77+
ApiPagination.config.base_url = 'http://guybrush:3000'
7778

78-
get :index, params: {count: 10}
79+
get :index, params: params
7980
end
8081

8182
after do
8283
ApiPagination.config.total_header = 'Total'
8384
ApiPagination.config.per_page_header = 'Per-Page'
8485
ApiPagination.config.page_header = nil
86+
ApiPagination.config.base_url = nil
8587
end
8688

89+
let(:params) { { count: 10 } }
8790
let(:total) { response.header['X-Total-Count'].to_i }
8891
let(:per_page) { response.header['X-Per-Page'].to_i }
8992
let(:page) { response.header['X-Page'].to_i }
93+
let(:link) { response.header['Link'] }
9094

9195
it 'should give a X-Total-Count header' do
9296
headers_keys = response.headers.keys
@@ -110,6 +114,15 @@
110114
expect(headers_keys).to include('X-Page')
111115
expect(page).to eq(1)
112116
end
117+
118+
context 'with paginated result' do
119+
let(:params) { { count: 20 } }
120+
it 'should use custom base_url in the Link header' do
121+
122+
expect(response.headers['Link']).to eq(
123+
'<http://guybrush:3000/numbers?count=20&page=2>; rel="last", <http://guybrush:3000/numbers?count=20&page=2>; rel="next"')
124+
end
125+
end
113126
end
114127

115128
context 'configured not to include the total' do

0 commit comments

Comments
 (0)