aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-12-02 23:07:04 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-12-02 23:07:04 +0000
commit7611fa6d469cef9accabe6bb5e7e95914756322a (patch)
tree1a2501c62c34858277dd0b91906fabd1884245d2
parent5410f2cb74737bd6d96c226230c2b9c2bfe1d80b (diff)
downloadrails-7611fa6d469cef9accabe6bb5e7e95914756322a.tar.gz
rails-7611fa6d469cef9accabe6bb5e7e95914756322a.tar.bz2
rails-7611fa6d469cef9accabe6bb5e7e95914756322a.zip
Added Request#format to return the format used for the request as a mime type. If no format is specified, the first Request#accepts type is used. This means you can stop using respond_to for anything else than responses [DHH]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5664 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r--actionpack/CHANGELOG6
-rw-r--r--actionpack/lib/action_controller/mime_type.rb4
-rwxr-xr-xactionpack/lib/action_controller/request.rb10
-rw-r--r--actionpack/test/controller/request_test.rb12
4 files changed, 32 insertions, 0 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index e2ef473fdd..072ca4a865 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,11 @@
*SVN*
+* Added Request#format to return the format used for the request as a mime type. If no format is specified, the first Request#accepts type is used. This means you can stop using respond_to for anything else than responses [DHH]. Examples:
+
+ GET /posts/5.xml | request.format => Mime::XML
+ GET /posts/5.xhtml | request.format => Mime::HTML
+ GET /posts/5 | request.format => request.accepts.first (usually Mime::HTML for browsers)
+
* Added the option for extension aliases to mime type registration [DHH]. Example (already in the default routes):
Mime::Type.register "text/html", :html, %w( application/xhtml+xml ), %w( xhtml )
diff --git a/actionpack/lib/action_controller/mime_type.rb b/actionpack/lib/action_controller/mime_type.rb
index e57339b3aa..9c4fe63ab0 100644
--- a/actionpack/lib/action_controller/mime_type.rb
+++ b/actionpack/lib/action_controller/mime_type.rb
@@ -48,6 +48,10 @@ module Mime
LOOKUP[string]
end
+ def lookup_by_extension(extension)
+ EXTENSION_LOOKUP[extension]
+ end
+
def register(string, symbol, mime_type_synonyms = [], extension_synonyms = [])
Mime.send :const_set, symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms)
diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb
index 98d28d24b8..8c21adb8df 100755
--- a/actionpack/lib/action_controller/request.rb
+++ b/actionpack/lib/action_controller/request.rb
@@ -83,6 +83,16 @@ module ActionController
end
end
+ # Returns the Mime type for the format used in the request. If there is no format available, the first of the
+ # accept types will be used. Examples:
+ #
+ # GET /posts/5.xml | request.format => Mime::XML
+ # GET /posts/5.xhtml | request.format => Mime::HTML
+ # GET /posts/5 | request.format => request.accepts.first (usually Mime::HTML for browsers)
+ def format
+ parameters[:format] ? Mime::Type.lookup_by_extension(parameters[:format]) : accepts.first
+ end
+
# Returns true if the request's "X-Requested-With" header contains
# "XMLHttpRequest". (The Prototype Javascript library sends this header with
# every Ajax request.)
diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb
index 65e88b74d8..db81eb3097 100644
--- a/actionpack/test/controller/request_test.rb
+++ b/actionpack/test/controller/request_test.rb
@@ -303,6 +303,18 @@ class RequestTest < Test::Unit::TestCase
assert @request.head?
end
+ def test_format
+ @request.instance_eval { @parameters = { :format => 'xml' } }
+ assert_equal Mime::XML, @request.format
+
+ @request.instance_eval { @parameters = { :format => 'xhtml' } }
+ assert_equal Mime::HTML, @request.format
+
+ @request.instance_eval { @parameters = { :format => nil } }
+ @request.env["HTTP_ACCEPT"] = "text/javascript"
+ assert_equal Mime::JS, @request.format
+ end
+
protected
def set_request_method_to(method)
@request.env['REQUEST_METHOD'] = method.to_s.upcase