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

Changeset 9119

Show
Ignore:
Timestamp:
03/28/08 20:21:52 (5 months ago)
Author:
bitsweat
Message:

Missing :js template falls back to :html, so you don't have to explicitly specify template format everywhere, breaking old code

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_view/template_finder.rb

    r9089 r9119  
    132132    # 
    133133    def pick_template_extension(template_path) 
    134       find_template_extension_from_handler(template_path) || find_template_extension_from_first_render 
     134      if extension = find_template_extension_from_handler(template_path, @template.template_format) || find_template_extension_from_first_render 
     135        extension 
     136      elsif @template.template_format == :js && extension = find_template_extension_from_handler(template_path, :html) 
     137        @template.template_format = :html 
     138        extension 
     139      end 
    135140    end 
    136141 
    137     def find_template_extension_from_handler(template_path
    138       formatted_template_path = "#{template_path}.#{@template.template_format}" 
     142    def find_template_extension_from_handler(template_path, template_format = @template.template_format
     143      formatted_template_path = "#{template_path}.#{template_format}" 
    139144 
    140145      view_paths.each do |path| 
    141146        if (extensions = @@file_extension_cache[path][formatted_template_path]).any? 
    142           return "#{@template.template_format}.#{extensions.first}" 
     147          return "#{template_format}.#{extensions.first}" 
    143148        elsif (extensions = @@file_extension_cache[path][template_path]).any? 
    144149          return extensions.first.to_s 
  • trunk/actionpack/test/template/template_object_test.rb

    r8976 r9119  
    6060  end 
    6161   
     62  class PartialTemplateFallbackTest < Test::Unit::TestCase 
     63    def setup 
     64      @view = ActionView::Base.new(LOAD_PATH_ROOT) 
     65      @path = 'test/layout_for_partial' 
     66    end 
     67 
     68    def test_default 
     69      template = ActionView::PartialTemplate.new(@view, @path, nil) 
     70      assert_equal 'test/_layout_for_partial', template.path 
     71      assert_equal 'erb', template.extension 
     72      assert_equal :html, @view.template_format 
     73    end 
     74 
     75    def test_js 
     76      @view.template_format = :js 
     77      template = ActionView::PartialTemplate.new(@view, @path, nil) 
     78      assert_equal 'test/_layout_for_partial', template.path 
     79      assert_equal 'erb', template.extension 
     80      assert_equal :html, @view.template_format 
     81    end 
     82 
     83    def test_xml 
     84      @view.template_format = :xml 
     85      assert_raise ActionView::ActionViewError do 
     86        ActionView::PartialTemplate.new(@view, @path, nil) 
     87      end 
     88    end 
     89  end 
    6290end