diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2007-04-24 18:29:37 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2007-04-24 18:29:37 +0000 |
commit | 82d70d1d16443edc787b2a3acf99dd1ff7f8fdcc (patch) | |
tree | ba04e190c090a03b24dbc98d298cb22ec73932af | |
parent | b445a74c5e1cdb10333b02515e42b584463ae10b (diff) | |
download | rails-82d70d1d16443edc787b2a3acf99dd1ff7f8fdcc.tar.gz rails-82d70d1d16443edc787b2a3acf99dd1ff7f8fdcc.tar.bz2 rails-82d70d1d16443edc787b2a3acf99dd1ff7f8fdcc.zip |
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
-rw-r--r-- | actionpack/CHANGELOG | 5 | ||||
-rwxr-xr-x | actionpack/lib/action_controller/base.rb | 2 | ||||
-rw-r--r-- | actionpack/test/controller/new_render_test.rb | 26 | ||||
-rw-r--r-- | actionpack/test/controller/render_test.rb | 9 |
4 files changed, 31 insertions, 11 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 040d1162ad..9797f24410 100644 --- a/actionpack/CHANGELOG +++ b/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) diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index c0c91a3ac9..c1c9e57342 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -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: diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index 0765772e06..18a32f6e30 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -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
\ No newline at end of file diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index eb23ed0c5b..d51af51454 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -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) |