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
Click here to lend your support to: will_paginate and make a donation at www.pledgie.com !
Rename LinkRenderer#url_options to `url_params` and refactor it for speed. 
Closes #197.
Remove `rec_merge!` from core_ext.rb because it is no longer needed.
Mislav Marohnić (author)
Fri Apr 04 19:28:05 -0700 2008
commit  342b04260a1391e2d016dfe6f7c9c349faa7aceb
tree    595f2fa5d6ad4232d5ab19d79d7b78ba997b4e45
parent  421aee8052d3b9f65fe0ddfb5f81daf059176aa2
...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
...
47
48
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -47,21 +47,3 @@ unless Hash.instance_methods.include? 'rec_merge'
0
     end 
0
   end
0
 end
0
-
0
-unless Hash.instance_methods.include? 'rec_merge!'
0
-  Hash.class_eval do
0
-    # Same as Hash#merge!, but recursively merges sub-hashes
0
-    # (stolen from Haml)
0
-    def rec_merge!(other)
0
-      other.each do |key, other_value|
0
-        value = self[key]
0
-        if value.is_a?(Hash) and other_value.is_a?(Hash)
0
-          value.rec_merge! other_value
0
-        else
0
-          self[key] = other_value
0
-        end
0
-      end
0
-      self
0
-    end
0
-  end
0
-end
...
243
244
245
246
 
247
248
249
...
251
252
253
254
255
256
257
258
259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
261
262
...
278
279
280
281
 
282
283
284
285
 
 
 
 
 
 
 
 
 
 
286
287
288
...
243
244
245
 
246
247
248
249
...
251
252
253
 
 
 
 
 
 
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
...
296
297
298
 
299
300
301
 
 
302
303
304
305
306
307
308
309
310
311
312
313
314
0
@@ -243,7 +243,7 @@ module WillPaginate
0
     def page_link_or_span(page, span_class = 'current', text = nil)
0
       text ||= page.to_s
0
       if page and page != current_page
0
-        @template.link_to text, url_options(page), :rel => rel_value(page)
0
+        @template.link_to text, url_params(page), :rel => rel_value(page)
0
       else
0
         @template.content_tag :span, text, :class => span_class
0
       end
0
@@ -251,12 +251,30 @@ module WillPaginate
0
 
0
     # Returns URL params for +page_link_or_span+, taking the current GET params
0
     # and <tt>:params</tt> option into account.
0
-    def url_options(page)
0
-      options = { param_name => page }
0
-      # page links should preserve GET parameters
0
-      options = params.merge(options) if @template.request.get?
0
-      options.rec_merge!(@options[:params]) if @options[:params]
0
-      return options
0
+    def url_params(page)
0
+      unless @url_params
0
+        @url_params = @param_hash = { }
0
+        # page links should preserve GET parameters
0
+        stringified_merge @url_params, @template.params if @template.request.get?
0
+        stringified_merge @url_params, @options[:params] if @options[:params]
0
+        
0
+        if param_name.index(/[^\w-]/)
0
+          page_param = if defined? CGIMethods
0
+            CGIMethods.parse_query_parameters(param_name)
0
+          else
0
+            ActionController::AbstractRequest.parse_query_parameters(param_name)
0
+          end
0
+          
0
+          stringified_merge @url_params, page_param
0
+
0
+          until page_param[@param_name = page_param.keys.first].nil?
0
+            @param_hash = @param_hash[@param_name]
0
+            page_param = page_param[@param_name]
0
+          end 
0
+        end
0
+      end
0
+      @param_hash[param_name] = page
0
+      @url_params
0
     end
0
 
0
   private
0
@@ -278,11 +296,19 @@ module WillPaginate
0
     end
0
 
0
     def param_name
0
-      @param_name ||= @options[:param_name].to_sym
0
+      @param_name ||= @options[:param_name].to_s
0
     end
0
 
0
-    def params
0
-      @params ||= @template.params.to_hash.symbolize_keys
0
+    def stringified_merge(target, other)
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
+        end
0
+      end
0
     end
0
   end
0
 end
...
155
156
157
 
 
 
 
 
158
159
160
...
155
156
157
158
159
160
161
162
163
164
165
0
@@ -155,6 +155,11 @@ class PaginationTest < Test::Unit::TestCase
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 => 2} }, :wp => { :param_name => 'developers[page]' }
0
+    assert_links_match /\?developers%5Bpage%5D=\d+$/
0
+  end
0
 
0
   def test_will_paginate_windows
0
     get :list_developers, { :page => 6, :per_page => 1 }, :wp => { :inner_window => 1 }

Comments