aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorSeb Bacon <seb.bacon@gmail.com>2012-01-09 08:44:05 +0000
committerSeb Bacon <seb.bacon@gmail.com>2012-01-09 08:44:05 +0000
commit650c5f26ac0f55f69cbfc80a9d0dc08d32be7f22 (patch)
tree84a1f6d173663fe46abe17f8f92021d2da43d788 /lib
parent7c6075ea10a95a4e74e85a07a55167558a2729a4 (diff)
Ensure pagination works even on general search page
Diffstat (limited to 'lib')
-rw-r--r--lib/willpaginate_extension.rb54
-rw-r--r--lib/willpaginate_hack.rb14
2 files changed, 54 insertions, 14 deletions
diff --git a/lib/willpaginate_extension.rb b/lib/willpaginate_extension.rb
new file mode 100644
index 000000000..98ca8d763
--- /dev/null
+++ b/lib/willpaginate_extension.rb
@@ -0,0 +1,54 @@
+# this extension is loaded in environment.rb
+module WillPaginateExtension
+ class LinkRenderer < WillPaginate::LinkRenderer
+ def page_link(page, text, attributes = {})
+ # Hack for admin pages, when proxied via https on mySociety servers, they
+ # need a relative URL.
+ url = url_for(page)
+ if url.match(/^\/admin.*(\?.*)/)
+ url = $1
+ end
+ @template.link_to text, url, attributes
+ end
+
+ # Returns URL params for +page_link_or_span+, taking the current GET params
+ # and <tt>:params</tt> option into account.
+ def url_for(page)
+ page_one = page == 1
+ unless @url_string and !page_one
+ # the following line makes pagination work on our specially munged search page
+ @url_params = @template.request.symbolized_path_parameters
+ # 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]
+ @request_method=:get, @symbolized_path_parameters={:locale=>"en", :action=>"search", :combined=>["school", "all"], :controller=>"general"}
+ if complex = param_name.index(/[^\w-]/)
+ page_param = parse_query_parameters("#{param_name}=#{page}")
+
+ stringified_merge @url_params, page_param
+ else
+ @url_params[param_name] = page_one ? 1 : 2
+ end
+ url = @template.url_for(@url_params)
+ return url if page_one
+
+ if complex
+ @url_string = url.sub(%r!((?:\?|&amp;)#{CGI.escape param_name}=)#{page}!, "\\1\0")
+ return url
+ else
+ @url_string = url
+ @url_params[param_name] = 3
+ @template.url_for(@url_params).split(//).each_with_index do |char, i|
+ if char == '3' and url[i, 1] == '2'
+ @url_string[i] = "\0"
+ break
+ end
+ end
+ end
+ end
+ # finally!
+ @url_string.sub "\0", page.to_s
+ end
+
+ end
+end
diff --git a/lib/willpaginate_hack.rb b/lib/willpaginate_hack.rb
deleted file mode 100644
index 084329e82..000000000
--- a/lib/willpaginate_hack.rb
+++ /dev/null
@@ -1,14 +0,0 @@
-# Monkeypatch! Hack for admin pages, when proxied via https on mySociety servers, they
-# need a relative URL.
-module WillPaginate
- class LinkRenderer
- def page_link(page, text, attributes = {})
- url = url_for(page)
- if url.match(/^\/admin.*(\?.*)/)
- url = $1
- end
- @template.link_to text, url, attributes
- end
- end
-end
-