public
Rubygem
Description: Most awesome pagination solution for Ruby
Homepage: http://github.com/mislav/will_paginate/wikis
Clone URL: git://github.com/mislav/will_paginate.git
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
...
303
304
305
306
307
308
309
 
 
 
 
 
 
 
310
 
 
311
312
313
...
260
261
262
263
264
265
266
...
304
305
306
 
 
 
 
307
308
309
310
311
312
313
314
315
316
317
318
319
0
@@ -260,6 +260,7 @@ module WillPaginate
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
@@ -303,11 +304,16 @@ module WillPaginate
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 @@ ActionController::Base.perform_caching = false
0
 class DummyRequest
0
   def initialize
0
     @get = true
0
+ @params = {}
0
   end
0
   
0
   def get?
0
@@ -30,6 +31,11 @@ class DummyRequest
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 @@ class DummyController
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)
...
12
13
14
15
16
17
 
18
19
 
 
20
21
22
...
55
56
57
 
 
58
59
60
...
88
89
90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
92
93
...
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
...
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
...
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
...
223
224
225
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
 
227
228
229
230
231
232
233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
235
236
237
...
273
274
275
 
 
 
 
 
276
277
278
279
280
281
282
283
284
285
0
@@ -12,11 +12,13 @@ class ViewTest < Test::Unit::TestCase
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 @@ class ViewTest < Test::Unit::TestCase
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
@@ -88,6 +92,101 @@ class ViewTest < Test::Unit::TestCase
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
@@ -124,55 +223,15 @@ class ViewTest < Test::Unit::TestCase
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
@@ -214,11 +273,13 @@ class ViewTest < Test::Unit::TestCase
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.