paginate-responder 1.5.0 → 1.6.0.b0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,34 +0,0 @@
1
- module PaginateResponder::Adapter
2
-
3
- # Pagination adapter for kaminari.
4
- #
5
- class KaminariAdapter < Base
6
-
7
- def suitable?
8
- resource.respond_to?(:page) and not resource.respond_to?(:paginate)
9
- end
10
-
11
- def paginate(opts)
12
- resource.page(opts[:page]).per(opts[:per_page])
13
- end
14
-
15
- def defaults
16
- {
17
- :per_page => Kaminari.config.default_per_page,
18
- :max_per_page => Kaminari.config.max_per_page
19
- }
20
- end
21
-
22
- def total_pages
23
- resource.num_pages if resource.respond_to? :num_pages
24
- end
25
-
26
- def total_count
27
- resource.total_count if resource.respond_to? :total_count
28
- end
29
- end
30
-
31
- if defined?(:Kaminari)
32
- ::PaginateResponder::Paginator.register KaminariAdapter
33
- end
34
- end
@@ -1,31 +0,0 @@
1
- module PaginateResponder::Adapter
2
-
3
- # Pagination adapter for will_paginate.
4
- #
5
- class PaginateAdapter < Base
6
-
7
- def suitable?
8
- resource.respond_to?(:paginate)
9
- end
10
-
11
- def paginate(opts)
12
- resource.paginate :page => opts[:page], :per_page => opts[:per_page]
13
- end
14
-
15
- def defaults
16
- { :per_page => 50, :max_per_page => 50 }
17
- end
18
-
19
- def total_pages
20
- resource.total_pages if resource.respond_to? :total_pages
21
- end
22
-
23
- def total_count
24
- resource.total_entries if resource.respond_to? :total_entries
25
- end
26
- end
27
-
28
- if defined?(:WillPaginate)
29
- ::PaginateResponder::Paginator.register PaginateAdapter
30
- end
31
- end
@@ -1,99 +0,0 @@
1
- module PaginateResponder
2
- class Paginator
3
- attr_reader :responder, :adapter, :resource
4
-
5
- def initialize(responder)
6
- @responder = responder
7
- @resource = responder.resource
8
- @adapter = find_adapter
9
- end
10
-
11
- def find_adapter
12
- return controller.pagination_adapter(resource) if controller.respond_to? :pagination_adapter
13
-
14
- self.class.adapters.each do |adapter_class|
15
- begin
16
- adapter_class.new(resource).tap do |adapter|
17
- return adapter if adapter.suitable?
18
- end
19
- rescue
20
- next
21
- end
22
- end
23
- Adapter::Base.new(resource)
24
- end
25
-
26
- def request; responder.request end
27
- def response; controller.response end
28
- def controller; responder.controller end
29
-
30
- def paginate!
31
- resource!
32
- headers!
33
-
34
- resource
35
- end
36
-
37
- def headers!
38
- link! 'first', 1
39
- link! 'prev', page - 1 if page > 1
40
- link! 'next', page + 1 if total_pages && page < total_pages
41
- link! 'last', total_pages if total_pages
42
-
43
- response.headers["X-Total-Pages"] = total_pages.to_s if total_pages
44
- response.headers["X-Total-Count"] = total_count.to_s if total_count
45
- response.headers["X-Per-Page"] = per_page.to_s
46
- end
47
-
48
- def link!(rel, page)
49
- pagination_params = { :page => page }
50
- pagination_params[:per_page] = per_page if request.params.has_key?(:per_page)
51
- response.link(controller.url_for(request.params.merge(pagination_params)), :rel => rel)
52
- end
53
-
54
- def resource!
55
- @resource = @adapter.paginate! :page => page, :per_page => per_page
56
- end
57
-
58
- def page
59
- @page ||= controller.page.try(:to_i) if controller.respond_to? :page
60
- @page ||= controller.params[:page].try(:to_i)
61
- @page ||= 1
62
- rescue
63
- 1
64
- end
65
-
66
- def per_page
67
- @per_page ||= controller.per_page.try(:to_i) if controller.respond_to? :per_page
68
- @per_page ||= controller.params[:per_page].try(:to_i)
69
- @per_page ||= @adapter.defaults[:per_page].try(:to_i)
70
- @per_page = [[1, @per_page].max, max_per_page].min
71
- rescue
72
- max_per_page
73
- end
74
-
75
- def max_per_page
76
- @max_per_page ||= controller.max_per_page.try(:to_i) if controller.respond_to? :max_per_page
77
- @max_per_page ||= @adapter.defaults[:max_per_page].try(:to_i)
78
- @max_per_page ||= 50
79
- end
80
-
81
- def total_pages
82
- @adapter.total_pages
83
- end
84
-
85
- def total_count
86
- @adapter.total_count
87
- end
88
-
89
- class << self
90
- def adapters
91
- @adapters ||= []
92
- end
93
-
94
- def register(adapter)
95
- adapters << adapter
96
- end
97
- end
98
- end
99
- end
@@ -1,247 +0,0 @@
1
- require 'test_helper.rb'
2
-
3
- GEM = ENV['GEM'].to_s.split ','
4
- GEM = ['will_paginate'] if GEM.empty?
5
- puts "[INFO] Running tests with #{GEM.join(' and ')}."
6
-
7
- class PaginateResponderTest < ActionController::TestCase
8
- tests PaginateController
9
-
10
- def array_resource
11
- ('AA'..'zz').to_a
12
- end
13
-
14
- GEM.each do |gem|
15
- case gem
16
- when 'will_paginate'
17
- require 'will_paginate/array'
18
- require 'will_paginate/active_record'
19
- when 'kaminari'
20
- require 'kaminari'
21
- require 'kaminari/models/array_extension'
22
- Kaminari::Hooks.init
23
-
24
- Kaminari.configure do |config|
25
- config.default_per_page = 50
26
- config.max_per_page = 50
27
- end
28
-
29
- def array_resource
30
- Kaminari.paginate_array ('AA'..'zz').to_a
31
- end
32
- end
33
- end
34
-
35
- def setup
36
- if ActiveRecord::VERSION::MAJOR >= 4
37
- @controller.resource = ArModel.all
38
- else
39
- @controller.resource = ArModel.scoped
40
- end
41
- end
42
-
43
- def json
44
- JSON[@response.body]
45
- end
46
-
47
- def test_pagination
48
- get :index, format: :json
49
-
50
- assert_equal 50, json.size
51
- assert_equal (1..50).to_a, json
52
- end
53
-
54
- def test_pagination_arr
55
- @controller.resource = array_resource
56
- get :index, format: :json
57
-
58
- assert_equal 50, json.size
59
- assert_equal ('AA'..'zz').to_a[0..49], json
60
- end
61
-
62
- def test_pagination_assoc
63
- @controller.resource = ArModel.find(1).ar_assoc_models
64
- get :index, format: :json
65
-
66
- assert_equal 5, json.size
67
- assert_equal (1..5).to_a, json
68
- end
69
-
70
- def test_pagination_page_2
71
- get :index, format: :json, page: 2
72
-
73
- assert_equal 50, json.size
74
- assert_equal (51..100).to_a, json
75
- end
76
-
77
- def test_pagination_arr_page_2
78
- @controller.resource = array_resource
79
- get :index, format: :json, page: 2
80
-
81
- assert_equal 50, json.size
82
- assert_equal ('AA'..'zz').to_a[50..99], json
83
- end
84
-
85
- def test_pagination_per_page
86
- get :index, format: :json, page: 1, per_page: 10
87
-
88
- assert_equal 10, json.size
89
- assert_equal (1..10).to_a, json
90
- end
91
-
92
-
93
- def test_pagination_arr_per_page
94
- @controller.resource = array_resource
95
- get :index, format: :json, page: 1, per_page: 10
96
-
97
- assert_equal 10, json.size
98
- assert_equal ('AA'..'zz').to_a[0..9], json
99
- end
100
-
101
- def test_pagination_per_page_2
102
- get :index, format: :json, page: 2, per_page: 10
103
-
104
- assert_equal 10, json.size
105
- assert_equal (11..20).to_a, json
106
- end
107
-
108
- def test_pagination_arr_per_page_2
109
- @controller.resource = array_resource
110
- get :index, format: :json, page: 2, per_page: 10
111
-
112
- assert_equal 10, json.size
113
- assert_equal ('AA'..'zz').to_a[10..19], json
114
- end
115
-
116
- def test_headers
117
- get :index, format: :json
118
-
119
- assert_equal 3, response.links.size
120
-
121
- assert_equal 'first', response.links[0][:params][:rel]
122
- assert_equal 'http://test.host/index.json?page=1', response.links[0][:url]
123
-
124
- assert_equal 'next', response.links[1][:params][:rel]
125
- assert_equal 'http://test.host/index.json?page=2', response.links[1][:url]
126
-
127
- assert_equal 'last', response.links[2][:params][:rel]
128
- assert_equal 'http://test.host/index.json?page=14', response.links[2][:url]
129
- end
130
-
131
- def test_headers_page_2
132
- get :index, format: :json, page: 2
133
-
134
- assert_equal 4, response.links.size
135
-
136
- assert_equal 'first', response.links[0][:params][:rel]
137
- assert_equal 'http://test.host/index.json?page=1', response.links[0][:url]
138
-
139
- assert_equal 'prev', response.links[1][:params][:rel]
140
- assert_equal 'http://test.host/index.json?page=1', response.links[1][:url]
141
-
142
- assert_equal 'next', response.links[2][:params][:rel]
143
- assert_equal 'http://test.host/index.json?page=3', response.links[2][:url]
144
-
145
- assert_equal 'last', response.links[3][:params][:rel]
146
- assert_equal 'http://test.host/index.json?page=14', response.links[3][:url]
147
- end
148
-
149
- def test_headers_page_5
150
- get :index, format: :json, page: 5
151
-
152
- assert_equal 4, response.links.size
153
-
154
- assert_equal 'first', response.links[0][:params][:rel]
155
- assert_equal 'http://test.host/index.json?page=1', response.links[0][:url]
156
-
157
- assert_equal 'prev', response.links[1][:params][:rel]
158
- assert_equal 'http://test.host/index.json?page=4', response.links[1][:url]
159
-
160
- assert_equal 'next', response.links[2][:params][:rel]
161
- assert_equal 'http://test.host/index.json?page=6', response.links[2][:url]
162
-
163
- assert_equal 'last', response.links[3][:params][:rel]
164
- assert_equal 'http://test.host/index.json?page=14', response.links[3][:url]
165
- end
166
-
167
- def test_headers_last_page
168
- get :index, format: :json, page: 14
169
-
170
- assert_equal 3, response.links.size
171
-
172
- assert_equal 'first', response.links[0][:params][:rel]
173
- assert_equal 'http://test.host/index.json?page=1', response.links[0][:url]
174
-
175
- assert_equal 'prev', response.links[1][:params][:rel]
176
- assert_equal 'http://test.host/index.json?page=13', response.links[1][:url]
177
-
178
- assert_equal 'last', response.links[2][:params][:rel]
179
- assert_equal 'http://test.host/index.json?page=14', response.links[2][:url]
180
- end
181
-
182
- def test_headers_page_before_last_page
183
- get :index, format: :json, page: 13
184
-
185
- assert_equal 4, response.links.size
186
-
187
- assert_equal 'first', response.links[0][:params][:rel]
188
- assert_equal 'http://test.host/index.json?page=1', response.links[0][:url]
189
-
190
- assert_equal 'prev', response.links[1][:params][:rel]
191
- assert_equal 'http://test.host/index.json?page=12', response.links[1][:url]
192
-
193
- assert_equal 'next', response.links[2][:params][:rel]
194
- assert_equal 'http://test.host/index.json?page=14', response.links[2][:url]
195
-
196
- assert_equal 'last', response.links[3][:params][:rel]
197
- assert_equal 'http://test.host/index.json?page=14', response.links[3][:url]
198
- end
199
-
200
- def test_headers_per_page
201
- get :index, format: :json, page: 1, per_page: 10
202
-
203
- assert_equal 3, response.links.size
204
-
205
- assert_equal 'first', response.links[0][:params][:rel]
206
- assert_equal 'http://test.host/index.json?page=1&per_page=10', response.links[0][:url]
207
-
208
- assert_equal 'next', response.links[1][:params][:rel]
209
- assert_equal 'http://test.host/index.json?page=2&per_page=10', response.links[1][:url]
210
-
211
- assert_equal 'last', response.links[2][:params][:rel]
212
- assert_equal 'http://test.host/index.json?page=68&per_page=10', response.links[2][:url]
213
- end
214
-
215
- def test_headers_per_page_maximum
216
- get :index, format: :json, page: 1, per_page: 100
217
-
218
- assert_equal 3, response.links.size
219
-
220
- assert_equal 'first', response.links[0][:params][:rel]
221
- assert_equal 'http://test.host/index.json?page=1&per_page=50', response.links[0][:url]
222
-
223
- assert_equal 'next', response.links[1][:params][:rel]
224
- assert_equal 'http://test.host/index.json?page=2&per_page=50', response.links[1][:url]
225
-
226
- assert_equal 'last', response.links[2][:params][:rel]
227
- assert_equal 'http://test.host/index.json?page=14&per_page=50', response.links[2][:url]
228
- end
229
-
230
- def test_custom_headers_total_pages
231
- get :index, format: :json
232
-
233
- assert_equal '14', response.headers['X-Total-Pages']
234
- end
235
-
236
- def test_custom_headers_total_count
237
- get :index, format: :json
238
-
239
- assert_equal '676', response.headers['X-Total-Count']
240
- end
241
-
242
- def test_custom_headers_per_page
243
- get :index, format: :json
244
-
245
- assert_equal '50', response.headers['X-Per-Page']
246
- end
247
- end