Permalink
Browse files

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
  • Loading branch information...
1 parent b445a74 commit 82d70d1d16443edc787b2a3acf99dd1ff7f8fdcc @dhh dhh committed Apr 24, 2007
@@ -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)
@@ -879,7 +879,7 @@ def render_javascript(javascript, status = nil, append_response = true) #: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:
@@ -258,6 +258,20 @@ def head_with_status_code_first
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 @@ def test_head_with_status_code_first
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
@@ -73,10 +73,6 @@ def heading
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 @@ def test_should_render_formatted_html_erb_template_with_faulty_accepts_header
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)

0 comments on commit 82d70d1

Please sign in to comment.