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

Changeset 5129

Show
Ignore:
Timestamp:
09/17/06 16:20:32 (2 years ago)
Author:
david
Message:

Added proper getters and setters for content type and charset [DHH] Added utf-8 as the default charset for all renders. You can change this default using ActionController::Base.default_charset=(encoding) [DHH]

Files:

Legend:

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

    r5128 r5129  
    11*SVN* 
     2 
     3* Added utf-8 as the default charset for all renders. You can change this default using ActionController::Base.default_charset=(encoding) [DHH] 
     4 
     5* Added proper getters and setters for content type and charset [DHH]. Example of what we used to do: 
     6 
     7    response.headers["Content-Type"] = "application/atom+xml; charset=utf-8" 
     8   
     9  ...now: 
     10   
     11    response.content_type = Mime::ATOM 
     12    response.charset      = "utf-8" 
    213 
    314* Updated prototype.js to 1.5.0_rc1 with latest fixes. [Rick Olson] 
  • trunk/actionpack/lib/action_controller/base.rb

    r5126 r5129  
    273273    cattr_accessor :param_parsers 
    274274 
     275    # Controls the default charset for all renders. 
     276    @@default_charset = "utf-8" 
     277    cattr_accessor :default_charset 
     278 
    275279    # Template root determines the base from which template references will be made. So a call to render("test/template") 
    276280    # will be converted to "#{template_root}/test/template.rhtml". 
     
    421425        send(method, *arguments) 
    422426 
     427        assign_default_content_type_and_charset 
    423428        response 
    424429      ensure 
     
    704709 
    705710        if content_type = options[:content_type] 
    706           headers["Content-Type"] = content_type.to_s 
     711          response.content_type = content_type.to_s 
    707712        end 
    708713 
     
    794799 
    795800      def render_javascript(javascript, status = nil) #:nodoc: 
    796         response.headers['Content-Type'] = 'text/javascript; charset=UTF-8' 
     801        response.content_type = Mime::JS 
    797802        render_text(javascript, status) 
    798803      end 
    799804 
    800805      def render_xml(xml, status = nil) #:nodoc: 
    801         response.headers['Content-Type'] = 'application/xml' 
     806        response.content_type = Mime::XML 
    802807        render_text(xml, status) 
    803808      end 
     
    10351040      end 
    10361041 
     1042      def assign_default_content_type_and_charset 
     1043        response.content_type ||= Mime::HTML 
     1044        response.charset      ||= self.class.default_charset 
     1045      end 
     1046 
    10371047      def action_methods 
    10381048        self.class.action_methods 
  • trunk/actionpack/lib/action_controller/caching.rb

    r4898 r5129  
    211211            if extention = action_cache_path.extension 
    212212              content_type = Mime::EXTENSION_LOOKUP[extention] 
    213               action_cache_path.controller.headers['Content-Type'] = content_type.to_s 
     213              action_cache_path.controller.content_type = content_type.to_s 
    214214            end 
    215215          end 
  • trunk/actionpack/lib/action_controller/rescue.rb

    r4955 r5129  
    7272        @template.instance_variable_set("@contents", @template.render_file(template_path_for_local_rescue(exception), false)) 
    7373     
    74         @headers["Content-Type"] = "text/html" 
     74        response.content_type = Mime::HTML 
    7575        render_file(rescues_path("layout"), response_code_for_rescue(exception)) 
    7676      end 
  • trunk/actionpack/lib/action_controller/response.rb

    r4346 r5129  
    66    def initialize 
    77      @body, @headers, @session, @assigns = "", DEFAULT_HEADERS.merge("cookie" => []), [], [] 
     8    end 
     9 
     10    def content_type=(mime_type) 
     11      @headers["Content-Type"] = charset ? "#{mime_type}; charset=#{charset}" : mime_type 
     12    end 
     13     
     14    def content_type 
     15      content_type = String(@headers["Content-Type"]).split(";")[0] 
     16      content_type.blank? ? nil : content_type 
     17    end 
     18     
     19    def charset=(encoding) 
     20      @headers["Content-Type"] = "#{content_type || "text/html"}; charset=#{encoding}" 
     21    end 
     22     
     23    def charset 
     24      charset = String(@headers["Content-Type"]).split(";")[1] 
     25      charset.blank? ? nil : charset.strip.split("=")[1] 
    826    end 
    927 
  • trunk/actionpack/lib/action_view/base.rb

    r5095 r5129  
    22 
    33module ActionView #:nodoc: 
    4  
    54  class ActionViewError < StandardError #:nodoc: 
    65  end 
     
    442441          body = case extension.to_sym 
    443442            when :rxml 
     443              "@controller.response.content_type ||= 'application/xml'\n" + 
    444444              "xml = Builder::XmlMarkup.new(:indent => 2)\n" + 
    445               "@controller.headers['Content-Type'] ||= 'application/xml'\n" + 
    446445              template 
    447446            when :rjs 
    448               "@controller.headers['Content-Type'] ||= 'text/javascript'\n" + 
     447              "@controller.response.content_type ||= 'text/javascript'\n" + 
    449448              "update_page do |page|\n#{template}\nend" 
    450449          end 
  • trunk/actionpack/test/controller/action_pack_assertions_test.rb

    r4976 r5129  
    569569  def test_rendering_xml_sets_content_type 
    570570    assert_deprecated(/render/) { process :hello_xml_world } 
    571     assert_equal('application/xml', @controller.headers['Content-Type']) 
     571    assert_equal('application/xml; charset=utf-8', @controller.headers['Content-Type']) 
    572572  end 
    573573 
     
    575575    @response.headers['Content-Type'] = 'application/pdf' 
    576576    assert_deprecated(/render/) { process :hello_xml_world } 
    577     assert_equal('application/pdf', @controller.headers['Content-Type']) 
     577    assert_equal('application/pdf; charset=utf-8', @controller.headers['Content-Type']) 
    578578  end 
    579579 
     
    581581  def test_render_text_with_custom_content_type 
    582582    get :render_text_with_custom_content_type 
    583     assert_equal 'application/rss+xml', @response.headers['Content-Type'] 
     583    assert_equal 'application/rss+xml; charset=utf-8', @response.headers['Content-Type'] 
    584584  end 
    585585end 
  • trunk/actionpack/test/controller/new_render_test.rb

    r4974 r5129  
    262262  def yield_content_for 
    263263    render :action => "content_for", :layout => "yield" 
     264  end 
     265 
     266  def render_content_type_from_body 
     267    response.content_type = Mime::RSS 
     268    render :text => "hello world!" 
    264269  end 
    265270 
     
    573578    get :update_page 
    574579    assert_template nil 
    575     assert_equal 'text/javascript; charset=UTF-8', @response.headers['Content-Type'] 
     580    assert_equal 'text/javascript; charset=utf-8', @response.headers['Content-Type'] 
    576581    assert_equal 2, @response.body.split($/).length 
    577582  end 
     
    580585    get :update_page_with_instance_variables 
    581586    assert_template nil 
    582     assert_equal 'text/javascript; charset=UTF-8', @response.headers['Content-Type'] 
     587    assert_equal 'text/javascript; charset=utf-8', @response.headers['Content-Type'] 
    583588    assert_match /balance/, @response.body 
    584589    assert_match /\$37/, @response.body