aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSeb Bacon <seb.bacon@gmail.com>2011-08-15 11:50:48 +0100
committerSeb Bacon <seb.bacon@gmail.com>2011-08-15 11:50:48 +0100
commit8af626ac69b1afb39ebba8ab0279c0adba3e6faf (patch)
tree08d1bb46d53d89143b5837a3ddc6c44deb933df8
parenta7df6f479f452503e3a99fe21b5827b96f4e279c (diff)
New files I forgot to add in a7df6f479f452503e3a9
-rw-r--r--lib/rack_quote_monkeypatch.rb65
-rw-r--r--spec/integration/search_request_spec.rb9
2 files changed, 74 insertions, 0 deletions
diff --git a/lib/rack_quote_monkeypatch.rb b/lib/rack_quote_monkeypatch.rb
new file mode 100644
index 000000000..be079c6a2
--- /dev/null
+++ b/lib/rack_quote_monkeypatch.rb
@@ -0,0 +1,65 @@
+# There's a bug in Rack 1.1.x which is fixed in Rack 1.2, but our
+# current version of Rails won't use that. So for now, monkeypatch,
+# This can be dropped when we move to Rails 3.
+#
+# See https://github.com/sebbacon/alaveteli/issues/38 for Alaveteli
+# bug report
+#
+# More info about the monkeypatch:
+# http://thewebfellas.com/blog/2010/7/15/rails-2-3-8-rack-1-1-and-the-curious-case-of-the-missing-quotes
+
+module Rack
+ module Utils
+ def parse_query(qs, d = nil)
+ params = {}
+
+ (qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p|
+ k, v = p.split('=', 2).map { |x| unescape(x) }
+ if cur = params[k]
+ if cur.class == Array
+ params[k] << v
+ else
+ params[k] = [cur, v]
+ end
+ else
+ params[k] = v
+ end
+ end
+
+ return params
+ end
+ module_function :parse_query
+
+ def normalize_params(params, name, v = nil)
+ name =~ %r(\A[\[\]]*([^\[\]]+)\]*)
+ k = $1 || ''
+ after = $' || ''
+
+ return if k.empty?
+
+ if after == ""
+ params[k] = v
+ elsif after == "[]"
+ params[k] ||= []
+ raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
+ params[k] << v
+ elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$)
+ child_key = $1
+ params[k] ||= []
+ raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
+ if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key)
+ normalize_params(params[k].last, child_key, v)
+ else
+ params[k] << normalize_params({}, child_key, v)
+ end
+ else
+ params[k] ||= {}
+ raise TypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Hash)
+ params[k] = normalize_params(params[k], after, v)
+ end
+
+ return params
+ end
+ module_function :normalize_params
+ end
+end
diff --git a/spec/integration/search_request_spec.rb b/spec/integration/search_request_spec.rb
new file mode 100644
index 000000000..9398519b7
--- /dev/null
+++ b/spec/integration/search_request_spec.rb
@@ -0,0 +1,9 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+describe "When searching" do
+ it "should not strip quotes from quoted query" do
+ request_via_redirect("post", "/search", :query => '"mouse stilton"')
+ response.body.should include("&quot;mouse stilton&quot;")
+ end
+end
+