Added that render :xml will try to call to_xml if it can [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6574 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson
2007-04-24 18:29:37 +00:00
parent b445a74c5e
commit 82d70d1d16
4 changed files with 31 additions and 11 deletions

View File

@@ -1,5 +1,10 @@
*SVN*
* Added that render :xml will try to call to_xml if it can [DHH]. Makes these work:
render :xml => post
render :xml => comments
* Added :location option to render so that the common pattern of rendering a response after creating a new resource is now a 1-liner [DHH]
render :xml => post.to_xml, :status => :created, :location => post_url(post)

View File

@@ -879,7 +879,7 @@ module ActionController #:nodoc:
def render_xml(xml, status = nil) #:nodoc:
response.content_type = Mime::XML
render_text(xml, status)
render_text(xml.respond_to?(:to_xml) ? xml.to_xml : xml, status)
end
def render_json(json, callback = nil, status = nil) #:nodoc:

View File

@@ -258,6 +258,20 @@ class NewRenderTestController < ActionController::Base
head :forbidden, :x_custom_header => "something"
end
def render_with_location
render :xml => "<hello/>", :location => "http://example.com", :status => 201
end
def render_with_to_xml
to_xmlable = Class.new do
def to_xml
"<i-am-xml/>"
end
end.new
render :xml => to_xmlable
end
helper NewRenderTestHelper
helper do
def rjs_helper_method(value)
@@ -742,4 +756,14 @@ EOS
assert_equal "something", @response.headers["X-Custom-Header"]
assert_response :forbidden
end
end
def test_rendering_with_location_should_set_header
get :render_with_location
assert_equal "http://example.com", @response.headers["Location"]
end
def test_rendering_xml_should_call_to_xml_if_possible
get :render_with_to_xml
assert_equal "<i-am-xml/>", @response.body
end
end

View File

@@ -73,10 +73,6 @@ class TestController < ActionController::Base
head :ok
end
def location
render :xml => "<hello/>", :location => "http://example.com", :status => 201
end
def greeting
# let's just rely on the template
end
@@ -372,11 +368,6 @@ class RenderTest < Test::Unit::TestCase
assert_equal '<test>passed formatted html erb</test>', @response.body
end
def test_rendering_with_location_should_set_header
get :location
assert_equal "http://example.com", @response.headers["Location"]
end
protected
def assert_deprecated_render(&block)