Permalink
Browse files

If a user wants json output then try best to render json output. In s…

…uch cases prefer kind_of(String) over respond_to?(to_str)

[#5841 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
1 parent 66212f6 commit fa2a5ae0339c90d023a7559e681a588219dc3903 Neeraj Singh committed with josevalim Nov 15, 2010
@@ -55,7 +55,7 @@ module All
end
add :json do |json, options|
- json = json.to_json(options) unless json.respond_to?(:to_str)
+ json = json.to_json(options) unless json.kind_of?(String)
json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
self.content_type ||= Mime::JSON
self.response_body = json
@@ -599,6 +599,17 @@ def index
end
end
+class RenderJsonRespondWithController < RespondWithController
+ clear_respond_to
+ respond_to :json
+
+ def index
+ respond_with(resource) do |format|
+ format.json { render :json => RenderJsonTestException.new('boom') }
+ end
+ end
+end
+
class EmptyRespondWithController < ActionController::Base
def index
respond_with(Customer.new("david", 13))
@@ -914,6 +925,13 @@ def test_block_inside_respond_with_is_rendered
assert_equal "JSON", @response.body
end
+ def test_render_json_object_responds_to_str_still_produce_json
+ @controller = RenderJsonRespondWithController.new
+ @request.accept = "application/json"
+ get :index, :format => :json
+ assert_equal %Q{{"message":"boom","error":"RenderJsonTestException"}}, @response.body
+ end
+
def test_no_double_render_is_raised
@request.accept = "text/html"
assert_raise ActionView::MissingTemplate do
@@ -194,3 +194,9 @@ def each
a.each { |i| yield i }
end
end
+
+class RenderJsonTestException < Exception
+ def to_json(options = nil)
+ return { :error => self.class.name, :message => self.to_str }.to_json
+ end
+end

0 comments on commit fa2a5ae

Please sign in to comment.