Skip to content

Commit

Permalink
Added that render :xml will try to call to_xml if it can [DHH]
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6574 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Apr 24, 2007
1 parent b445a74 commit 82d70d1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
5 changes: 5 additions & 0 deletions actionpack/CHANGELOG
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/base.rb
Expand Up @@ -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:
Expand Down
26 changes: 25 additions & 1 deletion actionpack/test/controller/new_render_test.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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
9 changes: 0 additions & 9 deletions actionpack/test/controller/render_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 82d70d1

Please sign in to comment.