will_paginate 3.1.6 → 4.0.0

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