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.
  • Loading branch information...
1 parent 06d518a commit b1667c7c2c1aba4a2adb5f36ad9499c01763655d @chriseppstein chriseppstein committed with josevalim Nov 5, 2010
@@ -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
@@ -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 b1667c7

Please sign in to comment.