Skip to content
Browse files

Use Rack 1.0's built in nested params

Our implementation is retained but is not used when Rack 1.0
is detected. We should remove Base#nested_params as soon as we
make Rack 1.0 a requirement.
  • Loading branch information...
1 parent e9cc0f7 commit eb18c9be6d1cf3e9d6aca73b21549285a43c9c63 @reinh reinh committed with rtomayko
Showing with 19 additions and 1 deletion.
  1. +19 −1 lib/sinatra/base.rb
View
20 lib/sinatra/base.rb
@@ -407,7 +407,13 @@ def forward
private
# Run before filters and then locate and run a matching route.
def route!
- @params = nested_params(@request.params)
+ # enable nested params in Rack < 1.0; allow indifferent access
+ @params =
+ if Rack::Utils.respond_to?(:parse_nested_query)
+ indifferent_params(@request.params)
+ else
+ nested_params(@request.params)
+ end
# before filters
self.class.filters.each { |block| instance_eval(&block) }
@@ -468,6 +474,18 @@ def route_missing
end
end
+ # Enable string or symbol key access to the nested params hash.
+ 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)
+ end
+ end
+
+ # Recursively replace the params hash with a nested indifferent
+ # hash. Rack 1.0 has a built in implementation of this method - remove
+ # this once Rack 1.0 is required.
def nested_params(params)
return indifferent_hash.merge(params) if !params.keys.join.include?('[')
params.inject indifferent_hash do |res, (key,val)|

0 comments on commit eb18c9b

Please sign in to comment.
Something went wrong with that request. Please try again.