diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2006-12-02 22:48:20 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2006-12-02 22:48:20 +0000 |
commit | 5410f2cb74737bd6d96c226230c2b9c2bfe1d80b (patch) | |
tree | fa98f0714273e73f2fcee6b7e7853c628502401a | |
parent | 1ac4470da52e3ad4eee945e70f597ce7af7f0003 (diff) | |
download | rails-5410f2cb74737bd6d96c226230c2b9c2bfe1d80b.tar.gz rails-5410f2cb74737bd6d96c226230c2b9c2bfe1d80b.tar.bz2 rails-5410f2cb74737bd6d96c226230c2b9c2bfe1d80b.zip |
Added the option for extension aliases to mime type registration [DHH] Refactored default mime types to use the extension framework instead of just declaring themselves verbosely
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5663 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | actionpack/CHANGELOG | 6 | ||||
-rw-r--r-- | actionpack/lib/action_controller/mime_type.rb | 77 | ||||
-rw-r--r-- | actionpack/lib/action_controller/mime_types.rb | 10 | ||||
-rw-r--r-- | actionpack/test/controller/mime_responds_test.rb | 5 |
4 files changed, 33 insertions, 65 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 8d5ee81c63..e2ef473fdd 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,11 @@ *SVN* +* 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 ) + + ...will respond on both .html and .xhtml. + * @response.redirect_url works with 201 Created responses: just return headers['Location'] rather than checking the response status. [Jeremy Kemper] * Added CSV to Mime::SET so that respond_to csv will work [Cody Fauser] diff --git a/actionpack/lib/action_controller/mime_type.rb b/actionpack/lib/action_controller/mime_type.rb index 81b84f7672..e57339b3aa 100644 --- a/actionpack/lib/action_controller/mime_type.rb +++ b/actionpack/lib/action_controller/mime_type.rb @@ -1,4 +1,8 @@ module Mime + SET = [] + EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" } + LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" } + # Encapsulates the notion of a mime type. Can be used at render time, for example, with: # # class PostsController < ActionController::Base @@ -44,10 +48,13 @@ module Mime LOOKUP[string] end - def register(string, symbol, synonyms = []) - Mime.send :const_set, symbol.to_s.upcase, Type.new(string, symbol, synonyms) + def register(string, symbol, mime_type_synonyms = [], extension_synonyms = []) + Mime.send :const_set, symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms) + SET << Mime.send(:const_get, symbol.to_s.upcase) - LOOKUP[string] = EXTENSION_LOOKUP[symbol.to_s] = SET.last + + ([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last } + ([symbol.to_s] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext] = SET.last } end def parse(accept_header) @@ -129,66 +136,6 @@ module Mime (@synonyms + [ self ]).any? { |synonym| synonym.to_s == mime_type.to_s } if mime_type end end +end - ALL = Type.new "*/*", :all - TEXT = Type.new "text/plain", :text - HTML = Type.new "text/html", :html, %w( application/xhtml+xml ) - JS = Type.new "text/javascript", :js, %w( application/javascript application/x-javascript ) - ICS = Type.new "text/calendar", :ics - CSV = Type.new "text/csv", :csv - XML = Type.new "application/xml", :xml, %w( text/xml application/x-xml ) - RSS = Type.new "application/rss+xml", :rss - ATOM = Type.new "application/atom+xml", :atom - YAML = Type.new "application/x-yaml", :yaml, %w( text/yaml ) - - SET = [ ALL, TEXT, HTML, JS, ICS, XML, RSS, ATOM, YAML, CSV ] - - LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" } - - LOOKUP["*/*"] = ALL - - LOOKUP["text/plain"] = TEXT - - LOOKUP["text/html"] = HTML - LOOKUP["application/xhtml+xml"] = HTML - - LOOKUP["text/javascript"] = JS - LOOKUP["application/javascript"] = JS - LOOKUP["application/x-javascript"] = JS - - LOOKUP["text/calendar"] = ICS - - LOOKUP["text/csv"] = CSV - - LOOKUP["application/xml"] = XML - LOOKUP["text/xml"] = XML - LOOKUP["application/x-xml"] = XML - - LOOKUP["text/yaml"] = YAML - LOOKUP["application/x-yaml"] = YAML - - LOOKUP["application/rss+xml"] = RSS - LOOKUP["application/atom+xml"] = ATOM - - - EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" } - - EXTENSION_LOOKUP["html"] = HTML - EXTENSION_LOOKUP["xhtml"] = HTML - - EXTENSION_LOOKUP["txt"] = TEXT - - EXTENSION_LOOKUP["xml"] = XML - - EXTENSION_LOOKUP["js"] = JS - - EXTENSION_LOOKUP["ics"] = ICS - - EXTENSION_LOOKUP["csv"] = CSV - - EXTENSION_LOOKUP["yml"] = YAML - EXTENSION_LOOKUP["yaml"] = YAML - - EXTENSION_LOOKUP["rss"] = RSS - EXTENSION_LOOKUP["atom"] = ATOM -end
\ No newline at end of file +require File.dirname(__FILE__) + "/mime_types"
\ No newline at end of file diff --git a/actionpack/lib/action_controller/mime_types.rb b/actionpack/lib/action_controller/mime_types.rb new file mode 100644 index 0000000000..d968344b26 --- /dev/null +++ b/actionpack/lib/action_controller/mime_types.rb @@ -0,0 +1,10 @@ +Mime::Type.register "*/*", :all +Mime::Type.register "text/plain", :text +Mime::Type.register "text/html", :html, %w( application/xhtml+xml ), %w( xhtml ) +Mime::Type.register "text/javascript", :js, %w( application/javascript application/x-javascript ) +Mime::Type.register "text/calendar", :ics +Mime::Type.register "text/csv", :csv +Mime::Type.register "application/xml", :xml, %w( text/xml application/x-xml ) +Mime::Type.register "application/rss+xml", :rss +Mime::Type.register "application/atom+xml", :atom +Mime::Type.register "application/x-yaml", :yaml, %w( text/yaml )
\ No newline at end of file diff --git a/actionpack/test/controller/mime_responds_test.rb b/actionpack/test/controller/mime_responds_test.rb index d2cb4f1d84..1d7fdc0052 100644 --- a/actionpack/test/controller/mime_responds_test.rb +++ b/actionpack/test/controller/mime_responds_test.rb @@ -303,6 +303,11 @@ class MimeControllerTest < Test::Unit::TestCase get :html_xml_or_rss, :format => "rss" assert_equal "RSS", @response.body end + + def test_extension_synonyms + get :html_xml_or_rss, :format => "xhtml" + assert_equal "HTML", @response.body + end def test_render_action_for_html @controller.instance_eval do |