Skip to content

Commit

Permalink
issue #1405 - params hash keys
Browse files Browse the repository at this point in the history
- the `params` variable is converted into a HashWithIndifferentAccess
  object
- fixes #1405
  • Loading branch information
Ortuna committed Oct 18, 2013
1 parent 00b3054 commit 3a885c8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
5 changes: 4 additions & 1 deletion padrino-core/lib/padrino-core/application/routing.rb
Expand Up @@ -172,6 +172,7 @@ def to_code
env['router.request'] = request
env['router.params'] ||= {}
#{"env['router.params'].merge!(Hash[#{param_names.inspect}.zip(request.params)])" if dynamic?}
env['router.params'] = env['router.params'].with_indifferent_access
@router.rewrite#{"_partial" if route.match_partially}_path_info(env, request)
response = @router.process_destination_path(#{path_ivar}, env)
return response unless router.pass_on_response(response)
Expand Down Expand Up @@ -959,10 +960,12 @@ def recognize_path(path)
#
def current_path(*path_params)
if path_params.last.is_a?(Hash)
path_params[-1] = params.merge(path_params[-1])
path_params[-1] = params.merge(path_params[-1].with_indifferent_access)
else
path_params << params
end

path_params[-1] = path_params[-1].symbolize_keys
@route.path(*path_params)
end

Expand Down
1 change: 1 addition & 0 deletions padrino-core/lib/padrino-core/support_lite.rb
Expand Up @@ -4,6 +4,7 @@
require 'active_support/core_ext/module/aliasing' # alias_method_chain
require 'active_support/core_ext/hash/keys' # symbolize_keys
require 'active_support/core_ext/hash/reverse_merge' # reverse_merge
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/hash/slice' # slice
require 'active_support/core_ext/object/blank' # present?
require 'active_support/core_ext/array/extract_options' # extract_options
Expand Down
2 changes: 1 addition & 1 deletion padrino-core/test/test_reloader_complex.rb
Expand Up @@ -65,7 +65,7 @@
assert_equal 200, status

get "/complex_2_demo/var/destroy/variable"
assert_equal '{:id=>"variable"}', body
assert_equal '{"id"=>"variable"}', body
ensure
# Now we need to prevent to commit a new changed file so we revert it
File.open(Complex1Demo.app_file, "w") { |f| f.write(buffer) }
Expand Down
27 changes: 27 additions & 0 deletions padrino-core/test/test_routing.rb
Expand Up @@ -1790,6 +1790,33 @@ def authorize(username, password)
assert ok?
end

should 'return params as a HashWithIndifferentAccess object via GET' do
mock_app do
get('/foo/:bar') { "#{params["bar"]} #{params[:bar]}" }
get(:foo, :map => '/prefix/:var') { "#{params["var"]} #{params[:var]}" }
end

get('/foo/some_text')
assert_equal "some_text some_text", body

get('/prefix/var')
assert_equal "var var", body
end

should 'return params as a HashWithIndifferentAccess object via POST' do
mock_app do
post('/user') do
"#{params["user"]["full_name"]} #{params[:user][:full_name]}"
end
end

post '/user', {:user => {:full_name => 'example user'}}
assert_equal "example user example user", body

post '/user', {"user" => {"full_name" => 'example user'}}
assert_equal "example user example user", body
end

should 'have MethodOverride middleware with more options' do
mock_app do
put('/hi', :provides => [:json]) { 'hi' }
Expand Down

0 comments on commit 3a885c8

Please sign in to comment.