Skip to content

Commit

Permalink
rack hack for rack params bug
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorturk committed Jan 14, 2011
1 parent 01071a2 commit 47975e3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/initializers/_rack_hack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# http://thewebfellas.com/blog/2010/7/15/rails-2-3-8-rack-1-1-and-the-curious-case-of-the-missing-quotes
raise "rack hack not needed anymore?" unless RAILS_GEM_VERSION == '2.3.10'
Dir[File.join(Rails.root, "lib", "patches", "**", "*.rb")].sort.each { |patch| require(patch) }
55 changes: 55 additions & 0 deletions lib/patches/rack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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

0 comments on commit 47975e3

Please sign in to comment.