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

Changeset 5664

Show
Ignore:
Timestamp:
12/02/06 23:07:04 (2 years ago)
Author:
david
Message:

Added Request#format to return the format used for the request as a mime type. If no format is specified, the first Request#accepts type is used. This means you can stop using respond_to for anything else than responses [DHH]

Files:

Legend:

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

    r5663 r5664  
    11*SVN* 
     2 
     3* Added Request#format to return the format used for the request as a mime type. If no format is specified, the first Request#accepts type is used. This means you can stop using respond_to for anything else than responses [DHH]. Examples: 
     4 
     5    GET /posts/5.xml   | request.format => Mime::XML 
     6    GET /posts/5.xhtml | request.format => Mime::HTML 
     7    GET /posts/5       | request.format => request.accepts.first (usually Mime::HTML for browsers) 
    28 
    39* Added the option for extension aliases to mime type registration [DHH]. Example (already in the default routes): 
  • trunk/actionpack/lib/action_controller/mime_type.rb

    r5663 r5664  
    4747      def lookup(string) 
    4848        LOOKUP[string] 
     49      end 
     50 
     51      def lookup_by_extension(extension) 
     52        EXTENSION_LOOKUP[extension] 
    4953      end 
    5054 
  • trunk/actionpack/lib/action_controller/request.rb

    r5621 r5664  
    8484    end 
    8585 
     86    # Returns the Mime type for the format used in the request. If there is no format available, the first of the  
     87    # accept types will be used. Examples: 
     88    # 
     89    #   GET /posts/5.xml   | request.format => Mime::XML 
     90    #   GET /posts/5.xhtml | request.format => Mime::HTML 
     91    #   GET /posts/5       | request.format => request.accepts.first (usually Mime::HTML for browsers) 
     92    def format 
     93      parameters[:format] ? Mime::Type.lookup_by_extension(parameters[:format]) : accepts.first 
     94    end 
     95 
    8696    # Returns true if the request's "X-Requested-With" header contains 
    8797    # "XMLHttpRequest". (The Prototype Javascript library sends this header with 
  • trunk/actionpack/test/controller/request_test.rb

    r5621 r5664  
    304304  end 
    305305 
     306  def test_format 
     307    @request.instance_eval { @parameters = { :format => 'xml' } } 
     308    assert_equal Mime::XML, @request.format 
     309 
     310    @request.instance_eval { @parameters = { :format => 'xhtml' } } 
     311    assert_equal Mime::HTML, @request.format 
     312 
     313    @request.instance_eval { @parameters = { :format => nil } } 
     314    @request.env["HTTP_ACCEPT"] = "text/javascript" 
     315    assert_equal Mime::JS, @request.format 
     316  end 
     317 
    306318  protected 
    307319    def set_request_method_to(method)