will_paginate 3.3.1 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/will_paginate/locale/en.yml +2 -0
- data/lib/will_paginate/version.rb +3 -3
- data/lib/will_paginate/view_helpers/action_view.rb +6 -2
- data/lib/will_paginate/view_helpers/link_renderer.rb +7 -5
- data/lib/will_paginate.rb +0 -12
- metadata +8 -38
- data/lib/will_paginate/data_mapper.rb +0 -100
- data/lib/will_paginate/view_helpers/merb.rb +0 -26
- data/spec/collection_spec.rb +0 -139
- data/spec/console +0 -12
- data/spec/console_fixtures.rb +0 -28
- data/spec/database.yml +0 -29
- data/spec/finders/active_record_spec.rb +0 -424
- data/spec/finders/activerecord_test_connector.rb +0 -107
- data/spec/fixtures/admin.rb +0 -3
- data/spec/fixtures/developer.rb +0 -9
- data/spec/fixtures/developers_projects.yml +0 -13
- data/spec/fixtures/project.rb +0 -13
- data/spec/fixtures/projects.yml +0 -6
- data/spec/fixtures/replies.yml +0 -29
- data/spec/fixtures/reply.rb +0 -8
- data/spec/fixtures/schema.rb +0 -38
- data/spec/fixtures/topic.rb +0 -8
- data/spec/fixtures/topics.yml +0 -30
- data/spec/fixtures/user.rb +0 -2
- data/spec/fixtures/users.yml +0 -35
- data/spec/matchers/deprecation_matcher.rb +0 -27
- data/spec/matchers/phrase_matcher.rb +0 -19
- data/spec/matchers/query_count_matcher.rb +0 -36
- data/spec/page_number_spec.rb +0 -91
- data/spec/per_page_spec.rb +0 -41
- data/spec/spec_helper.rb +0 -41
- data/spec/view_helpers/action_view_spec.rb +0 -481
- data/spec/view_helpers/base_spec.rb +0 -143
- data/spec/view_helpers/link_renderer_base_spec.rb +0 -87
- data/spec/view_helpers/view_example_group.rb +0 -105
@@ -1,481 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'spec_helper'
|
3
|
-
require 'action_controller'
|
4
|
-
require 'action_view'
|
5
|
-
require 'will_paginate/view_helpers/action_view'
|
6
|
-
require 'will_paginate/collection'
|
7
|
-
|
8
|
-
Routes = ActionDispatch::Routing::RouteSet.new
|
9
|
-
|
10
|
-
Routes.draw do
|
11
|
-
get 'dummy/page/:page' => 'dummy#index'
|
12
|
-
get 'dummy/dots/page.:page' => 'dummy#dots'
|
13
|
-
get 'ibocorp(/:page)' => 'ibocorp#index',
|
14
|
-
:constraints => { :page => /\d+/ }, :defaults => { :page => 1 }
|
15
|
-
|
16
|
-
get 'foo/bar' => 'foo#bar'
|
17
|
-
get 'baz/list' => 'baz#list'
|
18
|
-
end
|
19
|
-
|
20
|
-
describe WillPaginate::ActionView do
|
21
|
-
|
22
|
-
before(:all) do
|
23
|
-
I18n.load_path.concat WillPaginate::I18n.load_path
|
24
|
-
I18n.enforce_available_locales = false
|
25
|
-
|
26
|
-
ActionController::Parameters.permit_all_parameters = false
|
27
|
-
end
|
28
|
-
|
29
|
-
before(:each) do
|
30
|
-
I18n.reload!
|
31
|
-
end
|
32
|
-
|
33
|
-
before(:each) do
|
34
|
-
@assigns = {}
|
35
|
-
@controller = DummyController.new
|
36
|
-
@request = @controller.request
|
37
|
-
@template = '<%= will_paginate collection, options %>'
|
38
|
-
end
|
39
|
-
|
40
|
-
attr_reader :assigns, :controller, :request
|
41
|
-
|
42
|
-
def render(locals)
|
43
|
-
lookup_context = []
|
44
|
-
lookup_context = ActionView::LookupContext.new(lookup_context)
|
45
|
-
|
46
|
-
klass = ActionView::Base
|
47
|
-
klass = klass.with_empty_template_cache if klass.respond_to?(:with_empty_template_cache)
|
48
|
-
@view = klass.new(lookup_context, @assigns, @controller)
|
49
|
-
@view.request = @request
|
50
|
-
@view.singleton_class.send(:include, @controller._routes.url_helpers)
|
51
|
-
@view.render(:inline => @template, :locals => locals)
|
52
|
-
end
|
53
|
-
|
54
|
-
## basic pagination ##
|
55
|
-
|
56
|
-
it "should render" do
|
57
|
-
paginate do |pagination|
|
58
|
-
assert_select 'a[href]', 3 do |elements|
|
59
|
-
validate_page_numbers [2,3,2], elements
|
60
|
-
text(elements[2]).should == 'Next →'
|
61
|
-
end
|
62
|
-
assert_select 'span', 1 do |spans|
|
63
|
-
spans[0]['class'].should == 'previous_page disabled'
|
64
|
-
text(spans[0]).should == '← Previous'
|
65
|
-
end
|
66
|
-
assert_select 'em.current', '1'
|
67
|
-
text(pagination[0]).should == '← Previous 1 2 3 Next →'
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
it "should override existing page param value" do
|
72
|
-
request.params :page => 1
|
73
|
-
paginate do |pagination|
|
74
|
-
assert_select 'a[href]', 3 do |elements|
|
75
|
-
validate_page_numbers [2,3,2], elements
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should render nothing when there is only 1 page" do
|
81
|
-
paginate(:per_page => 30).should be_empty
|
82
|
-
end
|
83
|
-
|
84
|
-
it "should paginate with options" do
|
85
|
-
paginate({ :page => 2 }, :class => 'will_paginate', :previous_label => 'Prev', :next_label => 'Next') do
|
86
|
-
assert_select 'a[href]', 4 do |elements|
|
87
|
-
validate_page_numbers [1,1,3,3], elements
|
88
|
-
# test rel attribute values:
|
89
|
-
text(elements[0]).should == 'Prev'
|
90
|
-
elements[0]['rel'].should == 'prev'
|
91
|
-
text(elements[1]).should == '1'
|
92
|
-
elements[1]['rel'].should == 'prev'
|
93
|
-
text(elements[3]).should == 'Next'
|
94
|
-
elements[3]['rel'].should == 'next'
|
95
|
-
end
|
96
|
-
assert_select '.current', '2'
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
it "should paginate using a custom renderer class" do
|
101
|
-
paginate({}, :renderer => AdditionalLinkAttributesRenderer) do
|
102
|
-
assert_select 'a[default=true]', 3
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
it "should paginate using a custom renderer instance" do
|
107
|
-
renderer = WillPaginate::ActionView::LinkRenderer.new
|
108
|
-
def renderer.gap() '<span class="my-gap">~~</span>' end
|
109
|
-
|
110
|
-
paginate({ :per_page => 2 }, :inner_window => 0, :outer_window => 0, :renderer => renderer) do
|
111
|
-
assert_select 'span.my-gap', '~~'
|
112
|
-
end
|
113
|
-
|
114
|
-
renderer = AdditionalLinkAttributesRenderer.new(:title => 'rendered')
|
115
|
-
paginate({}, :renderer => renderer) do
|
116
|
-
assert_select 'a[title=rendered]', 3
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
it "should have classnames on previous/next links" do
|
121
|
-
paginate do |pagination|
|
122
|
-
assert_select 'span.disabled.previous_page:first-child'
|
123
|
-
assert_select 'a.next_page[href]:last-child'
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
it "should match expected markup" do
|
128
|
-
paginate
|
129
|
-
expected = <<-HTML
|
130
|
-
<div class="pagination" role="navigation" aria-label="Pagination"><span class="previous_page disabled" aria-disabled="true">← Previous</span>
|
131
|
-
<em class="current" aria-label="Page 1" aria-current="page">1</em>
|
132
|
-
<a href="/foo/bar?page=2" aria-label="Page 2" rel="next">2</a>
|
133
|
-
<a href="/foo/bar?page=3" aria-label="Page 3">3</a>
|
134
|
-
<a href="/foo/bar?page=2" class="next_page" rel="next">Next →</a></div>
|
135
|
-
HTML
|
136
|
-
expected.strip!.gsub!(/\s{2,}/, ' ')
|
137
|
-
expected_dom = parse_html_document(expected)
|
138
|
-
|
139
|
-
if expected_dom.respond_to?(:canonicalize)
|
140
|
-
html_document.canonicalize.should == expected_dom.canonicalize
|
141
|
-
else
|
142
|
-
html_document.root.should == expected_dom.root
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
it "should output escaped URLs" do
|
147
|
-
paginate({:page => 1, :per_page => 1, :total_entries => 2},
|
148
|
-
:page_links => false, :params => { :tag => '<br>' })
|
149
|
-
|
150
|
-
assert_select 'a[href]', 1 do |links|
|
151
|
-
query = links.first['href'].split('?', 2)[1]
|
152
|
-
parts = query.gsub('&', '&').split('&').sort
|
153
|
-
parts.should == %w(page=2 tag=%3Cbr%3E)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
## advanced options for pagination ##
|
158
|
-
|
159
|
-
it "should be able to render without container" do
|
160
|
-
paginate({}, :container => false)
|
161
|
-
assert_select 'div.pagination', 0, 'main DIV present when it shouldn\'t'
|
162
|
-
assert_select 'a[href]', 3
|
163
|
-
end
|
164
|
-
|
165
|
-
it "should be able to render without page links" do
|
166
|
-
paginate({ :page => 2 }, :page_links => false) do
|
167
|
-
assert_select 'a[href]', 2 do |elements|
|
168
|
-
validate_page_numbers [1,3], elements
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
## other helpers ##
|
174
|
-
|
175
|
-
it "should render a paginated section" do
|
176
|
-
@template = <<-ERB
|
177
|
-
<%= paginated_section collection, options do %>
|
178
|
-
<%= content_tag :div, '', :id => "developers" %>
|
179
|
-
<% end %>
|
180
|
-
ERB
|
181
|
-
|
182
|
-
paginate
|
183
|
-
assert_select 'div.pagination', 2
|
184
|
-
assert_select 'div.pagination + div#developers', 1
|
185
|
-
end
|
186
|
-
|
187
|
-
it "should not render a paginated section with a single page" do
|
188
|
-
@template = <<-ERB
|
189
|
-
<%= paginated_section collection, options do %>
|
190
|
-
<%= content_tag :div, '', :id => "developers" %>
|
191
|
-
<% end %>
|
192
|
-
ERB
|
193
|
-
|
194
|
-
paginate(:total_entries => 1)
|
195
|
-
assert_select 'div.pagination', 0
|
196
|
-
assert_select 'div#developers', 1
|
197
|
-
end
|
198
|
-
|
199
|
-
## parameter handling in page links ##
|
200
|
-
|
201
|
-
it "should preserve parameters on GET" do
|
202
|
-
request.params :foo => { :bar => 'baz' }
|
203
|
-
paginate
|
204
|
-
assert_links_match /foo\[bar\]=baz/
|
205
|
-
end
|
206
|
-
|
207
|
-
it "doesn't allow tampering with host, port, protocol" do
|
208
|
-
request.params :host => 'disney.com', :port => '99', :protocol => 'ftp'
|
209
|
-
paginate
|
210
|
-
assert_links_match %r{^/foo/bar}
|
211
|
-
assert_no_links_match /disney/
|
212
|
-
assert_no_links_match /99/
|
213
|
-
assert_no_links_match /ftp/
|
214
|
-
end
|
215
|
-
|
216
|
-
it "doesn't allow tampering with script_name" do
|
217
|
-
request.params :script_name => 'p0wned', :original_script_name => 'p0wned'
|
218
|
-
paginate
|
219
|
-
assert_links_match %r{^/foo/bar}
|
220
|
-
assert_no_links_match /p0wned/
|
221
|
-
end
|
222
|
-
|
223
|
-
it "should not preserve parameters on POST" do
|
224
|
-
request.post
|
225
|
-
request.params :foo => 'bar'
|
226
|
-
paginate
|
227
|
-
assert_no_links_match /foo=bar/
|
228
|
-
end
|
229
|
-
|
230
|
-
it "should add additional parameters to links" do
|
231
|
-
paginate({}, :params => { :foo => 'bar' })
|
232
|
-
assert_links_match /foo=bar/
|
233
|
-
end
|
234
|
-
|
235
|
-
it "should add anchor parameter" do
|
236
|
-
paginate({}, :params => { :anchor => 'anchor' })
|
237
|
-
assert_links_match /#anchor$/
|
238
|
-
end
|
239
|
-
|
240
|
-
it "should remove arbitrary parameters" do
|
241
|
-
request.params :foo => 'bar'
|
242
|
-
paginate({}, :params => { :foo => nil })
|
243
|
-
assert_no_links_match /foo=bar/
|
244
|
-
end
|
245
|
-
|
246
|
-
it "should override default route parameters" do
|
247
|
-
paginate({}, :params => { :controller => 'baz', :action => 'list' })
|
248
|
-
assert_links_match %r{\Wbaz/list\W}
|
249
|
-
end
|
250
|
-
|
251
|
-
it "should paginate with custom page parameter" do
|
252
|
-
paginate({ :page => 2 }, :param_name => :developers_page) do
|
253
|
-
assert_select 'a[href]', 4 do |elements|
|
254
|
-
validate_page_numbers [1,1,3,3], elements, :developers_page
|
255
|
-
end
|
256
|
-
end
|
257
|
-
end
|
258
|
-
|
259
|
-
it "should paginate with complex custom page parameter" do
|
260
|
-
request.params :developers => { :page => 2 }
|
261
|
-
|
262
|
-
paginate({ :page => 2 }, :param_name => 'developers[page]') do
|
263
|
-
assert_select 'a[href]', 4 do |links|
|
264
|
-
assert_links_match /\?developers\[page\]=\d+$/, links
|
265
|
-
validate_page_numbers [1,1,3,3], links, 'developers[page]'
|
266
|
-
end
|
267
|
-
end
|
268
|
-
end
|
269
|
-
|
270
|
-
it "should paginate with custom route page parameter" do
|
271
|
-
request.symbolized_path_parameters.update :controller => 'dummy', :action => 'index'
|
272
|
-
paginate :per_page => 2 do
|
273
|
-
assert_select 'a[href]', 6 do |links|
|
274
|
-
assert_links_match %r{/page/(\d+)$}, links, [2, 3, 4, 5, 6, 2]
|
275
|
-
end
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
it "should paginate with custom route with dot separator page parameter" do
|
280
|
-
request.symbolized_path_parameters.update :controller => 'dummy', :action => 'dots'
|
281
|
-
paginate :per_page => 2 do
|
282
|
-
assert_select 'a[href]', 6 do |links|
|
283
|
-
assert_links_match %r{/page\.(\d+)$}, links, [2, 3, 4, 5, 6, 2]
|
284
|
-
end
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
it "should paginate with custom route and first page number implicit" do
|
289
|
-
request.symbolized_path_parameters.update :controller => 'ibocorp', :action => 'index'
|
290
|
-
paginate :page => 2, :per_page => 2 do
|
291
|
-
assert_select 'a[href]', 7 do |links|
|
292
|
-
assert_links_match %r{/ibocorp(?:/(\d+))?$}, links, [nil, nil, 3, 4, 5, 6, 3]
|
293
|
-
end
|
294
|
-
end
|
295
|
-
# Routes.recognize_path('/ibocorp/2').should == {:page=>'2', :action=>'index', :controller=>'ibocorp'}
|
296
|
-
# Routes.recognize_path('/ibocorp/foo').should == {:action=>'foo', :controller=>'ibocorp'}
|
297
|
-
end
|
298
|
-
|
299
|
-
## internal hardcore stuff ##
|
300
|
-
|
301
|
-
it "should be able to guess the collection name" do
|
302
|
-
collection = mock
|
303
|
-
collection.expects(:total_pages).returns(1)
|
304
|
-
|
305
|
-
@template = '<%= will_paginate options %>'
|
306
|
-
controller.controller_name = 'developers'
|
307
|
-
assigns['developers'] = collection
|
308
|
-
|
309
|
-
paginate(nil)
|
310
|
-
end
|
311
|
-
|
312
|
-
it "should fail if the inferred collection is nil" do
|
313
|
-
@template = '<%= will_paginate options %>'
|
314
|
-
controller.controller_name = 'developers'
|
315
|
-
|
316
|
-
lambda {
|
317
|
-
paginate(nil)
|
318
|
-
}.should raise_error(ActionView::TemplateError, /@developers/)
|
319
|
-
end
|
320
|
-
|
321
|
-
## i18n
|
322
|
-
|
323
|
-
it "is able to translate previous/next labels" do
|
324
|
-
translation :will_paginate => {
|
325
|
-
:previous_label => 'Go back',
|
326
|
-
:next_label => 'Load more'
|
327
|
-
}
|
328
|
-
|
329
|
-
paginate do |pagination|
|
330
|
-
assert_select 'span.disabled:first-child', 'Go back'
|
331
|
-
assert_select 'a[rel=next]', 'Load more'
|
332
|
-
end
|
333
|
-
end
|
334
|
-
|
335
|
-
it "renders using ActionView helpers on a custom object" do
|
336
|
-
helper = Class.new {
|
337
|
-
attr_reader :controller
|
338
|
-
include ActionView::Helpers::UrlHelper
|
339
|
-
include Routes.url_helpers
|
340
|
-
include WillPaginate::ActionView
|
341
|
-
}.new
|
342
|
-
helper.default_url_options[:controller] = 'dummy'
|
343
|
-
|
344
|
-
collection = WillPaginate::Collection.new(2, 1, 3)
|
345
|
-
@render_output = helper.will_paginate(collection)
|
346
|
-
|
347
|
-
assert_select 'a[href]', 4 do |links|
|
348
|
-
urls = links.map {|l| l['href'] }.uniq
|
349
|
-
urls.should == ['/dummy/page/1', '/dummy/page/3']
|
350
|
-
end
|
351
|
-
end
|
352
|
-
|
353
|
-
it "renders using ActionDispatch helper on a custom object" do
|
354
|
-
helper = Class.new {
|
355
|
-
include ActionDispatch::Routing::UrlFor
|
356
|
-
include Routes.url_helpers
|
357
|
-
include WillPaginate::ActionView
|
358
|
-
}.new
|
359
|
-
helper.default_url_options.update \
|
360
|
-
:only_path => true,
|
361
|
-
:controller => 'dummy'
|
362
|
-
|
363
|
-
collection = WillPaginate::Collection.new(2, 1, 3)
|
364
|
-
@render_output = helper.will_paginate(collection)
|
365
|
-
|
366
|
-
assert_select 'a[href]', 4 do |links|
|
367
|
-
urls = links.map {|l| l['href'] }.uniq
|
368
|
-
urls.should == ['/dummy/page/1', '/dummy/page/3']
|
369
|
-
end
|
370
|
-
end
|
371
|
-
|
372
|
-
# TODO: re-enable once Rails 6.1.4 ships
|
373
|
-
xit "page_entries_info" do
|
374
|
-
@template = "<%= page_entries_info collection, options %>"
|
375
|
-
output = render(
|
376
|
-
collection: WillPaginate::Collection.new(1, 1, 3),
|
377
|
-
options: {html: false},
|
378
|
-
)
|
379
|
-
output.should == "Displaying entries 1 - 0 of 3 in total"
|
380
|
-
end
|
381
|
-
|
382
|
-
private
|
383
|
-
|
384
|
-
def translation(data)
|
385
|
-
I18n.available_locales # triggers loading existing translations
|
386
|
-
I18n.backend.store_translations(:en, data)
|
387
|
-
end
|
388
|
-
|
389
|
-
# Normalizes differences between HTML::Document and Nokogiri::HTML
|
390
|
-
def text(node)
|
391
|
-
node.inner_text.gsub('→', '→').gsub('←', '←')
|
392
|
-
end
|
393
|
-
end
|
394
|
-
|
395
|
-
class AdditionalLinkAttributesRenderer < WillPaginate::ActionView::LinkRenderer
|
396
|
-
def initialize(link_attributes = nil)
|
397
|
-
super()
|
398
|
-
@additional_link_attributes = link_attributes || { :default => 'true' }
|
399
|
-
end
|
400
|
-
|
401
|
-
def link(text, target, attributes = {})
|
402
|
-
super(text, target, attributes.merge(@additional_link_attributes))
|
403
|
-
end
|
404
|
-
end
|
405
|
-
|
406
|
-
class DummyController
|
407
|
-
attr_reader :request
|
408
|
-
attr_accessor :controller_name
|
409
|
-
|
410
|
-
include ActionController::UrlFor
|
411
|
-
include Routes.url_helpers
|
412
|
-
|
413
|
-
def initialize
|
414
|
-
@request = DummyRequest.new(self)
|
415
|
-
end
|
416
|
-
|
417
|
-
def params
|
418
|
-
@request.params
|
419
|
-
end
|
420
|
-
|
421
|
-
def env
|
422
|
-
{}
|
423
|
-
end
|
424
|
-
|
425
|
-
def _prefixes
|
426
|
-
[]
|
427
|
-
end
|
428
|
-
end
|
429
|
-
|
430
|
-
class IbocorpController < DummyController
|
431
|
-
end
|
432
|
-
|
433
|
-
class DummyRequest
|
434
|
-
attr_accessor :symbolized_path_parameters
|
435
|
-
alias :path_parameters :symbolized_path_parameters
|
436
|
-
|
437
|
-
def initialize(controller)
|
438
|
-
@controller = controller
|
439
|
-
@get = true
|
440
|
-
@params = {}.with_indifferent_access
|
441
|
-
@symbolized_path_parameters = { :controller => 'foo', :action => 'bar' }
|
442
|
-
end
|
443
|
-
|
444
|
-
def routes
|
445
|
-
@controller._routes
|
446
|
-
end
|
447
|
-
|
448
|
-
def get?
|
449
|
-
@get
|
450
|
-
end
|
451
|
-
|
452
|
-
def post
|
453
|
-
@get = false
|
454
|
-
end
|
455
|
-
|
456
|
-
def relative_url_root
|
457
|
-
''
|
458
|
-
end
|
459
|
-
|
460
|
-
def script_name
|
461
|
-
''
|
462
|
-
end
|
463
|
-
|
464
|
-
def params(more = nil)
|
465
|
-
@params.update(more) if more
|
466
|
-
ActionController::Parameters.new(@params)
|
467
|
-
end
|
468
|
-
|
469
|
-
def host_with_port
|
470
|
-
'example.com'
|
471
|
-
end
|
472
|
-
alias host host_with_port
|
473
|
-
|
474
|
-
def optional_port
|
475
|
-
''
|
476
|
-
end
|
477
|
-
|
478
|
-
def protocol
|
479
|
-
'http:'
|
480
|
-
end
|
481
|
-
end
|
@@ -1,143 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'will_paginate/view_helpers'
|
3
|
-
require 'will_paginate/array'
|
4
|
-
require 'active_support'
|
5
|
-
require 'active_support/core_ext/string/inflections'
|
6
|
-
require 'active_support/inflections'
|
7
|
-
|
8
|
-
describe WillPaginate::ViewHelpers do
|
9
|
-
|
10
|
-
before(:all) do
|
11
|
-
# make sure default translations aren't loaded
|
12
|
-
I18n.load_path.clear
|
13
|
-
I18n.enforce_available_locales = false
|
14
|
-
end
|
15
|
-
|
16
|
-
before(:each) do
|
17
|
-
I18n.reload!
|
18
|
-
end
|
19
|
-
|
20
|
-
include WillPaginate::ViewHelpers
|
21
|
-
|
22
|
-
describe "will_paginate" do
|
23
|
-
it "should render" do
|
24
|
-
collection = WillPaginate::Collection.new(1, 2, 4)
|
25
|
-
renderer = mock 'Renderer'
|
26
|
-
renderer.expects(:prepare).with(collection, instance_of(Hash), self)
|
27
|
-
renderer.expects(:to_html).returns('<PAGES>')
|
28
|
-
|
29
|
-
will_paginate(collection, :renderer => renderer).should == '<PAGES>'
|
30
|
-
end
|
31
|
-
|
32
|
-
it "should return nil for single-page collections" do
|
33
|
-
collection = mock 'Collection', :total_pages => 1
|
34
|
-
will_paginate(collection).should be_nil
|
35
|
-
end
|
36
|
-
|
37
|
-
it "should call html_safe on result" do
|
38
|
-
collection = WillPaginate::Collection.new(1, 2, 4)
|
39
|
-
|
40
|
-
html = mock 'HTML'
|
41
|
-
html.expects(:html_safe).returns(html)
|
42
|
-
renderer = mock 'Renderer'
|
43
|
-
renderer.stubs(:prepare)
|
44
|
-
renderer.expects(:to_html).returns(html)
|
45
|
-
|
46
|
-
will_paginate(collection, :renderer => renderer).should eql(html)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
describe "pagination_options" do
|
51
|
-
let(:pagination_options) { WillPaginate::ViewHelpers.pagination_options }
|
52
|
-
|
53
|
-
it "deprecates setting :renderer" do
|
54
|
-
begin
|
55
|
-
lambda {
|
56
|
-
pagination_options[:renderer] = 'test'
|
57
|
-
}.should have_deprecation("pagination_options[:renderer] shouldn't be set")
|
58
|
-
ensure
|
59
|
-
pagination_options.delete :renderer
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
describe "page_entries_info" do
|
65
|
-
before :all do
|
66
|
-
@array = ('a'..'z').to_a
|
67
|
-
end
|
68
|
-
|
69
|
-
def info(params, options = {})
|
70
|
-
collection = Hash === params ? @array.paginate(params) : params
|
71
|
-
page_entries_info collection, {:html => false}.merge(options)
|
72
|
-
end
|
73
|
-
|
74
|
-
it "should display middle results and total count" do
|
75
|
-
info(:page => 2, :per_page => 5).should == "Displaying strings 6 - 10 of 26 in total"
|
76
|
-
end
|
77
|
-
|
78
|
-
it "uses translation if available" do
|
79
|
-
translation :will_paginate => {
|
80
|
-
:page_entries_info => {:multi_page => 'Showing %{from} - %{to}'}
|
81
|
-
}
|
82
|
-
info(:page => 2, :per_page => 5).should == "Showing 6 - 10"
|
83
|
-
end
|
84
|
-
|
85
|
-
it "uses specific translation if available" do
|
86
|
-
translation :will_paginate => {
|
87
|
-
:page_entries_info => {:multi_page => 'Showing %{from} - %{to}'},
|
88
|
-
:string => { :page_entries_info => {:multi_page => 'Strings %{from} to %{to}'} }
|
89
|
-
}
|
90
|
-
info(:page => 2, :per_page => 5).should == "Strings 6 to 10"
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should output HTML by default" do
|
94
|
-
info({ :page => 2, :per_page => 5 }, :html => true).should ==
|
95
|
-
"Displaying strings <b>6 - 10</b> of <b>26</b> in total"
|
96
|
-
end
|
97
|
-
|
98
|
-
it "should display shortened end results" do
|
99
|
-
info(:page => 7, :per_page => 4).should include_phrase('strings 25 - 26')
|
100
|
-
end
|
101
|
-
|
102
|
-
it "should handle longer class names" do
|
103
|
-
collection = @array.paginate(:page => 2, :per_page => 5)
|
104
|
-
model = stub('Class', :name => 'ProjectType', :to_s => 'ProjectType')
|
105
|
-
collection.first.stubs(:class).returns(model)
|
106
|
-
info(collection).should include_phrase('project types')
|
107
|
-
end
|
108
|
-
|
109
|
-
it "should adjust output for single-page collections" do
|
110
|
-
info(('a'..'d').to_a.paginate(:page => 1, :per_page => 5)).should == "Displaying all 4 strings"
|
111
|
-
info(['a'].paginate(:page => 1, :per_page => 5)).should == "Displaying 1 string"
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should display 'no entries found' for empty collections" do
|
115
|
-
info([].paginate(:page => 1, :per_page => 5)).should == "No entries found"
|
116
|
-
end
|
117
|
-
|
118
|
-
it "uses model_name.human when available" do
|
119
|
-
name = stub('model name', :i18n_key => :flower_key)
|
120
|
-
name.expects(:human).with(:count => 1).returns('flower')
|
121
|
-
model = stub('Class', :model_name => name)
|
122
|
-
collection = [1].paginate(:page => 1)
|
123
|
-
|
124
|
-
info(collection, :model => model).should == "Displaying 1 flower"
|
125
|
-
end
|
126
|
-
|
127
|
-
it "uses custom translation instead of model_name.human" do
|
128
|
-
name = stub('model name', :i18n_key => :flower_key)
|
129
|
-
name.expects(:human).never
|
130
|
-
model = stub('Class', :model_name => name)
|
131
|
-
translation :will_paginate => {:models => {:flower_key => 'tulip'}}
|
132
|
-
collection = [1].paginate(:page => 1)
|
133
|
-
|
134
|
-
info(collection, :model => model).should == "Displaying 1 tulip"
|
135
|
-
end
|
136
|
-
|
137
|
-
private
|
138
|
-
|
139
|
-
def translation(data)
|
140
|
-
I18n.backend.store_translations(:en, data)
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
@@ -1,87 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'will_paginate/view_helpers/link_renderer_base'
|
3
|
-
require 'will_paginate/collection'
|
4
|
-
|
5
|
-
describe WillPaginate::ViewHelpers::LinkRendererBase do
|
6
|
-
|
7
|
-
before do
|
8
|
-
@renderer = described_class.new
|
9
|
-
end
|
10
|
-
|
11
|
-
it "should raise error when unprepared" do
|
12
|
-
lambda {
|
13
|
-
@renderer.pagination
|
14
|
-
}.should raise_error
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should prepare with collection and options" do
|
18
|
-
prepare({})
|
19
|
-
@renderer.send(:current_page).should == 1
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should have total_pages accessor" do
|
23
|
-
prepare :total_pages => 42
|
24
|
-
@renderer.send(:total_pages).should == 42
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should clear old cached values when prepared" do
|
28
|
-
prepare(:total_pages => 1)
|
29
|
-
@renderer.send(:total_pages).should == 1
|
30
|
-
# prepare with different object:
|
31
|
-
prepare(:total_pages => 2)
|
32
|
-
@renderer.send(:total_pages).should == 2
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should have pagination definition" do
|
36
|
-
prepare({ :total_pages => 1 }, :page_links => true)
|
37
|
-
@renderer.pagination.should == [:previous_page, 1, :next_page]
|
38
|
-
end
|
39
|
-
|
40
|
-
describe "visible page numbers" do
|
41
|
-
it "should calculate windowed visible links" do
|
42
|
-
prepare({ :page => 6, :total_pages => 11 }, :inner_window => 1, :outer_window => 1)
|
43
|
-
showing_pages 1, 2, :gap, 5, 6, 7, :gap, 10, 11
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should eliminate small gaps" do
|
47
|
-
prepare({ :page => 6, :total_pages => 11 }, :inner_window => 2, :outer_window => 1)
|
48
|
-
# pages 4 and 8 appear instead of the gap
|
49
|
-
showing_pages 1..11
|
50
|
-
end
|
51
|
-
|
52
|
-
it "should support having no windows at all" do
|
53
|
-
prepare({ :page => 4, :total_pages => 7 }, :inner_window => 0, :outer_window => 0)
|
54
|
-
showing_pages 1, :gap, 4, :gap, 7
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should adjust upper limit if lower is out of bounds" do
|
58
|
-
prepare({ :page => 1, :total_pages => 10 }, :inner_window => 2, :outer_window => 1)
|
59
|
-
showing_pages 1, 2, 3, 4, 5, :gap, 9, 10
|
60
|
-
end
|
61
|
-
|
62
|
-
it "should adjust lower limit if upper is out of bounds" do
|
63
|
-
prepare({ :page => 10, :total_pages => 10 }, :inner_window => 2, :outer_window => 1)
|
64
|
-
showing_pages 1, 2, :gap, 6, 7, 8, 9, 10
|
65
|
-
end
|
66
|
-
|
67
|
-
def showing_pages(*pages)
|
68
|
-
pages = pages.first.to_a if Array === pages.first or Range === pages.first
|
69
|
-
@renderer.send(:windowed_page_numbers).should == pages
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
protected
|
74
|
-
|
75
|
-
def collection(params = {})
|
76
|
-
if params[:total_pages]
|
77
|
-
params[:per_page] = 1
|
78
|
-
params[:total_entries] = params[:total_pages]
|
79
|
-
end
|
80
|
-
WillPaginate::Collection.new(params[:page] || 1, params[:per_page] || 30, params[:total_entries])
|
81
|
-
end
|
82
|
-
|
83
|
-
def prepare(collection_options, options = {})
|
84
|
-
@renderer.prepare(collection(collection_options), options)
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|