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

Changeset 6499

Show
Ignore:
Timestamp:
04/02/07 00:02:11 (1 year ago)
Author:
rick
Message:

Change ActionView template defaults. Look for templates using the request format first, such as show.html.erb or show.xml.builder, before looking for the old defaults like show.erb or show.builder [Rick]

Files:

Legend:

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

    r6493 r6499  
    11*SVN* 
     2 
     3* Change ActionView template defaults.  Look for templates using the request format first, such as "show.html.erb" or "show.xml.builder", before looking for the old defaults like "show.erb" or "show.builder" [Rick] 
    24 
    35* Highlight helper highlights one or many terms in a single pass.  [Jeremy Kemper] 
  • trunk/actionpack/lib/action_controller/base.rb

    r6435 r6499  
    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, 'erb'
     1228          full_template_path = @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_view/base.rb

    r6470 r6499  
    253253          template_extension = pick_template_extension(template_path).to_s 
    254254          template_file_name = full_template_path(template_path, template_extension) 
     255          template_extension.gsub!(/^\w+\./, '') # strip off any formats 
    255256        end 
    256257      else 
     
    331332 
    332333    def pick_template_extension(template_path)#:nodoc: 
     334      formatted_template_path = "#{template_path}.#{template_format}" 
    333335      if @@cache_template_extensions 
    334         @@cached_template_extension[template_path] ||= find_template_extension_for(template_path) 
     336        @@cached_template_extension[formatted_template_path] ||= find_template_extension_for(template_path, formatted_template_path) 
    335337      else 
    336         find_template_extension_for(template_path
     338        find_template_extension_for(template_path, formatted_template_path
    337339      end 
    338340    end 
     
    358360    def javascript_template_exists?(template_path)#:nodoc: 
    359361      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 
    360369    end 
    361370 
     
    365374        template_exists?(template_file_name, template_file_extension) 
    366375      else 
    367         cached_template_extension(template_path) || 
    368            %w(erb rhtml builder rxml javascript delegate).any? do |template_type| 
    369              send("#{template_type}_template_exists?", template_path) 
    370            end 
     376        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) 
     381          end 
    371382      end 
    372383    end 
     
    375386    def file_public?(template_path)#:nodoc: 
    376387      template_path.split('/').last[0,1] != '_' 
     388    end 
     389 
     390    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 
    377399    end 
    378400 
     
    390412      end 
    391413 
     414      # Splits the path and extension from the given template_path and returns as an array. 
    392415      def path_and_extension(template_path) 
    393416        template_path_without_extension = template_path.sub(/\.(\w+)$/, '') 
     
    395418      end 
    396419 
    397       def cached_template_extension(template_path) 
    398         @@cache_template_extensions && @@cached_template_extension[template_path] 
     420      # Caches the extension for the given formatted template path.  The extension may have the format 
     421      # too, such as 'html.erb'. 
     422      def cached_template_extension(formatted_template_path) 
     423        @@cache_template_extensions && @@cached_template_extension[formatted_template_path] 
    399424      end 
    400425       
     
    405430 
    406431      # Determines the template's file extension, such as rhtml, rxml, or rjs. 
    407       def find_template_extension_for(template_path) 
     432      def find_template_extension_for(template_path, formatted_template_path = nil) 
     433        formatted_template_path ||= "#{template_path}.#{template_format}" 
    408434        if match = delegate_template_exists?(template_path) 
    409435          match.first.to_sym 
    410         elsif extension = erb_template_exists?(template_path):        extension 
    411         elsif extension = builder_template_exists?(template_path):    extension 
     436        elsif extension = formatted_template_exists?(formatted_template_path): "#{template_format}.#{extension}" 
     437        elsif extension = erb_template_exists?(template_path):                 extension 
     438        elsif extension = builder_template_exists?(template_path):             extension 
    412439        elsif javascript_template_exists?(template_path): :rjs 
    413440        else 
  • trunk/actionpack/test/controller/render_test.rb

    r6168 r6499  
    121121           :locals => { "local_name" => name } 
    122122    ActionView::Base.local_assigns_support_string_keys = false 
     123  end 
     124 
     125  def formatted_html_erb 
     126  end 
     127 
     128  def formatted_xml_erb 
    123129  end 
    124130 
     
    342348  end 
    343349 
     350  def test_should_render_formatted_template 
     351    get :formatted_html_erb 
     352    assert_equal 'formatted html erb', @response.body 
     353  end 
     354   
     355  def test_should_render_formatted_xml_erb_template 
     356    get :formatted_xml_erb, :format => :xml 
     357    assert_equal '<test>passed formatted xml erb</test>', @response.body 
     358  end 
     359   
     360  def test_should_render_formatted_html_erb_template 
     361    get :formatted_xml_erb 
     362    assert_equal '<test>passed formatted html erb</test>', @response.body 
     363  end 
    344364 
    345365  protected 
  • trunk/actionpack/test/template/base_test.rb

    r6498 r6499  
    1515    end 
    1616     
     17    def test_should_find_formatted_erb_extension 
     18      @template.expects(:delegate_template_exists?).with('foo').returns(nil) 
     19      @template.expects(:formatted_template_exists?).with('foo.html').returns("erb") 
     20      assert_equal "html.erb", @template.send(:find_template_extension_for, 'foo') 
     21    end 
     22     
    1723    def test_should_find_erb_extension 
    1824      @template.expects(:delegate_template_exists?).with('foo').returns(nil) 
     25      @template.expects(:formatted_template_exists?).with('foo.html').returns(nil) 
    1926      @template.expects(:erb_template_exists?).with('foo').returns(:erb) 
    2027      assert_equal :erb, @template.send(:find_template_extension_for, 'foo') 
     
    2330    def test_should_find_builder_extension 
    2431      @template.expects(:delegate_template_exists?).with('foo').returns(nil) 
     32      @template.expects(:formatted_template_exists?).with('foo.html').returns(nil) 
    2533      @template.expects(:erb_template_exists?).with('foo').returns(nil) 
    2634      @template.expects(:builder_template_exists?).with('foo').returns(:builder) 
     
    3038    def test_should_find_javascript_extension 
    3139      @template.expects(:delegate_template_exists?).with('foo').returns(nil) 
     40      @template.expects(:formatted_template_exists?).with('foo.html').returns(nil) 
    3241      @template.expects(:erb_template_exists?).with('foo').returns(nil) 
    3342      @template.expects(:builder_template_exists?).with('foo').returns(nil)