Permalink
Browse files

Recursively converts parameter values to strings.

  • Loading branch information...
1 parent 5c1ea8b commit 8bb2555d04f7fb67407f7224536e8b32349cede1 AJ Schuster committed with danhodos Oct 13, 2010
View
5 lib/riot/action_controller/http_methods.rb
@@ -8,11 +8,10 @@ def put(uri, params={}); perform_request("PUT", uri, params); end
def delete(uri, params={}); perform_request("DELETE", uri, params); end
private
def perform_request(request_method, uri, params)
- params = params.inject({}) { |acc,(key,val)| acc[key] = val.to_s; acc }
- @env = ::Rack::MockRequest.env_for(uri, {:params => params, :method => request_method}).merge(@env)
+ @env = ::Rack::MockRequest.env_for(uri, {:params => params.stringify_values, :method => request_method}).merge(@env)
@env['action_dispatch.show_exceptions'] = false
@app.call(@env).tap { |state| @response = state.last }
end
end # HttpMethods
end # ActionController
-end # RiotRails
+end # RiotRails
View
1 lib/riot/core_ext.rb
@@ -0,0 +1 @@
+require 'riot/core_ext/hash'
View
18 lib/riot/core_ext/hash.rb
@@ -0,0 +1,18 @@
+module RiotRails
+ module CoreExt
+ module Hash
+ def stringify_values
+ inject({}) do |hsh,(key,val)|
+ hsh[key] = val.respond_to?(:stringify_values) ? val.stringify_values : val.to_s
+ hsh
+ end
+ end
+
+ def stringify_values!
+ replace(stringify_values)
+ end
+ end # Hash
+ end # CoreExt
+end # RiotRails
+
+Hash.class_eval { include RiotRails::CoreExt::Hash }
View
3 lib/riot/rails.rb
@@ -1 +1,2 @@
-require 'riot'
+require 'riot/core_ext'
+require 'riot'
View
11 test/action_controller/post_request_test.rb
@@ -39,6 +39,17 @@
asserts("params") { controller.params }.includes("momma")
asserts("params") { controller.params }.includes("love_you_too")
end # with parameters
+
+ context "with nested parameters" do
+ setup { post "/rooms/create", {:momma => {:loves => {:son => :yes, :daughter => :also} } } }
+
+ asserts("params") { controller.params }.includes("momma")
+ asserts("params") { controller.params[:momma] }.includes("loves")
+ asserts("params") { controller.params[:momma][:loves] }.includes("son")
+ asserts("params") { controller.params[:momma][:loves][:son] }.equals("yes")
+ asserts("params") { controller.params[:momma][:loves] }.includes("daughter")
+ asserts("params") { controller.params[:momma][:loves][:daughter] }.equals("also")
+ end
end # for a POST request
View
24 test/core_ext/hash_test.rb
@@ -0,0 +1,24 @@
+require 'teststrap'
+
+context "Hash#stringify_values" do
+
+ context "a simple hash" do
+ setup { { :a => 1} }
+ asserts(:stringify_values).equals { {:a => '1'} }
+ end
+
+ context "a nested hash" do
+ setup { { :a => { :b => :ddjkgdkj, :c => 23 }} }
+ asserts(:stringify_values).equals { {:a => { :b => 'ddjkgdkj', :c => '23' } } }
+ end
+
+end
+
+context "Hash#stringify_values!" do
+ context "some hash" do
+ setup { { 1 => 2 } }
+ should "replace self with stringify_values'd version" do
+ topic.tap { |t| t.stringify_values! }
+ end.equals({ 1 => '2' })
+ end
+end

0 comments on commit 8bb2555

Please sign in to comment.