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

Changeset 3844

Show
Ignore:
Timestamp:
03/12/06 06:02:44 (3 years ago)
Author:
david
Message:

Added synonym and custom type handling to respond_to [DHH]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/mime_responds.rb

    r3842 r3844  
    2727        @responses = {} 
    2828      end 
     29 
     30      def custom(mime_type, *args, &block) 
     31        mime_type = mime_type.is_a?(Mime::Type) ? mime_type : Mime::Type.lookup(mime_type.to_s) 
     32         
     33        @order << mime_type 
     34         
     35        if block_given? 
     36          @responses[mime_type] = block 
     37        else 
     38          if argument = args.first 
     39            eval("__mime_responder_arg__ = " + (argument.is_a?(String) ? "'" + argument + "'" : argument), @block_binding) 
     40            @responses[mime_type] = eval(DEFAULT_BLOCKS[(mime_type.to_sym.to_s + "_arg").to_sym], @block_binding) 
     41          else 
     42            @responses[mime_type] = eval(DEFAULT_BLOCKS[mime_type.to_sym], @block_binding) 
     43          end 
     44        end 
     45      end 
    2946       
    3047      for mime_type in %w( all html js xml rss atom yaml ) 
    3148        eval <<-EOT 
    3249          def #{mime_type}(argument = nil, &block) 
    33             @order << Mime::#{mime_type.upcase} 
    34              
    35             if block_given? 
    36               @responses[Mime::#{mime_type.upcase}] = block 
    37             else 
    38               if argument 
    39                 eval("__mime_responder_arg__ = " + (argument.is_a?(String) ? "'" + argument + "'" : argument), @block_binding) 
    40                 @responses[Mime::#{mime_type.upcase}] = eval(DEFAULT_BLOCKS[(Mime::#{mime_type.upcase}.to_sym.to_s + "_arg").to_sym], @block_binding) 
    41               else 
    42                 @responses[Mime::#{mime_type.upcase}] = eval(DEFAULT_BLOCKS[Mime::#{mime_type.upcase}.to_sym], @block_binding) 
    43               end 
    44             end 
     50            custom(Mime::#{mime_type.upcase}, argument, &block) 
    4551          end 
    4652        EOT 
     
    5359            return 
    5460          else 
    55             if @order.include?(priority) 
     61            if priority === @order 
    5662              @responses[priority].call 
    5763              return # mime type match found, be happy and return 
  • trunk/actionpack/lib/action_controller/mime_type.rb

    r3843 r3844  
    11module Mime 
    22  class Type < String 
    3     def initialize(string, part_of_all = true) 
    4       @part_of_all = part_of_all 
     3    def self.lookup(string) 
     4      LOOKUP[string] 
     5    end 
     6     
     7    def initialize(string, symbol = nil, synonyms = []) 
     8      @symbol, @synonyms = symbol, synonyms 
    59      super(string) 
    610    end 
    711     
    812    def to_sym 
    9       SYMBOLIZED_MIME_TYPES[self] ? SYMBOLIZED_MIME_TYPES[self] : to_sym 
     13      @symbol || to_sym 
    1014    end 
    1115 
    1216    def ===(list) 
    1317      if list.is_a?(Array) 
    14         list.include?(self) 
     18        (@synonyms + [ self ]).any? { |synonym| list.include?(synonym) } 
    1519      else 
    1620        super 
     
    1923  end 
    2024 
    21   SYMBOLIZED_MIME_TYPES = { 
    22     ""                         => :unspecified, 
    23     "*/*"                      => :all, 
    24     "text/html"                => :html, 
    25     "application/javascript"   => :js, 
    26     "application/x-javascript" => :js, 
    27     "text/javascript"          => :js, 
    28     "text/xml"                 => :xml, 
    29     "application/xml"          => :xml, 
    30     "application/rss+xml"      => :rss, 
    31     "application/rss+atom"     => :atom, 
    32     "application/x-xml"        => :xml, 
    33     "application/x-yaml"       => :yaml 
    34   } 
     25  ALL  = Type.new "*/*", :all 
     26  HTML = Type.new "text/html", :html 
     27  JS   = Type.new "text/javascript", :js, %w( application/javascript application/x-javascript ) 
     28  XML  = Type.new "text/xml", :xml, %w( application/xml application/x-xml ) 
     29  RSS  = Type.new "application/rss+xml", :rss 
     30  ATOM = Type.new "application/atom+xml", :atom 
     31  YAML = Type.new "application/x-yaml", :yaml 
    3532 
    36   ALL        = Type.new "*/*" 
    37   HTML       = Type.new "text/html" 
    38   JS         = Type.new "text/javascript" 
    39   JAVASCRIPT = Type.new "text/javascript" 
    40   XML        = Type.new "application/xml" 
    41   RSS        = Type.new "application/rss+xml" 
    42   ATOM       = Type.new "application/atom+xml" 
    43   YAML       = Type.new "application/x-yaml" 
     33  LOOKUP = Hash.new { |h, k| h[k] = Type.new(k) } 
     34 
     35  LOOKUP["*/*"]                      = ALL 
     36  LOOKUP["text/html"]                = HTML 
     37  LOOKUP["application/rss+xml"]      = RSS 
     38  LOOKUP["application/atom+xml"]     = ATOM 
     39  LOOKUP["application/x-yaml"]       = YAML 
     40 
     41  LOOKUP["text/javascript"]          = JS 
     42  LOOKUP["application/javascript"]   = JS 
     43  LOOKUP["application/x-javascript"] = JS 
     44 
     45  LOOKUP["text/xml"]                 = XML 
     46  LOOKUP["application/xml"]          = XML 
     47  LOOKUP["application/x-xml"]        = XML 
    4448end 
  • trunk/actionpack/lib/action_controller/request.rb

    r3843 r3844  
    6262      end 
    6363 
    64       @content_type = Mime::Type.new(@content_type) 
     64      @content_type = Mime::Type.lookup(@content_type) 
    6565    end 
    6666 
     
    7272      else 
    7373        @env['HTTP_ACCEPT'].split(";").collect! do |mime_type| 
    74           Mime::Type.new(mime_type.strip) 
     74          Mime::Type.lookup(mime_type.strip) 
    7575        end 
    7676      end 
  • trunk/actionpack/test/controller/mime_responds_test.rb

    r3843 r3844  
    5353      type.rss  { render :text => "RSS"  } 
    5454      type.atom { render :text => "ATOM" } 
     55      type.all  { render :text => "Nothing" } 
     56    end 
     57  end 
     58 
     59  def custom_type_handling 
     60    respond_to do |type| 
     61      type.html { render :text => "HTML"    } 
     62      type.custom("application/crazy-xml")  { render :text => "Crazy XML"  } 
    5563      type.all  { render :text => "Nothing" } 
    5664    end 
     
    156164    assert_equal "RSS", @response.body 
    157165  end 
     166   
     167  def test_synonyms 
     168    @request.env["HTTP_ACCEPT"] = "application/javascript" 
     169    get :js_or_html 
     170    assert_equal 'JS', @response.body 
     171 
     172    @request.env["HTTP_ACCEPT"] = "application/x-xml" 
     173    get :using_argument_defaults 
     174    assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n  <name>David</name>\n</person>\n", @response.body 
     175  end 
     176   
     177  def test_custom_types 
     178    @request.env["HTTP_ACCEPT"] = "application/crazy-xml" 
     179    get :custom_type_handling 
     180    assert_equal 'Crazy XML', @response.body 
     181 
     182    @request.env["HTTP_ACCEPT"] = "text/html" 
     183    get :custom_type_handling 
     184    assert_equal 'HTML', @response.body 
     185  end 
    158186end