public
Fork of mislav/will_paginate
Description: Most awesome pagination solution for Rails
Homepage: http://github.com/mislav/will_paginate/wikis
Clone URL: git://github.com/technoweenie/will_paginate.git
Search Repo:
finish transitioning pagination_test.rb to view_test.rb; caught and fixed 
an edge case in the process
mislav (author)
Sun Apr 06 14:03:42 -0700 2008
commit  8fe31cbe2c2c0c488cc7950c4b1ef498fcdc947e
tree    ee68108f1e63ac8326f22bfbf0e6f4650c2f908b
parent  6d64899d9405486039b9ba9ab8f3df1b920a777a
...
260
261
262
 
263
264
265
266
...
303
304
305
306
307
308
309
 
 
 
 
 
 
 
310
 
 
311
312
313
...
260
261
262
263
264
265
266
267
...
304
305
306
 
 
 
 
307
308
309
310
311
312
313
314
315
316
317
318
319
0
@@ -260,6 +260,7 @@
0
         
0
         if param_name.index(/[^\w-]/)
0
           page_param = if defined? CGIMethods
0
+ # Rails 1.2
0
             CGIMethods.parse_query_parameters(param_name)
0
           else
0
             ActionController::AbstractRequest.parse_query_parameters(param_name)
0
0
@@ -303,11 +304,16 @@
0
       other.each do |key, value|
0
         key = key.to_s
0
         existing = target[key]
0
- if existing.is_a?(Hash) and value.is_a?(Hash)
0
- stringified_merge(existing, value)
0
- else
0
- target[key] = value
0
+
0
+ if value.is_a?(Hash)
0
+ target[key] = existing = {} if existing.nil?
0
+ if existing.is_a?(Hash)
0
+ stringified_merge(existing, value)
0
+ return
0
+ end
0
         end
0
+
0
+ target[key] = value
0
       end
0
     end
0
   end
...
13
14
15
 
16
17
18
...
30
31
32
 
 
 
 
 
33
34
35
...
38
39
40
41
 
42
43
44
...
13
14
15
16
17
18
19
...
31
32
33
34
35
36
37
38
39
40
41
...
44
45
46
 
47
48
49
50
0
@@ -13,6 +13,7 @@
0
 class DummyRequest
0
   def initialize
0
     @get = true
0
+ @params = {}
0
   end
0
   
0
   def get?
0
@@ -30,6 +31,11 @@
0
   def relative_url_root
0
     ''
0
   end
0
+
0
+ def params(more = nil)
0
+ @params.update(more) if more
0
+ @params
0
+ end
0
 end
0
 
0
 class DummyController
0
@@ -38,7 +44,7 @@
0
   
0
   def initialize
0
     @request = DummyRequest.new
0
- @url = ActionController::UrlRewriter.new(@request, {})
0
+ @url = ActionController::UrlRewriter.new(@request, @request.params)
0
   end
0
   
0
   def url_for(params)
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,112 +1 @@
0
-require 'helper'
0
-require 'lib/view_test_process'
0
-
0
-class PaginationTest < Test::Unit::TestCase
0
-
0
- class DevelopersController < ActionController::Base
0
- def list_developers
0
- @options = session[:wp] || {}
0
-
0
- @developers = (1..11).to_a.paginate(
0
- :page => params[@options[:param_name] || :page] || 1,
0
- :per_page => params[:per_page] || 4
0
- )
0
-
0
- render :inline => '<%= will_paginate @developers, @options %>'
0
- end
0
-
0
- protected
0
- def rescue_errors(e) raise e end
0
- def rescue_action(e) raise e end
0
- end
0
-
0
- def setup
0
- @controller = DevelopersController.new
0
- @request = ActionController::TestRequest.new
0
- @response = ActionController::TestResponse.new
0
- super
0
- end
0
-
0
- def test_will_paginate_preserves_parameters_on_get
0
- get :list_developers, :foo => { :bar => 'baz' }
0
- assert_links_match /foo%5Bbar%5D=baz/
0
- end
0
-
0
- def test_will_paginate_doesnt_preserve_parameters_on_post
0
- post :list_developers, :foo => 'bar'
0
- assert_no_links_match /foo=bar/
0
- end
0
-
0
- def test_adding_additional_parameters
0
- get :list_developers, {}, :wp => { :params => { :foo => 'bar' } }
0
- assert_links_match /foo=bar/
0
- end
0
-
0
- def test_removing_arbitrary_parameters
0
- get :list_developers, { :foo => 'bar' }, :wp => { :params => { :foo => nil } }
0
- assert_no_links_match /foo=bar/
0
- end
0
-
0
- def test_adding_additional_route_parameters
0
- get :list_developers, {}, :wp => { :params => { :controller => 'baz' } }
0
- assert_links_match %r{\Wbaz/list_developers\W}
0
- end
0
-
0
- def test_will_paginate_with_custom_page_param
0
- get :list_developers, { :developers_page => 2 }, :wp => { :param_name => :developers_page }
0
- assert_response :success
0
-
0
- entries = assigns :developers
0
- assert entries
0
- assert_equal 4, entries.size
0
-
0
- assert_select 'div.pagination', 1, 'no main DIV' do
0
- assert_select 'a[href]', 4 do |elements|
0
- validate_page_numbers [1,1,3,3], elements, :developers_page
0
- end
0
- assert_select 'span.current', entries.current_page.to_s
0
- end
0
- end
0
-
0
- def test_complex_custom_page_param
0
- get :list_developers, { :developers => {:page => 1} }, :wp => { :param_name => 'developers[page]' }
0
- entries = assigns :developers
0
-
0
- assert_links_match /\?developers%5Bpage%5D=\d+$/
0
-
0
- assert_select 'div.pagination', 1, 'no main DIV' do
0
- assert_select 'a[href]', 3 do |elements|
0
- validate_page_numbers [2,3,2], elements, 'developers[page]'
0
- end
0
- assert_select 'span.current', entries.current_page.to_s
0
- end
0
- end
0
-
0
-protected
0
-
0
- def validate_page_numbers expected, links, param_name = :page
0
- param_pattern = /\W#{CGI.escape(param_name.to_s)}=([^&]*)/
0
-
0
- assert_equal(expected, links.map { |e|
0
- e['href'] =~ param_pattern
0
- $1 ? $1.to_i : $1
0
- })
0
- end
0
-
0
- def assert_links_match pattern
0
- assert_select 'div.pagination a[href]' do |elements|
0
- elements.each do |el|
0
- assert_match pattern, el['href']
0
- end
0
- end
0
- end
0
-
0
- def assert_no_links_match pattern
0
- assert_select 'div.pagination a[href]' do |elements|
0
- elements.each do |el|
0
- assert_no_match pattern, el['href']
0
- end
0
- end
0
- end
0
-end
...
12
13
14
15
16
17
 
18
19
 
 
20
21
22
...
55
56
57
 
 
58
59
60
61
...
88
89
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
93
94
95
96
97
98
...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
 
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
 
177
178
179
...
214
215
216
217
218
219
220
221
 
 
 
 
 
 
 
222
223
224
...
12
13
14
 
15
16
17
18
19
20
21
22
23
24
...
57
58
59
60
61
62
63
64
65
...
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
...
223
224
225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
 
227
228
229
230
231
232
233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
235
236
237
238
...
273
274
275
 
 
 
 
 
276
277
278
279
280
281
282
283
284
285
0
@@ -12,11 +12,13 @@
0
     @template = '<%= will_paginate collection, options %>'
0
     
0
     @view = ActionView::Base.new
0
- @view.assigns['_params'] = {}
0
     @view.assigns['controller'] = @controller
0
     @view.assigns['_request'] = @request
0
+ @view.assigns['_params'] = @request.params
0
   end
0
 
0
+ ## basic pagination ##
0
+
0
   def test_will_paginate
0
     paginate do |pagination|
0
       assert_select 'a[href]', 3 do |elements|
0
@@ -55,6 +57,8 @@
0
     end
0
   end
0
 
0
+ ## advanced options for pagination ##
0
+
0
   def test_will_paginate_without_container
0
     paginate({}, :container => false)
0
     assert_select 'div.pagination', 0, 'main DIV present when it shouldn\'t'
0
0
@@ -88,7 +92,102 @@
0
       end
0
     end
0
   end
0
+
0
+ def test_container_id
0
+ paginate do |div|
0
+ assert_nil div.first['id']
0
+ end
0
+
0
+ # magic ID
0
+ paginate({}, :id => true) do |div|
0
+ assert_equal 'fixnums_pagination', div.first['id']
0
+ end
0
+
0
+ # explicit ID
0
+ paginate({}, :id => 'custom_id') do |div|
0
+ assert_equal 'custom_id', div.first['id']
0
+ end
0
+ end
0
 
0
+ ## other helpers ##
0
+
0
+ def test_paginated_section
0
+ @template = <<-ERB
0
+ <% paginated_section collection, options do %>
0
+ <%= content_tag :div, '', :id => "developers" %>
0
+ <% end %>
0
+ ERB
0
+
0
+ paginate
0
+ assert_select 'div.pagination', 2
0
+ assert_select 'div.pagination + div#developers', 1
0
+ end
0
+
0
+ def test_page_entries_info
0
+ @template = '<%= page_entries_info collection %>'
0
+ array = ('a'..'z').to_a
0
+
0
+ paginate array.paginate(:page => 2, :per_page => 5)
0
+ assert_equal %{Displaying entries <b>6&nbsp;-&nbsp;10</b> of <b>26</b> in total},
0
+ @html_result
0
+
0
+ paginate array.paginate(:page => 7, :per_page => 4)
0
+ assert_equal %{Displaying entries <b>25&nbsp;-&nbsp;26</b> of <b>26</b> in total},
0
+ @html_result
0
+ end
0
+
0
+ ## parameter handling in page links ##
0
+
0
+ def test_will_paginate_preserves_parameters_on_get
0
+ @request.params :foo => { :bar => 'baz' }
0
+ paginate
0
+ assert_links_match /foo%5Bbar%5D=baz/
0
+ end
0
+
0
+ def test_will_paginate_doesnt_preserve_parameters_on_post
0
+ @request.post
0
+ @request.params :foo => 'bar'
0
+ paginate
0
+ assert_no_links_match /foo=bar/
0
+ end
0
+
0
+ def test_adding_additional_parameters
0
+ paginate({}, :params => { :foo => 'bar' })
0
+ assert_links_match /foo=bar/
0
+ end
0
+
0
+ def test_removing_arbitrary_parameters
0
+ @request.params :foo => 'bar'
0
+ paginate({}, :params => { :foo => nil })
0
+ assert_no_links_match /foo=bar/
0
+ end
0
+
0
+ def test_adding_additional_route_parameters
0
+ paginate({}, :params => { :controller => 'baz', :action => 'list' })
0
+ assert_links_match %r{\Wbaz/list\W}
0
+ end
0
+
0
+ def test_will_paginate_with_custom_page_param
0
+ paginate({ :page => 2 }, :param_name => :developers_page) do
0
+ assert_select 'a[href]', 4 do |elements|
0
+ validate_page_numbers [1,1,3,3], elements, :developers_page
0
+ end
0
+ end
0
+ end
0
+
0
+ def test_complex_custom_page_param
0
+ @request.params :developers => { :page => 2 }
0
+
0
+ paginate({ :page => 2 }, :param_name => 'developers[page]') do
0
+ assert_select 'a[href]', 4 do |links|
0
+ assert_links_match /\?developers%5Bpage%5D=\d+$/, links
0
+ validate_page_numbers [1,1,3,3], links, 'developers[page]'
0
+ end
0
+ end
0
+ end
0
+
0
+ ## internal hardcore stuff ##
0
+
0
   class LegacyCollection < WillPaginate::Collection
0
     alias :page_count :total_pages
0
     undef :total_pages
0
0
0
0
0
@@ -124,56 +223,16 @@
0
     end
0
     assert e.message.include?('@developers')
0
   end
0
-
0
- def test_container_id
0
- paginate do |div|
0
- assert_nil div.first['id']
0
- end
0
-
0
- # magic ID
0
- paginate({}, :id => true) do |div|
0
- assert_equal 'fixnums_pagination', div.first['id']
0
- end
0
-
0
- # explicit ID
0
- paginate({}, :id => 'custom_id') do |div|
0
- assert_equal 'custom_id', div.first['id']
0
- end
0
- end
0
 
0
- # only on Rails 2
0
   if ActionController::Base.respond_to? :rescue_responses
0
+ # only on Rails 2
0
     def test_rescue_response_hook_presence
0
       assert_equal :not_found,
0
         ActionController::Base.rescue_responses['WillPaginate::InvalidPage']
0
     end
0
   end
0
-
0
- def test_paginated_section
0
- @template = <<-ERB
0
- <% paginated_section collection, options do %>
0
- <%= content_tag :div, '', :id => "developers" %>
0
- <% end %>
0
- ERB
0
-
0
- paginate
0
- assert_select 'div.pagination', 2
0
- assert_select 'div.pagination + div#developers', 1
0
- end
0
-
0
- def test_page_entries_info
0
- @template = '<%= page_entries_info collection %>'
0
- array = ('a'..'z').to_a
0
-
0
- paginate array.paginate(:page => 2, :per_page => 5)
0
- assert_equal %{Displaying entries <b>6&nbsp;-&nbsp;10</b> of <b>26</b> in total},
0
- @html_result
0
-
0
- paginate array.paginate(:page => 7, :per_page => 4)
0
- assert_equal %{Displaying entries <b>25&nbsp;-&nbsp;26</b> of <b>26</b> in total},
0
- @html_result
0
- end
0
   
0
+
0
   protected
0
 
0
     def paginate(collection = {}, options = {}, &block)
0
@@ -214,11 +273,13 @@
0
       })
0
     end
0
 
0
- def assert_links_match pattern
0
- assert_select 'div.pagination a[href]' do |elements|
0
- elements.each do |el|
0
- assert_match pattern, el['href']
0
- end
0
+ def assert_links_match pattern, links = nil
0
+ links ||= assert_select 'div.pagination a[href]' do |elements|
0
+ elements
0
+ end
0
+
0
+ links.each do |el|
0
+ assert_match pattern, el['href']
0
       end
0
     end
0
 

Comments

    No one has commented yet.