will_paginate 2.3.16 → 3.0.pre

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.
Files changed (83) hide show
  1. data/CHANGELOG.rdoc +24 -80
  2. data/LICENSE +1 -1
  3. data/README.rdoc +125 -0
  4. data/Rakefile +26 -22
  5. data/lib/will_paginate/array.rb +25 -8
  6. data/lib/will_paginate/collection.rb +15 -28
  7. data/lib/will_paginate/core_ext.rb +26 -0
  8. data/lib/will_paginate/deprecation.rb +50 -0
  9. data/lib/will_paginate/finders/active_record.rb +158 -0
  10. data/lib/will_paginate/finders/active_resource.rb +51 -0
  11. data/lib/will_paginate/finders/base.rb +112 -0
  12. data/lib/will_paginate/finders/data_mapper.rb +30 -0
  13. data/lib/will_paginate/finders/sequel.rb +23 -0
  14. data/lib/will_paginate/finders.rb +9 -0
  15. data/lib/will_paginate/railtie.rb +26 -0
  16. data/lib/will_paginate/version.rb +5 -5
  17. data/lib/will_paginate/view_helpers/action_view.rb +142 -0
  18. data/lib/will_paginate/view_helpers/base.rb +126 -0
  19. data/lib/will_paginate/view_helpers/link_renderer.rb +130 -0
  20. data/lib/will_paginate/view_helpers/link_renderer_base.rb +83 -0
  21. data/lib/will_paginate/view_helpers/merb.rb +13 -0
  22. data/lib/will_paginate/view_helpers.rb +25 -436
  23. data/lib/will_paginate.rb +10 -84
  24. data/spec/collection_spec.rb +147 -0
  25. data/spec/console +8 -0
  26. data/spec/console_fixtures.rb +8 -0
  27. data/spec/database.yml +22 -0
  28. data/spec/finders/active_record_spec.rb +377 -0
  29. data/spec/finders/active_resource_spec.rb +52 -0
  30. data/spec/finders/activerecord_test_connector.rb +114 -0
  31. data/spec/finders/data_mapper_spec.rb +62 -0
  32. data/spec/finders/data_mapper_test_connector.rb +20 -0
  33. data/spec/finders/sequel_spec.rb +53 -0
  34. data/spec/finders/sequel_test_connector.rb +9 -0
  35. data/spec/finders_spec.rb +76 -0
  36. data/{test → spec}/fixtures/developer.rb +2 -3
  37. data/{test → spec}/fixtures/project.rb +2 -6
  38. data/{test → spec}/fixtures/projects.yml +1 -1
  39. data/{test → spec}/fixtures/reply.rb +1 -1
  40. data/spec/fixtures/topic.rb +7 -0
  41. data/spec/rcov.opts +2 -0
  42. data/spec/spec.opts +2 -0
  43. data/spec/spec_helper.rb +74 -0
  44. data/spec/tasks.rake +60 -0
  45. data/spec/view_helpers/action_view_spec.rb +345 -0
  46. data/spec/view_helpers/base_spec.rb +64 -0
  47. data/spec/view_helpers/link_renderer_base_spec.rb +84 -0
  48. data/spec/view_helpers/view_example_group.rb +103 -0
  49. metadata +60 -52
  50. data/README.md +0 -53
  51. data/lib/will_paginate/finder.rb +0 -264
  52. data/lib/will_paginate/i18n.rb +0 -29
  53. data/lib/will_paginate/locale/en.yml +0 -33
  54. data/lib/will_paginate/named_scope.rb +0 -170
  55. data/lib/will_paginate/named_scope_patch.rb +0 -37
  56. data/lib/will_paginate/per_page.rb +0 -27
  57. data/test/ci.rb +0 -56
  58. data/test/collection_test.rb +0 -160
  59. data/test/console +0 -8
  60. data/test/database.yml +0 -16
  61. data/test/finder_test.rb +0 -520
  62. data/test/fixtures/topic.rb +0 -12
  63. data/test/gemfiles/Gemfile.1.2 +0 -13
  64. data/test/gemfiles/Gemfile.1.2.lock +0 -39
  65. data/test/gemfiles/Gemfile.2.0 +0 -16
  66. data/test/gemfiles/Gemfile.2.0.lock +0 -28
  67. data/test/gemfiles/Gemfile.2.1 +0 -16
  68. data/test/gemfiles/Gemfile.2.1.lock +0 -28
  69. data/test/gemfiles/Gemfile.2.2 +0 -16
  70. data/test/gemfiles/Gemfile.2.2.lock +0 -28
  71. data/test/helper.rb +0 -34
  72. data/test/lib/activerecord_test_case.rb +0 -38
  73. data/test/lib/activerecord_test_connector.rb +0 -86
  74. data/test/lib/load_fixtures.rb +0 -12
  75. data/test/lib/view_test_process.rb +0 -186
  76. data/test/view_test.rb +0 -373
  77. /data/{test → spec}/fixtures/admin.rb +0 -0
  78. /data/{test → spec}/fixtures/developers_projects.yml +0 -0
  79. /data/{test → spec}/fixtures/replies.yml +0 -0
  80. /data/{test → spec}/fixtures/schema.rb +0 -0
  81. /data/{test → spec}/fixtures/topics.yml +0 -0
  82. /data/{test → spec}/fixtures/user.rb +0 -0
  83. /data/{test → spec}/fixtures/users.yml +0 -0
@@ -0,0 +1,345 @@
1
+ require 'spec_helper'
2
+ require 'active_support/rescuable' # needed for Ruby 1.9.1
3
+ require 'action_controller'
4
+ require 'view_helpers/view_example_group'
5
+ require 'will_paginate/view_helpers/action_view'
6
+ require 'will_paginate/collection'
7
+
8
+ ActionController::Routing::Routes.draw do |map|
9
+ map.connect 'dummy/page/:page', :controller => 'dummy'
10
+ map.connect 'dummy/dots/page.:page', :controller => 'dummy', :action => 'dots'
11
+ map.connect 'ibocorp/:page', :controller => 'ibocorp',
12
+ :requirements => { :page => /\d+/ },
13
+ :defaults => { :page => 1 }
14
+
15
+ map.connect ':controller/:action/:id'
16
+ end
17
+
18
+ describe WillPaginate::ViewHelpers::ActionView do
19
+ before(:each) do
20
+ @assigns = {}
21
+ @controller = DummyController.new
22
+ @request = @controller.request
23
+ @template = '<%= will_paginate collection, options %>'
24
+ end
25
+
26
+ attr_reader :assigns, :controller, :request
27
+
28
+ def render(locals)
29
+ @view = ActionView::Base.new([], @assigns, @controller)
30
+ @view.request = @request
31
+ @view.render(:inline => @template, :locals => locals)
32
+ end
33
+
34
+ ## basic pagination ##
35
+
36
+ it "should render" do
37
+ paginate do |pagination|
38
+ assert_select 'a[href]', 3 do |elements|
39
+ validate_page_numbers [2,3,2], elements
40
+ assert_select elements.last, ':last-child', "Next &#8594;"
41
+ end
42
+ assert_select 'span', 1
43
+ assert_select 'span.disabled:first-child', '&#8592; Previous'
44
+ assert_select 'em', '1'
45
+ pagination.first.inner_text.should == '&#8592; Previous 1 2 3 Next &#8594;'
46
+ end
47
+ end
48
+
49
+ it "should render nothing when there is only 1 page" do
50
+ paginate(:per_page => 30).should be_empty
51
+ end
52
+
53
+ it "should paginate with options" do
54
+ paginate({ :page => 2 }, :class => 'will_paginate', :previous_label => 'Prev', :next_label => 'Next') do
55
+ assert_select 'a[href]', 4 do |elements|
56
+ validate_page_numbers [1,1,3,3], elements
57
+ # test rel attribute values:
58
+ assert_select elements[1], 'a', '1' do |link|
59
+ link.first['rel'].should == 'prev start'
60
+ end
61
+ assert_select elements.first, 'a', "Prev" do |link|
62
+ link.first['rel'].should == 'prev start'
63
+ end
64
+ assert_select elements.last, 'a', "Next" do |link|
65
+ link.first['rel'].should == 'next'
66
+ end
67
+ end
68
+ assert_select 'em', '2'
69
+ end
70
+ end
71
+
72
+ it "should paginate using a custom renderer class" do
73
+ paginate({}, :renderer => AdditionalLinkAttributesRenderer) do
74
+ assert_select 'a[default=true]', 3
75
+ end
76
+ end
77
+
78
+ it "should paginate using a custom renderer instance" do
79
+ renderer = WillPaginate::ViewHelpers::LinkRenderer.new
80
+ def renderer.gap() '<span class="my-gap">~~</span>' end
81
+
82
+ paginate({ :per_page => 2 }, :inner_window => 0, :outer_window => 0, :renderer => renderer) do
83
+ assert_select 'span.my-gap', '~~'
84
+ end
85
+
86
+ renderer = AdditionalLinkAttributesRenderer.new(:title => 'rendered')
87
+ paginate({}, :renderer => renderer) do
88
+ assert_select 'a[title=rendered]', 3
89
+ end
90
+ end
91
+
92
+ it "should have classnames on previous/next links" do
93
+ paginate do |pagination|
94
+ assert_select 'span.disabled.previous_page:first-child'
95
+ assert_select 'a.next_page[href]:last-child'
96
+ end
97
+ end
98
+
99
+ it "should warn about :prev_label being deprecated" do
100
+ lambda {
101
+ paginate({ :page => 2 }, :prev_label => 'Deprecated') do
102
+ assert_select 'a[href]:first-child', 'Deprecated'
103
+ end
104
+ }.should have_deprecation
105
+ end
106
+
107
+ it "should match expected markup" do
108
+ paginate
109
+ expected = <<-HTML
110
+ <div class="pagination"><span class="previous_page disabled">&#8592; Previous</span>
111
+ <em>1</em>
112
+ <a href="/foo/bar?page=2" rel="next">2</a>
113
+ <a href="/foo/bar?page=3">3</a>
114
+ <a href="/foo/bar?page=2" class="next_page" rel="next">Next &#8594;</a></div>
115
+ HTML
116
+ expected.strip!.gsub!(/\s{2,}/, ' ')
117
+ expected_dom = HTML::Document.new(expected).root
118
+
119
+ html_document.root.should == expected_dom
120
+ end
121
+
122
+ it "should output escaped URLs" do
123
+ paginate({:page => 1, :per_page => 1, :total_entries => 2},
124
+ :page_links => false, :params => { :tag => '<br>' })
125
+
126
+ assert_select 'a[href]', 1 do |links|
127
+ query = links.first['href'].split('?', 2)[1]
128
+ query.split('&amp;').sort.should == %w(page=2 tag=%3Cbr%3E)
129
+ end
130
+ end
131
+
132
+ ## advanced options for pagination ##
133
+
134
+ it "should be able to render without container" do
135
+ paginate({}, :container => false)
136
+ assert_select 'div.pagination', 0, 'main DIV present when it shouldn\'t'
137
+ assert_select 'a[href]', 3
138
+ end
139
+
140
+ it "should be able to render without page links" do
141
+ paginate({ :page => 2 }, :page_links => false) do
142
+ assert_select 'a[href]', 2 do |elements|
143
+ validate_page_numbers [1,3], elements
144
+ end
145
+ end
146
+ end
147
+
148
+ it "should have magic HTML ID for the container" do
149
+ paginate do |div|
150
+ div.first['id'].should be_nil
151
+ end
152
+
153
+ # magic ID
154
+ paginate({}, :id => true) do |div|
155
+ div.first['id'].should == 'fixnums_pagination'
156
+ end
157
+
158
+ # explicit ID
159
+ paginate({}, :id => 'custom_id') do |div|
160
+ div.first['id'].should == 'custom_id'
161
+ end
162
+ end
163
+
164
+ ## other helpers ##
165
+
166
+ it "should render a paginated section" do
167
+ @template = <<-ERB
168
+ <% paginated_section collection, options do %>
169
+ <%= content_tag :div, '', :id => "developers" %>
170
+ <% end %>
171
+ ERB
172
+
173
+ paginate
174
+ assert_select 'div.pagination', 2
175
+ assert_select 'div.pagination + div#developers', 1
176
+ end
177
+
178
+ ## parameter handling in page links ##
179
+
180
+ it "should preserve parameters on GET" do
181
+ request.params :foo => { :bar => 'baz' }
182
+ paginate
183
+ assert_links_match /foo\[bar\]=baz/
184
+ end
185
+
186
+ it "should not preserve parameters on POST" do
187
+ request.post
188
+ request.params :foo => 'bar'
189
+ paginate
190
+ assert_no_links_match /foo=bar/
191
+ end
192
+
193
+ it "should add additional parameters to links" do
194
+ paginate({}, :params => { :foo => 'bar' })
195
+ assert_links_match /foo=bar/
196
+ end
197
+
198
+ it "should add anchor parameter" do
199
+ paginate({}, :params => { :anchor => 'anchor' })
200
+ assert_links_match /#anchor$/
201
+ end
202
+
203
+ it "should remove arbitrary parameters" do
204
+ request.params :foo => 'bar'
205
+ paginate({}, :params => { :foo => nil })
206
+ assert_no_links_match /foo=bar/
207
+ end
208
+
209
+ it "should override default route parameters" do
210
+ paginate({}, :params => { :controller => 'baz', :action => 'list' })
211
+ assert_links_match %r{\Wbaz/list\W}
212
+ end
213
+
214
+ it "should paginate with custom page parameter" do
215
+ paginate({ :page => 2 }, :param_name => :developers_page) do
216
+ assert_select 'a[href]', 4 do |elements|
217
+ validate_page_numbers [1,1,3,3], elements, :developers_page
218
+ end
219
+ end
220
+ end
221
+
222
+ it "should paginate with complex custom page parameter" do
223
+ request.params :developers => { :page => 2 }
224
+
225
+ paginate({ :page => 2 }, :param_name => 'developers[page]') do
226
+ assert_select 'a[href]', 4 do |links|
227
+ assert_links_match /\?developers\[page\]=\d+$/, links
228
+ validate_page_numbers [1,1,3,3], links, 'developers[page]'
229
+ end
230
+ end
231
+ end
232
+
233
+ it "should paginate with custom route page parameter" do
234
+ request.symbolized_path_parameters.update :controller => 'dummy', :action => nil
235
+ paginate :per_page => 2 do
236
+ assert_select 'a[href]', 6 do |links|
237
+ assert_links_match %r{/page/(\d+)$}, links, [2, 3, 4, 5, 6, 2]
238
+ end
239
+ end
240
+ end
241
+
242
+ it "should paginate with custom route with dot separator page parameter" do
243
+ request.symbolized_path_parameters.update :controller => 'dummy', :action => 'dots'
244
+ paginate :per_page => 2 do
245
+ assert_select 'a[href]', 6 do |links|
246
+ assert_links_match %r{/page\.(\d+)$}, links, [2, 3, 4, 5, 6, 2]
247
+ end
248
+ end
249
+ end
250
+
251
+ it "should paginate with custom route and first page number implicit" do
252
+ request.symbolized_path_parameters.update :controller => 'ibocorp', :action => nil
253
+ paginate :page => 2, :per_page => 2 do
254
+ assert_select 'a[href]', 7 do |links|
255
+ assert_links_match %r{/ibocorp(?:/(\d+))?$}, links, [nil, nil, 3, 4, 5, 6, 3]
256
+ end
257
+ end
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
+ end
282
+
283
+ class AdditionalLinkAttributesRenderer < WillPaginate::ViewHelpers::LinkRenderer
284
+ def initialize(link_attributes = nil)
285
+ super()
286
+ @additional_link_attributes = link_attributes || { :default => 'true' }
287
+ end
288
+
289
+ def link(text, target, attributes = {})
290
+ super(text, target, attributes.merge(@additional_link_attributes))
291
+ end
292
+ end
293
+
294
+ class DummyController
295
+ attr_reader :request
296
+ attr_accessor :controller_name
297
+
298
+ def initialize
299
+ @request = DummyRequest.new
300
+ @url = ActionController::UrlRewriter.new(@request, @request.params)
301
+ end
302
+
303
+ def params
304
+ @request.params
305
+ end
306
+
307
+ def url_for(params)
308
+ @url.rewrite(params)
309
+ end
310
+ end
311
+
312
+ class DummyRequest
313
+ attr_accessor :symbolized_path_parameters
314
+
315
+ def initialize
316
+ @get = true
317
+ @params = {}
318
+ @symbolized_path_parameters = { :controller => 'foo', :action => 'bar' }
319
+ end
320
+
321
+ def get?
322
+ @get
323
+ end
324
+
325
+ def post
326
+ @get = false
327
+ end
328
+
329
+ def relative_url_root
330
+ ''
331
+ end
332
+
333
+ def params(more = nil)
334
+ @params.update(more) if more
335
+ @params
336
+ end
337
+
338
+ def host_with_port
339
+ 'example.com'
340
+ end
341
+
342
+ def protocol
343
+ 'http:'
344
+ end
345
+ end
@@ -0,0 +1,64 @@
1
+ require 'spec_helper'
2
+ require 'will_paginate/view_helpers/base'
3
+ require 'will_paginate/array'
4
+
5
+ describe WillPaginate::ViewHelpers::Base do
6
+
7
+ include WillPaginate::ViewHelpers::Base
8
+
9
+ describe "will_paginate" do
10
+ it "should render" do
11
+ collection = WillPaginate::Collection.new(1, 2, 4)
12
+ renderer = mock 'Renderer'
13
+ renderer.expects(:prepare).with(collection, instance_of(Hash), self)
14
+ renderer.expects(:to_html).returns('<PAGES>')
15
+
16
+ will_paginate(collection, :renderer => renderer).should == '<PAGES>'
17
+ end
18
+
19
+ it "should return nil for single-page collections" do
20
+ collection = mock 'Collection', :total_pages => 1
21
+ will_paginate(collection).should be_nil
22
+ end
23
+ end
24
+
25
+ describe "page_entries_info" do
26
+ before :all do
27
+ @array = ('a'..'z').to_a
28
+ end
29
+
30
+ def info(params, options = {})
31
+ options[:html] ||= false unless options.key?(:html) and options[:html].nil?
32
+ collection = Hash === params ? @array.paginate(params) : params
33
+ page_entries_info collection, options
34
+ end
35
+
36
+ it "should display middle results and total count" do
37
+ info(:page => 2, :per_page => 5).should == "Displaying strings 6 - 10 of 26 in total"
38
+ end
39
+
40
+ it "should output HTML by default" do
41
+ info({ :page => 2, :per_page => 5 }, :html => nil).should ==
42
+ "Displaying strings <b>6&nbsp;-&nbsp;10</b> of <b>26</b> in total"
43
+ end
44
+
45
+ it "should display shortened end results" do
46
+ info(:page => 7, :per_page => 4).should include_phrase('strings 25 - 26')
47
+ end
48
+
49
+ it "should handle longer class names" do
50
+ collection = @array.paginate(:page => 2, :per_page => 5)
51
+ collection.first.stubs(:class).returns(mock('Class', :name => 'ProjectType'))
52
+ info(collection).should include_phrase('project types')
53
+ end
54
+
55
+ it "should adjust output for single-page collections" do
56
+ info(('a'..'d').to_a.paginate(:page => 1, :per_page => 5)).should == "Displaying all 4 strings"
57
+ info(['a'].paginate(:page => 1, :per_page => 5)).should == "Displaying 1 string"
58
+ end
59
+
60
+ it "should display 'no entries found' for empty collections" do
61
+ info([].paginate(:page => 1, :per_page => 5)).should == "No entries found"
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,84 @@
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 = WillPaginate::ViewHelpers::LinkRendererBase.new
9
+ end
10
+
11
+ it "should raise error when unprepared" do
12
+ lambda {
13
+ @renderer.send :param_name
14
+ }.should raise_error
15
+ end
16
+
17
+ it "should prepare with collection and options" do
18
+ prepare({}, :param_name => 'mypage')
19
+ @renderer.send(:current_page).should == 1
20
+ @renderer.send(:param_name).should == 'mypage'
21
+ end
22
+
23
+ it "should have total_pages accessor" do
24
+ prepare :total_pages => 42
25
+ lambda {
26
+ @renderer.send(:total_pages).should == 42
27
+ }.should_not have_deprecation
28
+ end
29
+
30
+ it "should clear old cached values when prepared" do
31
+ prepare({ :total_pages => 1 }, :param_name => 'foo')
32
+ @renderer.send(:total_pages).should == 1
33
+ @renderer.send(:param_name).should == 'foo'
34
+ # prepare with different object and options:
35
+ prepare({ :total_pages => 2 }, :param_name => 'bar')
36
+ @renderer.send(:total_pages).should == 2
37
+ @renderer.send(:param_name).should == 'bar'
38
+ end
39
+
40
+ it "should have pagination definition" do
41
+ prepare({ :total_pages => 1 }, :page_links => true)
42
+ @renderer.pagination.should == [:previous_page, 1, :next_page]
43
+ end
44
+
45
+ describe "visible page numbers" do
46
+ it "should calculate windowed visible links" do
47
+ prepare({ :page => 6, :total_pages => 11 }, :inner_window => 1, :outer_window => 1)
48
+ showing_pages 1, 2, :gap, 5, 6, 7, :gap, 10, 11
49
+ end
50
+
51
+ it "should eliminate small gaps" do
52
+ prepare({ :page => 6, :total_pages => 11 }, :inner_window => 2, :outer_window => 1)
53
+ # pages 4 and 8 appear instead of the gap
54
+ showing_pages 1..11
55
+ end
56
+
57
+ it "should support having no windows at all" do
58
+ prepare({ :page => 4, :total_pages => 7 }, :inner_window => 0, :outer_window => 0)
59
+ showing_pages 1, :gap, 4, :gap, 7
60
+ end
61
+
62
+ it "should adjust upper limit if lower is out of bounds" do
63
+ prepare({ :page => 1, :total_pages => 10 }, :inner_window => 2, :outer_window => 1)
64
+ showing_pages 1, 2, 3, 4, 5, :gap, 9, 10
65
+ end
66
+
67
+ it "should adjust lower limit if upper is out of bounds" do
68
+ prepare({ :page => 10, :total_pages => 10 }, :inner_window => 2, :outer_window => 1)
69
+ showing_pages 1, 2, :gap, 6, 7, 8, 9, 10
70
+ end
71
+
72
+ def showing_pages(*pages)
73
+ pages = pages.first.to_a if Array === pages.first or Range === pages.first
74
+ @renderer.send(:windowed_page_numbers).should == pages
75
+ end
76
+ end
77
+
78
+ protected
79
+
80
+ def prepare(collection_options, options = {})
81
+ @renderer.prepare(collection(collection_options), options)
82
+ end
83
+
84
+ end
@@ -0,0 +1,103 @@
1
+ require 'action_dispatch/testing/assertions'
2
+
3
+ class ViewExampleGroup < Spec::Example::ExampleGroup
4
+
5
+ include ActionDispatch::Assertions::SelectorAssertions
6
+
7
+ def assert(value, message)
8
+ raise message unless value
9
+ end
10
+
11
+ def paginate(collection = {}, options = {}, &block)
12
+ if collection.instance_of? Hash
13
+ page_options = { :page => 1, :total_entries => 11, :per_page => 4 }.merge(collection)
14
+ collection = [1].paginate(page_options)
15
+ end
16
+
17
+ locals = { :collection => collection, :options => options }
18
+
19
+ @render_output = render(locals)
20
+ @html_document = nil
21
+
22
+ if block_given?
23
+ classname = options[:class] || WillPaginate::ViewHelpers.pagination_options[:class]
24
+ assert_select("div.#{classname}", 1, 'no main DIV', &block)
25
+ end
26
+
27
+ @render_output
28
+ end
29
+
30
+ def html_document
31
+ @html_document ||= HTML::Document.new(@render_output, true, false)
32
+ end
33
+
34
+ def response_from_page_or_rjs
35
+ html_document.root
36
+ end
37
+
38
+ def validate_page_numbers(expected, links, param_name = :page)
39
+ param_pattern = /\W#{Regexp.escape(param_name.to_s)}=([^&]*)/
40
+
41
+ links.map { |e|
42
+ e['href'] =~ param_pattern
43
+ $1 ? $1.to_i : $1
44
+ }.should == expected
45
+ end
46
+
47
+ def assert_links_match(pattern, links = nil, numbers = nil)
48
+ links ||= assert_select 'div.pagination a[href]' do |elements|
49
+ elements
50
+ end
51
+
52
+ pages = [] if numbers
53
+
54
+ links.each do |el|
55
+ el['href'].should =~ pattern
56
+ if numbers
57
+ el['href'] =~ pattern
58
+ pages << ($1.nil?? nil : $1.to_i)
59
+ end
60
+ end
61
+
62
+ pages.should == numbers if numbers
63
+ end
64
+
65
+ def assert_no_links_match(pattern)
66
+ assert_select 'div.pagination a[href]' do |elements|
67
+ elements.each do |el|
68
+ el['href'] !~ pattern
69
+ end
70
+ end
71
+ end
72
+
73
+ def build_message(message, pattern, *args)
74
+ built_message = pattern.dup
75
+ for value in args
76
+ built_message.sub! '?', value.inspect
77
+ end
78
+ built_message
79
+ end
80
+
81
+ end
82
+
83
+ Spec::Example::ExampleGroupFactory.register(:view_helpers, ViewExampleGroup)
84
+
85
+ module HTML
86
+ Node.class_eval do
87
+ def inner_text
88
+ children.map(&:inner_text).join('')
89
+ end
90
+ end
91
+
92
+ Text.class_eval do
93
+ def inner_text
94
+ self.to_s
95
+ end
96
+ end
97
+
98
+ Tag.class_eval do
99
+ def inner_text
100
+ childless?? '' : super
101
+ end
102
+ end
103
+ end