Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 5663

Show
Ignore:
Timestamp:
12/02/06 22:48:20 (2 years ago)
Author:
david
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r5643 r5663  
    11*SVN* 
     2 
     3* Added the option for extension aliases to mime type registration [DHH]. Example (already in the default routes): 
     4 
     5    Mime::Type.register "text/html", :html, %w( application/xhtml+xml ), %w( xhtml ) 
     6   
     7  ...will respond on both .html and .xhtml. 
    28 
    39* @response.redirect_url works with 201 Created responses: just return headers['Location'] rather than checking the response status.  [Jeremy Kemper] 
  • trunk/actionpack/lib/action_controller/mime_type.rb

    r5623 r5663  
    11module Mime 
     2  SET              = [] 
     3  EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" } 
     4  LOOKUP           = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" } 
     5 
    26  # Encapsulates the notion of a mime type. Can be used at render time, for example, with: 
    37  # 
     
    4549      end 
    4650 
    47       def register(string, symbol, synonyms = []) 
    48         Mime.send :const_set, symbol.to_s.upcase, Type.new(string, symbol, synonyms) 
     51      def register(string, symbol, mime_type_synonyms = [], extension_synonyms = []) 
     52        Mime.send :const_set, symbol.to_s.upcase, Type.new(string, symbol, mime_type_synonyms) 
     53 
    4954        SET << Mime.send(:const_get, symbol.to_s.upcase) 
    50         LOOKUP[string] = EXTENSION_LOOKUP[symbol.to_s] = SET.last         
     55 
     56        ([string] + mime_type_synonyms).each { |string| LOOKUP[string] = SET.last } 
     57        ([symbol.to_s] + extension_synonyms).each { |ext| EXTENSION_LOOKUP[ext] = SET.last } 
    5158      end 
    5259 
     
    130137    end 
    131138  end 
     139end 
    132140 
    133   ALL   = Type.new "*/*", :all 
    134   TEXT  = Type.new "text/plain", :text 
    135   HTML  = Type.new "text/html", :html, %w( application/xhtml+xml ) 
    136   JS    = Type.new "text/javascript", :js, %w( application/javascript application/x-javascript ) 
    137   ICS   = Type.new "text/calendar", :ics 
    138   CSV   = Type.new "text/csv", :csv 
    139   XML   = Type.new "application/xml", :xml, %w( text/xml application/x-xml ) 
    140   RSS   = Type.new "application/rss+xml", :rss 
    141   ATOM  = Type.new "application/atom+xml", :atom 
    142   YAML  = Type.new "application/x-yaml", :yaml, %w( text/yaml ) 
    143  
    144   SET   = [ ALL, TEXT, HTML, JS, ICS, XML, RSS, ATOM, YAML, CSV ] 
    145  
    146   LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" } 
    147  
    148   LOOKUP["*/*"]                      = ALL 
    149  
    150   LOOKUP["text/plain"]               = TEXT 
    151  
    152   LOOKUP["text/html"]                = HTML 
    153   LOOKUP["application/xhtml+xml"]    = HTML 
    154  
    155   LOOKUP["text/javascript"]          = JS 
    156   LOOKUP["application/javascript"]   = JS 
    157   LOOKUP["application/x-javascript"] = JS 
    158  
    159   LOOKUP["text/calendar"]            = ICS 
    160  
    161   LOOKUP["text/csv"]                 = CSV 
    162  
    163   LOOKUP["application/xml"]          = XML 
    164   LOOKUP["text/xml"]                 = XML 
    165   LOOKUP["application/x-xml"]        = XML 
    166  
    167   LOOKUP["text/yaml"]                = YAML 
    168   LOOKUP["application/x-yaml"]       = YAML 
    169  
    170   LOOKUP["application/rss+xml"]      = RSS 
    171   LOOKUP["application/atom+xml"]     = ATOM 
    172  
    173    
    174   EXTENSION_LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) unless k == "" } 
    175  
    176   EXTENSION_LOOKUP["html"]  = HTML 
    177   EXTENSION_LOOKUP["xhtml"] = HTML 
    178  
    179   EXTENSION_LOOKUP["txt"]   = TEXT 
    180  
    181   EXTENSION_LOOKUP["xml"]   = XML 
    182  
    183   EXTENSION_LOOKUP["js"]    = JS 
    184  
    185   EXTENSION_LOOKUP["ics"]   = ICS 
    186  
    187   EXTENSION_LOOKUP["csv"]   = CSV 
    188  
    189   EXTENSION_LOOKUP["yml"]   = YAML 
    190   EXTENSION_LOOKUP["yaml"]  = YAML 
    191  
    192   EXTENSION_LOOKUP["rss"]   = RSS 
    193   EXTENSION_LOOKUP["atom"]  = ATOM 
    194 end 
     141require File.dirname(__FILE__) + "/mime_types" 
  • trunk/actionpack/test/controller/mime_responds_test.rb

    r5232 r5663  
    303303    get :html_xml_or_rss, :format => "rss" 
    304304    assert_equal "RSS", @response.body 
     305  end 
     306   
     307  def test_extension_synonyms 
     308    get :html_xml_or_rss, :format => "xhtml" 
     309    assert_equal "HTML", @response.body 
    305310  end 
    306311