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

Changeset 6517

Show
Ignore:
Timestamp:
04/12/07 20:25:32 (1 year ago)
Author:
rick
Message:

The default respond_to blocks don't set a specific extension anymore, so that both 'show.rjs' and 'show.js.rjs' will work. [Rick]

Files:

Legend:

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

    r6516 r6517  
    11*SVN* 
     2 
     3* The default respond_to blocks don't set a specific extension anymore, so that both 'show.rjs' and 'show.js.rjs' will work. [Rick] 
    24 
    35* Allow layouts with extension of .html.erb.  Closes #8032 [Josh Knowles] 
  • trunk/actionpack/lib/action_controller/base.rb

    r6499 r6517  
    12261226      def assert_existence_of_template_file(template_name) 
    12271227        unless template_exists?(template_name) || ignore_missing_templates 
    1228           full_template_path = @template.send(:full_template_path, template_name, "#{@template.send(:template_format)}.erb") 
     1228          full_template_path = template_name.include?('.') ? template_name : @template.send(:full_template_path, template_name, "#{@template.send(:template_format)}.erb") 
    12291229          template_type = (template_name =~ /layouts/i) ? 'layout' : 'template' 
    12301230          raise(MissingTemplate, "Missing #{template_type} #{full_template_path}") 
  • trunk/actionpack/lib/action_controller/mime_responds.rb

    r6507 r6517  
    108108     
    109109    class Responder #:nodoc: 
    110       default_block_format = %(Proc.new { render :action => "\#{action_name}%s", :content_type => Mime::%s }) 
    111       DEFAULT_BLOCKS = {} 
    112       DEFAULT_BLOCKS[:html] = default_block_format % ['', 'HTML'] 
    113       DEFAULT_BLOCKS[:js]   = default_block_format % ['.js.rjs', 'JS'] 
    114       DEFAULT_BLOCKS[:xml]  = default_block_format % ['.xml.builder', 'XML'] 
     110      default_block_format = <<-END 
     111        Proc.new {  
     112          @template.template_format = '%s' 
     113          render :action => "\#{action_name}", :content_type => Mime::%s 
     114        } 
     115      END 
     116 
     117      DEFAULT_BLOCKS = [:html, :js, :xml].inject({}) do |memo, ext|  
     118        default_block = default_block_format % [ext, ext.to_s.upcase] 
     119        memo.update(ext => default_block) 
     120      end 
    115121       
    116122      def initialize(block_binding) 
     
    133139        if block_given? 
    134140          @responses[mime_type] = Proc.new do 
    135             eval "response.content_type = '#{mime_type.to_s}'", @block_binding 
     141            eval <<-END, @block_binding 
     142              @template.template_format = '#{mime_type.to_sym}' 
     143              response.content_type     = '#{mime_type.to_s}' 
     144            END 
    136145            block.call 
    137146          end 
  • trunk/actionpack/lib/action_controller/request.rb

    r6451 r6517  
    99    # such as { 'RAILS_ENV' => 'production' }. 
    1010    attr_reader :env 
     11 
     12    attr_accessor :format 
    1113 
    1214    # Returns the HTTP request method as a lowercase symbol (:get, for example). Note, HEAD is returned as :get 
     
    9294    #   GET /posts/5       | request.format => request.accepts.first (usually Mime::HTML for browsers) 
    9395    def format 
    94       parameters[:format] ? Mime::Type.lookup_by_extension(parameters[:format]) : accepts.first 
     96      @format ||= parameters[:format] ? Mime::Type.lookup_by_extension(parameters[:format]) : accepts.first 
    9597    end 
    9698 
  • trunk/actionpack/lib/action_view/base.rb

    r6509 r6517  
    159159    attr_reader :logger, :response, :headers, :view_paths 
    160160    attr_internal :cookies, :flash, :headers, :params, :request, :response, :session 
     161     
     162    attr_writer :template_format 
    161163 
    162164    # Specify trim mode for the ERB compiler. Defaults to '-'. 
     
    208210    @@cached_template_extension = {} 
    209211 
     212    # Order of template handers checked by #file_exists? depending on the current #template_format 
     213    DEFAULT_TEMPLATE_HANDLER_PREFERENCE = %w(erb rhtml builder rxml javascript delegate) 
     214    TEMPLATE_HANDLER_PREFERENCES = { 
     215      :js       => %w(javascript erb rhtml builder rxml delegate), 
     216      :xml      => %w(builder rxml erb rhtml javascript delegate), 
     217      :delegate => %w(delegate) 
     218    } 
     219 
    210220    class ObjectWrapper < Struct.new(:value) #:nodoc: 
    211221    end 
     
    230240    # return the rendered template as a string. 
    231241    def self.register_template_handler(extension, klass) 
     242      TEMPLATE_HANDLER_PREFERENCES[extension.to_sym] = TEMPLATE_HANDLER_PREFERENCES[:delegate] 
    232243      @@template_handlers[extension] = klass 
    233244    end 
     
    332343 
    333344    def pick_template_extension(template_path)#:nodoc: 
    334       formatted_template_path = "#{template_path}.#{template_format}" 
    335345      if @@cache_template_extensions 
    336         @@cached_template_extension[formatted_template_path] ||= find_template_extension_for(template_path, formatted_template_path) 
     346        formatted_template_path = "#{template_path}.#{template_format}" 
     347        @@cached_template_extension[formatted_template_path] ||= find_template_extension_for(template_path) 
    337348      else 
    338         find_template_extension_for(template_path, formatted_template_path
     349        find_template_extension_for(template_path
    339350      end 
    340351    end 
    341352 
    342353    def delegate_template_exists?(template_path)#:nodoc: 
    343       @@template_handlers.find { |k,| template_exists?(template_path, k) } 
    344     end 
    345      
    346     def one_of(template_path, *extensions)#:nodoc: 
    347       extensions.detect{|ext| template_exists?(template_path, ext)} 
    348     end 
    349      
     354      delegate = @@template_handlers.find { |k,| template_exists?(template_path, k) } 
     355      delegate && delegate.first.to_sym 
     356    end 
     357 
    350358    def erb_template_exists?(template_path)#:nodoc: 
    351       one_of(template_path, :erb, :rhtml) 
    352     end 
    353     alias :rhtml_template_exists? :erb_template_exists? 
    354      
     359      template_exists?(template_path, :erb) && :erb 
     360    end 
     361 
    355362    def builder_template_exists?(template_path)#:nodoc: 
    356       one_of(template_path, :builder, :rxml) 
    357     end 
    358     alias :rxml_template_exists? :builder_template_exists? 
    359      
     363      template_exists?(template_path, :builder) && :builder 
     364    end 
     365 
     366    def rhtml_template_exists?(template_path)#:nodoc: 
     367      template_exists?(template_path, :rhtml) && :rhtml 
     368    end 
     369 
     370    def rxml_template_exists?(template_path)#:nodoc: 
     371      template_exists?(template_path, :rxml) && :rxml 
     372    end 
     373 
    360374    def javascript_template_exists?(template_path)#:nodoc: 
    361       template_exists?(template_path, :rjs) 
    362     end 
    363  
    364     def formatted_template_exists?(formatted_template_exists) 
    365       [:erb, :builder, :rjs].each do |ext| 
    366         return ext if template_exists?(formatted_template_exists, ext) 
    367       end 
    368       nil 
     375      template_exists?(template_path, :rjs) && :rjs 
    369376    end 
    370377 
     
    372379      template_file_name, template_file_extension = path_and_extension(template_path) 
    373380      if template_file_extension 
    374         template_exists?(template_file_name, template_file_extension) 
     381        template_exists?(template_file_name, template_file_extension) && template_file_extension 
    375382      else 
    376383        formatted_template_path = "#{template_path}.#{template_format}" 
    377         cached_template_extension(formatted_template_path) || 
    378           formatted_template_exists?(formatted_template_path) |
    379           %w(erb rhtml builder rxml javascript delegate).any? do |template_type| 
    380             send("#{template_type}_template_exists?", template_path) 
     384        return true if cached_template_extension(formatted_template_path) 
     385        template_handler_preferences.each do |template_type
     386          if extension = send("#{template_type}_template_exists?", formatted_template_path) 
     387            return "#{template_format}.#{extension}" 
    381388          end 
     389        end 
     390        template_handler_preferences.each do |template_type| 
     391          if extension = send("#{template_type}_template_exists?", template_path) 
     392            return extension 
     393          end 
     394        end 
     395        nil 
    382396      end 
    383397    end 
     
    388402    end 
    389403 
     404    # symbolized version of the :format parameter of the request, or :html by default. 
    390405    def template_format 
    391       if @template_format != false 
    392         # check controller.respond_to?(:request) in case its an ActionMailer::Base, or some other sneaky class. 
    393         @template_format = controller.respond_to?(:request) ? false : :html 
    394         if controller && controller.respond_to?(:request) && controller.request && controller.request.format 
    395           @template_format = controller.request.format == Mime::ALL ? :html : controller.request.format.to_sym 
    396         end 
    397       end 
    398       @template_format 
     406      @template_format ||= controller.request.parameters[:format].to_sym rescue :html 
     407    end 
     408 
     409    def template_handler_preferences 
     410      TEMPLATE_HANDLER_PREFERENCES[template_format] || DEFAULT_TEMPLATE_HANDLER_PREFERENCE 
    399411    end 
    400412 
     
    435447 
    436448      # Determines the template's file extension, such as rhtml, rxml, or rjs. 
    437       def find_template_extension_for(template_path, formatted_template_path = nil) 
    438         formatted_template_path ||= "#{template_path}.#{template_format}" 
    439         if match = delegate_template_exists?(template_path) 
    440           match.first.to_sym 
    441         elsif extension = formatted_template_exists?(formatted_template_path): "#{template_format}.#{extension}" 
    442         elsif extension = erb_template_exists?(template_path):                 extension 
    443         elsif extension = builder_template_exists?(template_path):             extension 
    444         elsif javascript_template_exists?(template_path): :rjs 
     449      def find_template_extension_for(template_path) 
     450        if extension = file_exists?(template_path) 
     451          return extension 
    445452        else 
    446453          raise ActionViewError, "No erb, builder, rhtml, rxml, rjs or delegate template found for #{template_path} in #{@view_paths.inspect}" 
  • trunk/actionpack/test/controller/mime_responds_test.rb

    r6507 r6517  
    289289  end 
    290290   
    291   def test_all_types_with_layout 
     291  def test_rjs_type_skips_layout 
    292292    @request.env["HTTP_ACCEPT"] = "text/javascript" 
    293293    get :all_types_with_layout 
    294294    assert_equal 'RJS for all_types_with_layout', @response.body 
    295  
     295  end 
     296   
     297  def test_html_type_with_layout 
    296298    @request.env["HTTP_ACCEPT"] = "text/html" 
    297299    get :all_types_with_layout 
     
    344346          @action = args.first[:action] 
    345347        end 
    346         response.body = @action 
     348        response.body = "#{@action} - #{@template.template_format}" 
    347349      end 
    348350    end 
    349351 
    350352    get :using_defaults 
    351     assert_equal "using_defaults", @response.body 
     353    assert_equal "using_defaults - html", @response.body 
    352354 
    353355    get :using_defaults, :format => "xml" 
    354     assert_equal "using_defaults.xml.builder", @response.body 
     356    assert_equal "using_defaults - xml", @response.body 
    355357  end 
    356358end 
  • trunk/actionpack/test/controller/render_test.rb

    r6499 r6517  
    362362    assert_equal '<test>passed formatted html erb</test>', @response.body 
    363363  end 
     364   
     365  def test_should_render_formatted_html_erb_template_with_faulty_accepts_header 
     366    @request.env["HTTP_ACCEPT"] = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, appliction/x-shockwave-flash, */*" 
     367    get :formatted_xml_erb 
     368    assert_equal '<test>passed formatted html erb</test>', @response.body 
     369  end 
    364370 
    365371  protected 
  • trunk/actionpack/test/controller/request_test.rb

    r6340 r6517  
    310310  end 
    311311 
    312   def test_format 
     312  def test_xml_format 
    313313    @request.instance_eval { @parameters = { :format => 'xml' } } 
    314314    assert_equal Mime::XML, @request.format 
    315  
     315  end 
     316   
     317  def test_xhtml_format 
    316318    @request.instance_eval { @parameters = { :format => 'xhtml' } } 
    317319    assert_equal Mime::HTML, @request.format 
    318  
     320  end 
     321   
     322  def test_txt_format 
    319323    @request.instance_eval { @parameters = { :format => 'txt' } } 
    320324    assert_equal Mime::TEXT, @request.format 
    321  
     325  end 
     326 
     327  def test_nil_format 
    322328    @request.instance_eval { @parameters = { :format => nil } } 
    323329    @request.env["HTTP_ACCEPT"] = "text/javascript"