aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-09-17 18:10:25 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-09-17 18:10:25 +0000
commitcaa8a005202b267b75baf521d5f613f3a2bce600 (patch)
treedf72b8673b625b22037895a81b672a8b1650d5a6
parent98a412aa3853b9e31b747f53e58a841966df5f1b (diff)
downloadrails-caa8a005202b267b75baf521d5f613f3a2bce600.tar.gz
rails-caa8a005202b267b75baf521d5f613f3a2bce600.tar.bz2
rails-caa8a005202b267b75baf521d5f613f3a2bce600.zip
Added that respond_to blocks will automatically set the content type to be the same as is requested [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5131 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--actionpack/CHANGELOG8
-rw-r--r--actionpack/lib/action_controller/mime_responds.rb7
-rw-r--r--actionpack/test/controller/content_type_test.rb31
-rw-r--r--actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml1
4 files changed, 45 insertions, 2 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index f8d4057df5..c91a0dcde7 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,13 @@
*SVN*
+* Added that respond_to blocks will automatically set the content type to be the same as is requested [DHH]. Examples:
+
+ respond_to do |format|
+ format.html { render :text => "I'm being sent as text/html" }
+ format.rss { render :text => "I'm being sent as application/rss+xml" }
+ format.atom { render :text => "I'm being sent as application/xml", :content_type => Mime::XML }
+ end
+
* Added utf-8 as the default charset for all renders. You can change this default using ActionController::Base.default_charset=(encoding) [DHH]
* Added proper getters and setters for content type and charset [DHH]. Example of what we used to do:
diff --git a/actionpack/lib/action_controller/mime_responds.rb b/actionpack/lib/action_controller/mime_responds.rb
index 07bbbb4732..6c721f4ba2 100644
--- a/actionpack/lib/action_controller/mime_responds.rb
+++ b/actionpack/lib/action_controller/mime_responds.rb
@@ -108,7 +108,7 @@ module ActionController #:nodoc:
class Responder #:nodoc:
DEFAULT_BLOCKS = [:html, :js, :xml].inject({}) do |blocks, ext|
- blocks.update ext => %(Proc.new { render :action => "\#{action_name}.r#{ext}" })
+ blocks.update ext => %(Proc.new { render :action => "\#{action_name}.r#{ext}", :content_type => Mime::#{ext.to_s.upcase} })
end
def initialize(block_binding)
@@ -129,7 +129,10 @@ module ActionController #:nodoc:
@order << mime_type
if block_given?
- @responses[mime_type] = block
+ @responses[mime_type] = Proc.new do
+ eval "response.content_type = Mime::#{mime_type.to_sym.to_s.upcase}", @block_binding
+ block.call
+ end
else
if source = DEFAULT_BLOCKS[mime_type.to_sym]
@responses[mime_type] = eval(source, @block_binding)
diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb
index 71563ba9a6..f2708acc0a 100644
--- a/actionpack/test/controller/content_type_test.rb
+++ b/actionpack/test/controller/content_type_test.rb
@@ -30,6 +30,15 @@ class ContentTypeController < ActionController::Base
render :action => "render_default_for_rxml"
end
+ def render_default_content_types_for_respond_to
+ respond_to do |format|
+ format.html { render :text => "hello world!" }
+ format.xml { render :action => "render_default_content_types_for_respond_to.rhtml" }
+ format.js { render :text => "hello world!" }
+ format.rss { render :text => "hello world!", :content_type => Mime::XML }
+ end
+ end
+
def rescue_action(e) raise end
end
@@ -96,4 +105,26 @@ class ContentTypeTest < Test::Unit::TestCase
assert_equal Mime::HTML, @response.content_type
assert_equal "utf-8", @response.charset
end
+
+ def test_render_default_content_types_for_respond_to
+ @request.env["HTTP_ACCEPT"] = Mime::HTML.to_s
+ get :render_default_content_types_for_respond_to
+ assert_equal Mime::HTML, @response.content_type
+
+ @request.env["HTTP_ACCEPT"] = Mime::JS.to_s
+ get :render_default_content_types_for_respond_to
+ assert_equal Mime::JS, @response.content_type
+ end
+
+ def test_render_default_content_types_for_respond_to_with_template
+ @request.env["HTTP_ACCEPT"] = Mime::XML.to_s
+ get :render_default_content_types_for_respond_to
+ assert_equal Mime::XML, @response.content_type
+ end
+
+ def test_render_default_content_types_for_respond_to_with_overwrite
+ @request.env["HTTP_ACCEPT"] = Mime::RSS.to_s
+ get :render_default_content_types_for_respond_to
+ assert_equal Mime::XML, @response.content_type
+ end
end \ No newline at end of file
diff --git a/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml b/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml
new file mode 100644
index 0000000000..25dc746886
--- /dev/null
+++ b/actionpack/test/fixtures/content_type/render_default_content_types_for_respond_to.rhtml
@@ -0,0 +1 @@
+<hello>world</hello> \ No newline at end of file