Extend indifferent_params to support hashes nested within arrays.

This commit is contained in:
Chris Hanks 2012-02-01 12:53:16 -08:00
parent 80b98a22a8
commit 3f480092f1
2 changed files with 16 additions and 2 deletions

View File

@ -871,8 +871,10 @@ module Sinatra
def indifferent_params(params)
params = indifferent_hash.merge(params)
params.each do |key, value|
next unless value.is_a?(Hash)
params[key] = indifferent_params(value)
case value
when Hash then params[key] = indifferent_params(value)
when Array then params[key] = value.map { |item| indifferent_params(item) }
end
end
end

View File

@ -360,6 +360,18 @@ class RoutingTest < Test::Unit::TestCase
assert_equal 'well, alright', body
end
it "exposes params nested within arrays with indifferent hash" do
mock_app {
get '/testme' do
assert_equal 'baz', params['bar'][0]['foo']
assert_equal 'baz', params['bar'][0][:foo]
'well, alright'
end
}
get '/testme?bar[][foo]=baz'
assert_equal 'well, alright', body
end
it "supports deeply nested params" do
expected_params = {
"emacs" => {