Permalink
Browse files

Correctly handle the case of an API response that returns a hash by t…

…reating a single hash argument as the resource instead of as options.

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information...
1 parent 7ab940c commit d4b7fa5fb6b4aee8dee90b526c08c0dd2e5e97d3 @chriseppstein chriseppstein committed with spastorino Nov 5, 2010
View
2 actionpack/lib/action_controller/metal/mime_responds.rb
@@ -227,7 +227,7 @@ def respond_with(*resources, &block)
"controller responds to in the class level" if self.class.mimes_for_respond_to.empty?
if response = retrieve_response_from_mimes(&block)
- options = resources.extract_options!
+ options = resources.size == 1 ? {} : resources.extract_options!
options.merge!(:default_response => response)
(options.delete(:responder) || self.class.responder).call(self, resources, options)
end
View
16 actionpack/test/controller/mime_responds_test.rb
@@ -487,6 +487,10 @@ def using_resource
respond_with(resource)
end
+ def using_hash_resource
+ respond_with({:result => resource})
+ end
+
def using_resource_with_block
respond_with(resource) do |format|
format.csv { render :text => "CSV" }
@@ -587,6 +591,18 @@ def test_using_resource
end
end
+ def test_using_hash_resource
+ @request.accept = "application/xml"
+ get :using_hash_resource
+ assert_equal "application/xml", @response.content_type
+ assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<hash>\n <name>david</name>\n</hash>\n", @response.body
+
+ @request.accept = "application/json"
+ get :using_hash_resource
+ assert_equal "application/json", @response.content_type
+ assert_equal %Q[{"result":["david",13]}], @response.body
+ end
+
def test_using_resource_with_block
@request.accept = "*/*"
get :using_resource_with_block

0 comments on commit d4b7fa5

Please sign in to comment.