Permalink
Browse files

break the #url method into smaller methods and make some optimization…

… by caching base params
  • Loading branch information...
1 parent c0c802b commit 6be4a96e95fb6086758ac2e88d3fb4672de0ac20 @mislav mislav committed Oct 12, 2008
Showing with 42 additions and 23 deletions.
  1. +42 −23 lib/will_paginate/view_helpers/link_renderer.rb
@@ -14,7 +14,7 @@ class LinkRenderer < LinkRendererBase
def prepare(collection, options, template)
super(collection, options)
@template = template
- @container_attributes = nil
+ @container_attributes = @base_url_params = nil
end
# Process it! This method returns the complete HTML string which contains
@@ -77,6 +77,47 @@ def html_container(html)
tag(:div, html, container_attributes)
end
+ # Returns URL params for +page_link_or_span+, taking the current GET params
+ # and <tt>:params</tt> option into account.
+ def url(page)
+ @base_url_params ||= begin
+ url_params = default_url_params
+ merge_optional_params(url_params)
+ url_params
+ end
+
+ url_params = @base_url_params.dup
+ add_current_page_param(url_params, page)
+
+ @template.url_for(url_params)
+ end
+
+ def default_url_params
+ url_params = { :escape => false }
+ if @template.request.get?
+ # page links should preserve GET parameters
+ stringified_merge(url_params, @template.params)
+ end
+ url_params
+ end
+
+ def add_current_page_param(url_params, page)
+ unless param_name.index(/[^\w-]/)
+ url_params[param_name] = page
+ else
+ page_param = (defined?(CGIMethods) ? CGIMethods : ActionController::AbstractRequest).
+ parse_query_parameters(param_name + '=' + page.to_s)
+
+ stringified_merge(url_params, page_param)
+ end
+ end
+
+ def merge_optional_params(url_params)
+ stringified_merge(url_params, @options[:params]) if @options[:params]
+ end
+
+ private
+
def link(text, target, attributes = {})
if target.is_a? Fixnum
attributes[:rel] = rel_value(target)
@@ -96,28 +137,6 @@ def tag(name, value, attributes = {})
"<#{name}#{string_attributes}>#{value}</#{name}>"
end
- # Returns URL params for +page_link_or_span+, taking the current GET params
- # and <tt>:params</tt> option into account.
- def url(page)
- url_params = { :escape => false }
- # page links should preserve GET parameters
- stringified_merge url_params, @template.params if @template.request.get?
- stringified_merge url_params, @options[:params] if @options[:params]
-
- if complex = param_name.index(/[^\w-]/)
- page_param = (defined?(CGIMethods) ? CGIMethods : ActionController::AbstractRequest).
- parse_query_parameters("#{param_name}=#{page}")
-
- stringified_merge url_params, page_param
- else
- url_params[param_name] = page
- end
-
- @template.url_for url_params
- end
-
- private
-
def rel_value(page)
case page
when @collection.previous_page; 'prev' + (page == 1 ? ' start' : '')

0 comments on commit 6be4a96

Please sign in to comment.