Added Mime::Type.register(string, symbol, synonyms = []) for adding new custom mime types [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4405 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2006-06-02 04:11:56 +00:00
parent 5c30352328
commit 984aa7eeb9
3 changed files with 20 additions and 4 deletions

@ -1,5 +1,7 @@
*SVN*
* Added Mime::Type.register(string, symbol, synonyms = []) for adding new custom mime types [DHH]. Example: Mime::Type.register("image/gif", :gif)
* Added support for Mime objects in render :content_type option [DHH]. Example: render :text => some_atom, :content_type => Mime::ATOM
* Add :status option to send_data and send_file. Defaults to '200 OK'. #5243 [Manfred Stienstra <m.stienstra@fngtps.com>]

@ -31,6 +31,11 @@ def lookup(string)
LOOKUP[string]
end
def register(string, symbol, synonyms = [])
Mime.send :const_set, symbol.to_s.upcase, Type.new(string, symbol, synonyms)
LOOKUP[string] = Mime.send :const_get, symbol.to_s.upcase
end
def parse(accept_header)
# keep track of creation order to keep the subsequent sort stable
index = 0
@ -114,6 +119,7 @@ def ==(mime_type)
ALL = Type.new "*/*", :all
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
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
@ -127,14 +133,16 @@ def ==(mime_type)
LOOKUP["text/html"] = HTML
LOOKUP["application/xhtml+xml"] = HTML
LOOKUP["application/xml"] = XML
LOOKUP["text/xml"] = XML
LOOKUP["application/x-xml"] = XML
LOOKUP["text/javascript"] = JS
LOOKUP["application/javascript"] = JS
LOOKUP["application/x-javascript"] = JS
LOOKUP["text/calendar"] = ICS
LOOKUP["application/xml"] = XML
LOOKUP["text/xml"] = XML
LOOKUP["application/x-xml"] = XML
LOOKUP["text/yaml"] = YAML
LOOKUP["application/x-yaml"] = YAML

@ -21,4 +21,10 @@ def test_parse_with_q
expect = [Mime::HTML, Mime::XML, Mime::PNG, Mime::PLAIN, Mime::YAML, Mime::ALL]
assert_equal expect, Mime::Type.parse(accept)
end
def test_custom_type
Mime::Type.register("image/gif", :gif)
assert_nothing_raised { Mime::GIF }
Mime.send :remove_const, :GIF
end
end