mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
renderer calls object.to_json when rendering :json => object [#5655 state:resolved]
Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
parent
4966b915fe
commit
72f37bd8bc
2 changed files with 14 additions and 1 deletions
|
@ -71,7 +71,7 @@ module ActionController
|
|||
end
|
||||
|
||||
add :json do |json, options|
|
||||
json = ActiveSupport::JSON.encode(json, options) unless json.respond_to?(:to_str)
|
||||
json = json.to_json(options) unless json.respond_to?(:to_str)
|
||||
json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
|
||||
self.content_type ||= Mime::JSON
|
||||
self.response_body = json
|
||||
|
|
|
@ -9,6 +9,10 @@ class RenderJsonTest < ActionController::TestCase
|
|||
hash.except!(*options[:except]) if options[:except]
|
||||
hash
|
||||
end
|
||||
|
||||
def to_json(options = {})
|
||||
super :except => [:c, :e]
|
||||
end
|
||||
end
|
||||
|
||||
class TestController < ActionController::Base
|
||||
|
@ -49,6 +53,10 @@ class RenderJsonTest < ActionController::TestCase
|
|||
def render_json_with_extra_options
|
||||
render :json => JsonRenderable.new, :except => [:c, :e]
|
||||
end
|
||||
|
||||
def render_json_without_options
|
||||
render :json => JsonRenderable.new
|
||||
end
|
||||
end
|
||||
|
||||
tests TestController
|
||||
|
@ -109,4 +117,9 @@ class RenderJsonTest < ActionController::TestCase
|
|||
assert_equal '{"a":"b"}', @response.body
|
||||
assert_equal 'application/json', @response.content_type
|
||||
end
|
||||
|
||||
def test_render_json_calls_to_json_from_object
|
||||
get :render_json_without_options
|
||||
assert_equal '{"a":"b"}', @response.body
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue