From d48039cefb684d678480e1f078c6082792038d03 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 15 Sep 2007 04:18:32 +0000 Subject: Fixed that setting request.format would also affect respond_to blocks [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7479 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/request.rb | 20 +++++++++++++++++--- actionpack/test/controller/mime_responds_test.rb | 17 +++++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) (limited to 'actionpack') diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index ec7cb735aa..5ae64a89fd 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Fixed that setting request.format would also affect respond_to blocks [DHH] + * Add option to force binary mode on tempfile used for fixture_file_upload. #6380 [Jonathan Viney] * Fixed that resource namespaces wouldn't stick to all nested resources #9399 [pixeltrix] diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb index ceedcfec4f..0f14ede347 100755 --- a/actionpack/lib/action_controller/request.rb +++ b/actionpack/lib/action_controller/request.rb @@ -12,9 +12,6 @@ module ActionController # such as { 'RAILS_ENV' => 'production' }. attr_reader :env - # The requested content type, such as :html or :xml. - attr_writer :format - # The HTTP request method as a lowercase symbol, such as :get. # Note, HEAD is returned as :get since the two are functionally # equivalent from the application's perspective. @@ -90,6 +87,23 @@ module ActionController def format @format ||= parameters[:format] ? Mime::Type.lookup_by_extension(parameters[:format]) : accepts.first end + + + # Sets the format by string extension, which can be used to force custom formats that are not controlled by the extension. + # Example: + # + # class ApplicationController < ActionController::Base + # before_filter :adjust_format_for_iphone + # + # private + # def adjust_format_for_iphone + # request.format = :iphone if request.env["HTTP_USER_AGENT"][/iPhone/] + # end + # end + def format=(extension) + parameters[:format] = extension.to_s + format + end # Returns true if the request's "X-Requested-With" header contains # "XMLHttpRequest". (The Prototype Javascript library sends this header with diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index 40392abbe9..144aff7d7c 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -35,6 +35,15 @@ class RespondToController < ActionController::Base end end + def forced_xml + request.format = :xml + + respond_to do |type| + type.html { render :text => "HTML" } + type.xml { render :text => "XML" } + end + end + def just_xml respond_to do |type| type.xml { render :text => "XML" } @@ -343,6 +352,14 @@ class MimeControllerTest < Test::Unit::TestCase assert_equal "RSS", @response.body end + def test_internally_forced_format + get :forced_xml + assert_equal "XML", @response.body + + get :forced_xml, :format => "html" + assert_equal "XML", @response.body + end + def test_extension_synonyms get :html_xml_or_rss, :format => "xhtml" assert_equal "HTML", @response.body -- cgit v1.2.3