Permalink
Browse files

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
  • Loading branch information...
dhh committed Dec 2, 2006
1 parent 1ac4470 commit 5410f2cb74737bd6d96c226230c2b9c2bfe1d80b
View
@@ -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]
@@ -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 @@ def lookup(string)
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 @@ def ==(mime_type)
(@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
+require File.dirname(__FILE__) + "/mime_types"
@@ -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 )
@@ -303,6 +303,11 @@ def test_forced_format
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

0 comments on commit 5410f2c

Please sign in to comment.