File tree Expand file tree Collapse file tree 3 files changed +24
-3
lines changed
Expand file tree Collapse file tree 3 files changed +24
-3
lines changed Original file line number Diff line number Diff 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 |
Original file line number Diff line number Diff 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
6267end
Original file line number Diff line number Diff line change 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
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
You can’t perform that action at this time.
0 commit comments