will_paginate 3.0.4 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +7 -10
- data/lib/will_paginate/active_record.rb +18 -17
- data/lib/will_paginate/deprecation.rb +3 -3
- data/lib/will_paginate/i18n.rb +3 -3
- data/lib/will_paginate/locale/en.yml +4 -0
- data/lib/will_paginate/mongoid.rb +48 -0
- data/lib/will_paginate/page_number.rb +7 -11
- data/lib/will_paginate/railtie.rb +19 -13
- data/lib/will_paginate/version.rb +2 -2
- data/lib/will_paginate/view_helpers/action_view.rb +12 -5
- data/lib/will_paginate/view_helpers/hanami.rb +41 -0
- data/lib/will_paginate/view_helpers/link_renderer.rb +23 -17
- data/lib/will_paginate/view_helpers/link_renderer_base.rb +1 -1
- data/lib/will_paginate/view_helpers.rb +3 -1
- data/lib/will_paginate.rb +0 -12
- metadata +29 -58
- data/Rakefile +0 -25
- data/lib/will_paginate/data_mapper.rb +0 -95
- data/lib/will_paginate/view_helpers/merb.rb +0 -26
- data/spec/ci.rb +0 -29
- data/spec/collection_spec.rb +0 -139
- data/spec/console +0 -12
- data/spec/console_fixtures.rb +0 -28
- data/spec/database.yml +0 -22
- data/spec/finders/active_record_spec.rb +0 -556
- data/spec/finders/activerecord_test_connector.rb +0 -115
- data/spec/finders/data_mapper_spec.rb +0 -103
- data/spec/finders/data_mapper_test_connector.rb +0 -54
- data/spec/finders/sequel_spec.rb +0 -67
- data/spec/finders/sequel_test_connector.rb +0 -15
- data/spec/fixtures/admin.rb +0 -3
- data/spec/fixtures/developer.rb +0 -13
- data/spec/fixtures/developers_projects.yml +0 -13
- data/spec/fixtures/project.rb +0 -15
- data/spec/fixtures/projects.yml +0 -6
- data/spec/fixtures/replies.yml +0 -29
- data/spec/fixtures/reply.rb +0 -9
- data/spec/fixtures/schema.rb +0 -38
- data/spec/fixtures/topic.rb +0 -7
- data/spec/fixtures/topics.yml +0 -30
- data/spec/fixtures/user.rb +0 -2
- data/spec/fixtures/users.yml +0 -35
- data/spec/page_number_spec.rb +0 -65
- data/spec/per_page_spec.rb +0 -41
- data/spec/spec_helper.rb +0 -71
- data/spec/view_helpers/action_view_spec.rb +0 -423
- data/spec/view_helpers/base_spec.rb +0 -130
- data/spec/view_helpers/link_renderer_base_spec.rb +0 -87
- data/spec/view_helpers/view_example_group.rb +0 -121
@@ -1,423 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'active_support/rescuable' # needed for Ruby 1.9.1
|
3
|
-
require 'action_controller'
|
4
|
-
require 'will_paginate/view_helpers/action_view'
|
5
|
-
require 'will_paginate/collection'
|
6
|
-
|
7
|
-
Routes = ActionDispatch::Routing::RouteSet.new
|
8
|
-
|
9
|
-
Routes.draw do
|
10
|
-
get 'dummy/page/:page' => 'dummy#index'
|
11
|
-
get 'dummy/dots/page.:page' => 'dummy#dots'
|
12
|
-
get 'ibocorp(/:page)' => 'ibocorp#index',
|
13
|
-
:constraints => { :page => /\d+/ }, :defaults => { :page => 1 }
|
14
|
-
|
15
|
-
get ':controller(/:action(/:id(.:format)))'
|
16
|
-
end
|
17
|
-
|
18
|
-
describe WillPaginate::ActionView do
|
19
|
-
|
20
|
-
before(:all) do
|
21
|
-
I18n.load_path.concat WillPaginate::I18n.load_path
|
22
|
-
end
|
23
|
-
|
24
|
-
before(:each) do
|
25
|
-
I18n.reload!
|
26
|
-
end
|
27
|
-
|
28
|
-
before(:each) do
|
29
|
-
@assigns = {}
|
30
|
-
@controller = DummyController.new
|
31
|
-
@request = @controller.request
|
32
|
-
@template = '<%= will_paginate collection, options %>'
|
33
|
-
end
|
34
|
-
|
35
|
-
attr_reader :assigns, :controller, :request
|
36
|
-
|
37
|
-
def render(locals)
|
38
|
-
@view = ActionView::Base.new([], @assigns, @controller)
|
39
|
-
@view.request = @request
|
40
|
-
@view.singleton_class.send(:include, @controller._routes.url_helpers)
|
41
|
-
@view.render(:inline => @template, :locals => locals)
|
42
|
-
end
|
43
|
-
|
44
|
-
## basic pagination ##
|
45
|
-
|
46
|
-
it "should render" do
|
47
|
-
paginate do |pagination|
|
48
|
-
assert_select 'a[href]', 3 do |elements|
|
49
|
-
validate_page_numbers [2,3,2], elements
|
50
|
-
assert_select elements.last, ':last-child', "Next →"
|
51
|
-
end
|
52
|
-
assert_select 'span', 1
|
53
|
-
assert_select 'span.disabled:first-child', '← Previous'
|
54
|
-
assert_select 'em.current', '1'
|
55
|
-
pagination.first.inner_text.should == '← Previous 1 2 3 Next →'
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
it "should render nothing when there is only 1 page" do
|
60
|
-
paginate(:per_page => 30).should be_empty
|
61
|
-
end
|
62
|
-
|
63
|
-
it "should paginate with options" do
|
64
|
-
paginate({ :page => 2 }, :class => 'will_paginate', :previous_label => 'Prev', :next_label => 'Next') do
|
65
|
-
assert_select 'a[href]', 4 do |elements|
|
66
|
-
validate_page_numbers [1,1,3,3], elements
|
67
|
-
# test rel attribute values:
|
68
|
-
assert_select elements[1], 'a', '1' do |link|
|
69
|
-
link.first['rel'].should == 'prev start'
|
70
|
-
end
|
71
|
-
assert_select elements.first, 'a', "Prev" do |link|
|
72
|
-
link.first['rel'].should == 'prev start'
|
73
|
-
end
|
74
|
-
assert_select elements.last, 'a', "Next" do |link|
|
75
|
-
link.first['rel'].should == 'next'
|
76
|
-
end
|
77
|
-
end
|
78
|
-
assert_select '.current', '2'
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
it "should paginate using a custom renderer class" do
|
83
|
-
paginate({}, :renderer => AdditionalLinkAttributesRenderer) do
|
84
|
-
assert_select 'a[default=true]', 3
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
it "should paginate using a custom renderer instance" do
|
89
|
-
renderer = WillPaginate::ActionView::LinkRenderer.new
|
90
|
-
def renderer.gap() '<span class="my-gap">~~</span>' end
|
91
|
-
|
92
|
-
paginate({ :per_page => 2 }, :inner_window => 0, :outer_window => 0, :renderer => renderer) do
|
93
|
-
assert_select 'span.my-gap', '~~'
|
94
|
-
end
|
95
|
-
|
96
|
-
renderer = AdditionalLinkAttributesRenderer.new(:title => 'rendered')
|
97
|
-
paginate({}, :renderer => renderer) do
|
98
|
-
assert_select 'a[title=rendered]', 3
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
it "should have classnames on previous/next links" do
|
103
|
-
paginate do |pagination|
|
104
|
-
assert_select 'span.disabled.previous_page:first-child'
|
105
|
-
assert_select 'a.next_page[href]:last-child'
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
it "should match expected markup" do
|
110
|
-
paginate
|
111
|
-
expected = <<-HTML
|
112
|
-
<div class="pagination"><span class="previous_page disabled">← Previous</span>
|
113
|
-
<em class="current">1</em>
|
114
|
-
<a href="/foo/bar?page=2" rel="next">2</a>
|
115
|
-
<a href="/foo/bar?page=3">3</a>
|
116
|
-
<a href="/foo/bar?page=2" class="next_page" rel="next">Next →</a></div>
|
117
|
-
HTML
|
118
|
-
expected.strip!.gsub!(/\s{2,}/, ' ')
|
119
|
-
expected_dom = HTML::Document.new(expected).root
|
120
|
-
|
121
|
-
html_document.root.should == expected_dom
|
122
|
-
end
|
123
|
-
|
124
|
-
it "should output escaped URLs" do
|
125
|
-
paginate({:page => 1, :per_page => 1, :total_entries => 2},
|
126
|
-
:page_links => false, :params => { :tag => '<br>' })
|
127
|
-
|
128
|
-
assert_select 'a[href]', 1 do |links|
|
129
|
-
query = links.first['href'].split('?', 2)[1]
|
130
|
-
query.split('&').sort.should == %w(page=2 tag=%3Cbr%3E)
|
131
|
-
end
|
132
|
-
end
|
133
|
-
|
134
|
-
## advanced options for pagination ##
|
135
|
-
|
136
|
-
it "should be able to render without container" do
|
137
|
-
paginate({}, :container => false)
|
138
|
-
assert_select 'div.pagination', 0, 'main DIV present when it shouldn\'t'
|
139
|
-
assert_select 'a[href]', 3
|
140
|
-
end
|
141
|
-
|
142
|
-
it "should be able to render without page links" do
|
143
|
-
paginate({ :page => 2 }, :page_links => false) do
|
144
|
-
assert_select 'a[href]', 2 do |elements|
|
145
|
-
validate_page_numbers [1,3], elements
|
146
|
-
end
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
## other helpers ##
|
151
|
-
|
152
|
-
it "should render a paginated section" do
|
153
|
-
@template = <<-ERB
|
154
|
-
<%= paginated_section collection, options do %>
|
155
|
-
<%= content_tag :div, '', :id => "developers" %>
|
156
|
-
<% end %>
|
157
|
-
ERB
|
158
|
-
|
159
|
-
paginate
|
160
|
-
assert_select 'div.pagination', 2
|
161
|
-
assert_select 'div.pagination + div#developers', 1
|
162
|
-
end
|
163
|
-
|
164
|
-
it "should not render a paginated section with a single page" do
|
165
|
-
@template = <<-ERB
|
166
|
-
<%= paginated_section collection, options do %>
|
167
|
-
<%= content_tag :div, '', :id => "developers" %>
|
168
|
-
<% end %>
|
169
|
-
ERB
|
170
|
-
|
171
|
-
paginate(:total_entries => 1)
|
172
|
-
assert_select 'div.pagination', 0
|
173
|
-
assert_select 'div#developers', 1
|
174
|
-
end
|
175
|
-
|
176
|
-
## parameter handling in page links ##
|
177
|
-
|
178
|
-
it "should preserve parameters on GET" do
|
179
|
-
request.params :foo => { :bar => 'baz' }
|
180
|
-
paginate
|
181
|
-
assert_links_match /foo\[bar\]=baz/
|
182
|
-
end
|
183
|
-
|
184
|
-
it "should not preserve parameters on POST" do
|
185
|
-
request.post
|
186
|
-
request.params :foo => 'bar'
|
187
|
-
paginate
|
188
|
-
assert_no_links_match /foo=bar/
|
189
|
-
end
|
190
|
-
|
191
|
-
it "should add additional parameters to links" do
|
192
|
-
paginate({}, :params => { :foo => 'bar' })
|
193
|
-
assert_links_match /foo=bar/
|
194
|
-
end
|
195
|
-
|
196
|
-
it "should add anchor parameter" do
|
197
|
-
paginate({}, :params => { :anchor => 'anchor' })
|
198
|
-
assert_links_match /#anchor$/
|
199
|
-
end
|
200
|
-
|
201
|
-
it "should remove arbitrary parameters" do
|
202
|
-
request.params :foo => 'bar'
|
203
|
-
paginate({}, :params => { :foo => nil })
|
204
|
-
assert_no_links_match /foo=bar/
|
205
|
-
end
|
206
|
-
|
207
|
-
it "should override default route parameters" do
|
208
|
-
paginate({}, :params => { :controller => 'baz', :action => 'list' })
|
209
|
-
assert_links_match %r{\Wbaz/list\W}
|
210
|
-
end
|
211
|
-
|
212
|
-
it "should paginate with custom page parameter" do
|
213
|
-
paginate({ :page => 2 }, :param_name => :developers_page) do
|
214
|
-
assert_select 'a[href]', 4 do |elements|
|
215
|
-
validate_page_numbers [1,1,3,3], elements, :developers_page
|
216
|
-
end
|
217
|
-
end
|
218
|
-
end
|
219
|
-
|
220
|
-
it "should paginate with complex custom page parameter" do
|
221
|
-
request.params :developers => { :page => 2 }
|
222
|
-
|
223
|
-
paginate({ :page => 2 }, :param_name => 'developers[page]') do
|
224
|
-
assert_select 'a[href]', 4 do |links|
|
225
|
-
assert_links_match /\?developers\[page\]=\d+$/, links
|
226
|
-
validate_page_numbers [1,1,3,3], links, 'developers[page]'
|
227
|
-
end
|
228
|
-
end
|
229
|
-
end
|
230
|
-
|
231
|
-
it "should paginate with custom route page parameter" do
|
232
|
-
request.symbolized_path_parameters.update :controller => 'dummy', :action => nil
|
233
|
-
paginate :per_page => 2 do
|
234
|
-
assert_select 'a[href]', 6 do |links|
|
235
|
-
assert_links_match %r{/page/(\d+)$}, links, [2, 3, 4, 5, 6, 2]
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
it "should paginate with custom route with dot separator page parameter" do
|
241
|
-
request.symbolized_path_parameters.update :controller => 'dummy', :action => 'dots'
|
242
|
-
paginate :per_page => 2 do
|
243
|
-
assert_select 'a[href]', 6 do |links|
|
244
|
-
assert_links_match %r{/page\.(\d+)$}, links, [2, 3, 4, 5, 6, 2]
|
245
|
-
end
|
246
|
-
end
|
247
|
-
end
|
248
|
-
|
249
|
-
it "should paginate with custom route and first page number implicit" do
|
250
|
-
request.symbolized_path_parameters.update :controller => 'ibocorp', :action => nil
|
251
|
-
paginate :page => 2, :per_page => 2 do
|
252
|
-
assert_select 'a[href]', 7 do |links|
|
253
|
-
assert_links_match %r{/ibocorp(?:/(\d+))?$}, links, [nil, nil, 3, 4, 5, 6, 3]
|
254
|
-
end
|
255
|
-
end
|
256
|
-
# Routes.recognize_path('/ibocorp/2').should == {:page=>'2', :action=>'index', :controller=>'ibocorp'}
|
257
|
-
# Routes.recognize_path('/ibocorp/foo').should == {:action=>'foo', :controller=>'ibocorp'}
|
258
|
-
end
|
259
|
-
|
260
|
-
## internal hardcore stuff ##
|
261
|
-
|
262
|
-
it "should be able to guess the collection name" do
|
263
|
-
collection = mock
|
264
|
-
collection.expects(:total_pages).returns(1)
|
265
|
-
|
266
|
-
@template = '<%= will_paginate options %>'
|
267
|
-
controller.controller_name = 'developers'
|
268
|
-
assigns['developers'] = collection
|
269
|
-
|
270
|
-
paginate(nil)
|
271
|
-
end
|
272
|
-
|
273
|
-
it "should fail if the inferred collection is nil" do
|
274
|
-
@template = '<%= will_paginate options %>'
|
275
|
-
controller.controller_name = 'developers'
|
276
|
-
|
277
|
-
lambda {
|
278
|
-
paginate(nil)
|
279
|
-
}.should raise_error(ActionView::TemplateError, /@developers/)
|
280
|
-
end
|
281
|
-
|
282
|
-
## i18n
|
283
|
-
|
284
|
-
it "is able to translate previous/next labels" do
|
285
|
-
translation :will_paginate => {
|
286
|
-
:previous_label => 'Go back',
|
287
|
-
:next_label => 'Load more'
|
288
|
-
}
|
289
|
-
|
290
|
-
paginate do |pagination|
|
291
|
-
assert_select 'span.disabled:first-child', 'Go back'
|
292
|
-
assert_select 'a[rel=next]', 'Load more'
|
293
|
-
end
|
294
|
-
end
|
295
|
-
|
296
|
-
it "renders using ActionView helpers on a custom object" do
|
297
|
-
helper = Object.new
|
298
|
-
class << helper
|
299
|
-
attr_reader :controller
|
300
|
-
include ActionView::Helpers::UrlHelper
|
301
|
-
include Routes.url_helpers
|
302
|
-
include WillPaginate::ActionView
|
303
|
-
end
|
304
|
-
helper.default_url_options[:controller] = 'dummy'
|
305
|
-
|
306
|
-
collection = WillPaginate::Collection.new(2, 1, 3)
|
307
|
-
@render_output = helper.will_paginate(collection)
|
308
|
-
|
309
|
-
assert_select 'a[href]', 4 do |links|
|
310
|
-
urls = links.map {|l| l['href'] }.uniq
|
311
|
-
urls.should == ['/dummy/page/1', '/dummy/page/3']
|
312
|
-
end
|
313
|
-
end
|
314
|
-
|
315
|
-
it "renders using ActionDispatch helper on a custom object" do
|
316
|
-
helper = Object.new
|
317
|
-
class << helper
|
318
|
-
include ActionDispatch::Routing::UrlFor
|
319
|
-
include Routes.url_helpers
|
320
|
-
include WillPaginate::ActionView
|
321
|
-
end
|
322
|
-
helper.default_url_options[:host] = 'example.com'
|
323
|
-
helper.default_url_options[:controller] = 'dummy'
|
324
|
-
# helper.default_url_options[:only_path] = true
|
325
|
-
|
326
|
-
collection = WillPaginate::Collection.new(2, 1, 3)
|
327
|
-
@render_output = helper.will_paginate(collection)
|
328
|
-
|
329
|
-
assert_select 'a[href]', 4 do |links|
|
330
|
-
urls = links.map {|l| l['href'] }.uniq
|
331
|
-
urls.should == ['http://example.com/dummy/page/1', 'http://example.com/dummy/page/3']
|
332
|
-
end
|
333
|
-
end
|
334
|
-
|
335
|
-
private
|
336
|
-
|
337
|
-
def translation(data)
|
338
|
-
I18n.available_locales # triggers loading existing translations
|
339
|
-
I18n.backend.store_translations(:en, data)
|
340
|
-
end
|
341
|
-
end
|
342
|
-
|
343
|
-
class AdditionalLinkAttributesRenderer < WillPaginate::ActionView::LinkRenderer
|
344
|
-
def initialize(link_attributes = nil)
|
345
|
-
super()
|
346
|
-
@additional_link_attributes = link_attributes || { :default => 'true' }
|
347
|
-
end
|
348
|
-
|
349
|
-
def link(text, target, attributes = {})
|
350
|
-
super(text, target, attributes.merge(@additional_link_attributes))
|
351
|
-
end
|
352
|
-
end
|
353
|
-
|
354
|
-
class DummyController
|
355
|
-
attr_reader :request
|
356
|
-
attr_accessor :controller_name
|
357
|
-
|
358
|
-
include ActionController::UrlFor
|
359
|
-
include Routes.url_helpers
|
360
|
-
|
361
|
-
def initialize
|
362
|
-
@request = DummyRequest.new
|
363
|
-
end
|
364
|
-
|
365
|
-
def params
|
366
|
-
@request.params
|
367
|
-
end
|
368
|
-
|
369
|
-
def env
|
370
|
-
{}
|
371
|
-
end
|
372
|
-
|
373
|
-
def _prefixes
|
374
|
-
[]
|
375
|
-
end
|
376
|
-
end
|
377
|
-
|
378
|
-
class IbocorpController < DummyController
|
379
|
-
end
|
380
|
-
|
381
|
-
class DummyRequest
|
382
|
-
attr_accessor :symbolized_path_parameters
|
383
|
-
|
384
|
-
def initialize
|
385
|
-
@get = true
|
386
|
-
@params = {}
|
387
|
-
@symbolized_path_parameters = { :controller => 'foo', :action => 'bar' }
|
388
|
-
end
|
389
|
-
|
390
|
-
def get?
|
391
|
-
@get
|
392
|
-
end
|
393
|
-
|
394
|
-
def post
|
395
|
-
@get = false
|
396
|
-
end
|
397
|
-
|
398
|
-
def relative_url_root
|
399
|
-
''
|
400
|
-
end
|
401
|
-
|
402
|
-
def script_name
|
403
|
-
''
|
404
|
-
end
|
405
|
-
|
406
|
-
def params(more = nil)
|
407
|
-
@params.update(more) if more
|
408
|
-
@params
|
409
|
-
end
|
410
|
-
|
411
|
-
def host_with_port
|
412
|
-
'example.com'
|
413
|
-
end
|
414
|
-
alias host host_with_port
|
415
|
-
|
416
|
-
def optional_port
|
417
|
-
''
|
418
|
-
end
|
419
|
-
|
420
|
-
def protocol
|
421
|
-
'http:'
|
422
|
-
end
|
423
|
-
end
|
@@ -1,130 +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
|
-
end
|
14
|
-
|
15
|
-
before(:each) do
|
16
|
-
I18n.reload!
|
17
|
-
end
|
18
|
-
|
19
|
-
include WillPaginate::ViewHelpers
|
20
|
-
|
21
|
-
describe "will_paginate" do
|
22
|
-
it "should render" do
|
23
|
-
collection = WillPaginate::Collection.new(1, 2, 4)
|
24
|
-
renderer = mock 'Renderer'
|
25
|
-
renderer.expects(:prepare).with(collection, instance_of(Hash), self)
|
26
|
-
renderer.expects(:to_html).returns('<PAGES>')
|
27
|
-
|
28
|
-
will_paginate(collection, :renderer => renderer).should == '<PAGES>'
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should return nil for single-page collections" do
|
32
|
-
collection = mock 'Collection', :total_pages => 1
|
33
|
-
will_paginate(collection).should be_nil
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
describe "pagination_options" do
|
38
|
-
let(:pagination_options) { WillPaginate::ViewHelpers.pagination_options }
|
39
|
-
|
40
|
-
it "deprecates setting :renderer" do
|
41
|
-
begin
|
42
|
-
lambda {
|
43
|
-
pagination_options[:renderer] = 'test'
|
44
|
-
}.should have_deprecation("pagination_options[:renderer] shouldn't be set")
|
45
|
-
ensure
|
46
|
-
pagination_options.delete :renderer
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
describe "page_entries_info" do
|
52
|
-
before :all do
|
53
|
-
@array = ('a'..'z').to_a
|
54
|
-
end
|
55
|
-
|
56
|
-
def info(params, options = {})
|
57
|
-
collection = Hash === params ? @array.paginate(params) : params
|
58
|
-
page_entries_info collection, {:html => false}.merge(options)
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should display middle results and total count" do
|
62
|
-
info(:page => 2, :per_page => 5).should == "Displaying strings 6 - 10 of 26 in total"
|
63
|
-
end
|
64
|
-
|
65
|
-
it "uses translation if available" do
|
66
|
-
translation :will_paginate => {
|
67
|
-
:page_entries_info => {:multi_page => 'Showing %{from} - %{to}'}
|
68
|
-
}
|
69
|
-
info(:page => 2, :per_page => 5).should == "Showing 6 - 10"
|
70
|
-
end
|
71
|
-
|
72
|
-
it "uses specific translation if available" do
|
73
|
-
translation :will_paginate => {
|
74
|
-
:page_entries_info => {:multi_page => 'Showing %{from} - %{to}'},
|
75
|
-
:string => { :page_entries_info => {:multi_page => 'Strings %{from} to %{to}'} }
|
76
|
-
}
|
77
|
-
info(:page => 2, :per_page => 5).should == "Strings 6 to 10"
|
78
|
-
end
|
79
|
-
|
80
|
-
it "should output HTML by default" do
|
81
|
-
info({ :page => 2, :per_page => 5 }, :html => true).should ==
|
82
|
-
"Displaying strings <b>6 - 10</b> of <b>26</b> in total"
|
83
|
-
end
|
84
|
-
|
85
|
-
it "should display shortened end results" do
|
86
|
-
info(:page => 7, :per_page => 4).should include_phrase('strings 25 - 26')
|
87
|
-
end
|
88
|
-
|
89
|
-
it "should handle longer class names" do
|
90
|
-
collection = @array.paginate(:page => 2, :per_page => 5)
|
91
|
-
model = stub('Class', :name => 'ProjectType', :to_s => 'ProjectType')
|
92
|
-
collection.first.stubs(:class).returns(model)
|
93
|
-
info(collection).should include_phrase('project types')
|
94
|
-
end
|
95
|
-
|
96
|
-
it "should adjust output for single-page collections" do
|
97
|
-
info(('a'..'d').to_a.paginate(:page => 1, :per_page => 5)).should == "Displaying all 4 strings"
|
98
|
-
info(['a'].paginate(:page => 1, :per_page => 5)).should == "Displaying 1 string"
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should display 'no entries found' for empty collections" do
|
102
|
-
info([].paginate(:page => 1, :per_page => 5)).should == "No entries found"
|
103
|
-
end
|
104
|
-
|
105
|
-
it "uses model_name.human when available" do
|
106
|
-
name = stub('model name', :i18n_key => :flower_key)
|
107
|
-
name.expects(:human).with(:count => 1).returns('flower')
|
108
|
-
model = stub('Class', :model_name => name)
|
109
|
-
collection = [1].paginate(:page => 1)
|
110
|
-
|
111
|
-
info(collection, :model => model).should == "Displaying 1 flower"
|
112
|
-
end
|
113
|
-
|
114
|
-
it "uses custom translation instead of model_name.human" do
|
115
|
-
name = stub('model name', :i18n_key => :flower_key)
|
116
|
-
name.expects(:human).never
|
117
|
-
model = stub('Class', :model_name => name)
|
118
|
-
translation :will_paginate => {:models => {:flower_key => 'tulip'}}
|
119
|
-
collection = [1].paginate(:page => 1)
|
120
|
-
|
121
|
-
info(collection, :model => model).should == "Displaying 1 tulip"
|
122
|
-
end
|
123
|
-
|
124
|
-
private
|
125
|
-
|
126
|
-
def translation(data)
|
127
|
-
I18n.backend.store_translations(:en, data)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
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
|