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
8
8
9
9
attr_accessor :include_total
10
10
11
+ attr_accessor :base_url
12
+
11
13
def configure ( &block )
12
14
yield self
13
15
end
@@ -17,6 +19,7 @@ def initialize
17
19
@per_page_header = 'Per-Page'
18
20
@page_header = nil
19
21
@include_total = true
22
+ @base_url = nil
20
23
end
21
24
22
25
[ '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={})
29
29
30
30
collection = ApiPagination . paginate ( collection , options )
31
31
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
34
34
pages = ApiPagination . pages_from ( collection )
35
35
36
36
pages . each do |k , v |
@@ -58,5 +58,10 @@ def total_count(collection, options)
58
58
end
59
59
total_count || ApiPagination . total_from ( collection )
60
60
end
61
+
62
+ def base_url
63
+ ApiPagination . config . base_url || request . base_url
64
+ end
65
+
61
66
end
62
67
end
Original file line number Diff line number Diff line change 74
74
ApiPagination . config . total_header = 'X-Total-Count'
75
75
ApiPagination . config . per_page_header = 'X-Per-Page'
76
76
ApiPagination . config . page_header = 'X-Page'
77
+ ApiPagination . config . base_url = 'http://guybrush:3000'
77
78
78
- get :index , params : { count : 10 }
79
+ get :index , params : params
79
80
end
80
81
81
82
after do
82
83
ApiPagination . config . total_header = 'Total'
83
84
ApiPagination . config . per_page_header = 'Per-Page'
84
85
ApiPagination . config . page_header = nil
86
+ ApiPagination . config . base_url = nil
85
87
end
86
88
89
+ let ( :params ) { { count : 10 } }
87
90
let ( :total ) { response . header [ 'X-Total-Count' ] . to_i }
88
91
let ( :per_page ) { response . header [ 'X-Per-Page' ] . to_i }
89
92
let ( :page ) { response . header [ 'X-Page' ] . to_i }
93
+ let ( :link ) { response . header [ 'Link' ] }
90
94
91
95
it 'should give a X-Total-Count header' do
92
96
headers_keys = response . headers . keys
110
114
expect ( headers_keys ) . to include ( 'X-Page' )
111
115
expect ( page ) . to eq ( 1 )
112
116
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
113
126
end
114
127
115
128
context 'configured not to include the total' do
You can’t perform that action at this time.
0 commit comments