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

Changeset 8683

Show
Ignore:
Timestamp:
01/21/08 20:45:04 (1 year ago)
Author:
nzkoz
Message:

Reapply the TemplateFinder first applied in [8669] then reverted in [8676]. Closes #10800 [lifofifo]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionmailer/lib/action_mailer/base.rb

    r8212 r8683  
    393393      def register_template_extension(extension) 
    394394        template_extensions << extension 
     395      end 
     396 
     397      def template_root=(root) 
     398        write_inheritable_attribute(:template_root, root) 
     399        ActionView::TemplateFinder.process_view_paths(root) 
    395400      end 
    396401    end 
  • trunk/actionpack/lib/action_controller/base.rb

    r8676 r8683  
    66require 'action_controller/url_rewriter' 
    77require 'action_controller/status_codes' 
     8require 'action_view/template_finder' 
    89require 'drb' 
    910require 'set' 
     
    429430      def view_paths=(value) 
    430431        @view_paths = value 
     432        ActionView::TemplateFinder.process_view_paths(value) 
    431433      end 
    432434 
     
    441443        @view_paths = superclass.view_paths.dup if @view_paths.nil? 
    442444        view_paths.unshift(*path) 
     445        ActionView::TemplateFinder.process_view_paths(path) 
    443446      end 
    444447       
     
    453456        @view_paths = superclass.view_paths.dup if @view_paths.nil? 
    454457        view_paths.push(*path) 
     458        ActionView::TemplateFinder.process_view_paths(path) 
    455459      end 
    456460       
     
    643647      # View load paths for controller. 
    644648      def view_paths 
    645         @template.view_paths 
     649        @template.finder.view_paths 
    646650      end 
    647651     
    648652      def view_paths=(value) 
    649         @template.view_paths = value 
     653        @template.finder.view_paths = value  # Mutex needed 
    650654      end 
    651655 
     
    657661      # 
    658662      def prepend_view_path(path) 
    659         @template.prepend_view_path(path) 
     663        @template.finder.prepend_view_path(path)  # Mutex needed 
    660664      end 
    661665       
     
    667671      # 
    668672      def append_view_path(path) 
    669       @template.append_view_path(path) 
     673        @template.finder.append_view_path(path)  # Mutex needed 
    670674      end 
    671675 
     
    12501254 
    12511255      def template_exists?(template_name = default_template_name) 
    1252         @template.file_exists?(template_name) 
     1256        @template.finder.file_exists?(template_name) 
    12531257      end 
    12541258 
     
    12581262 
    12591263      def template_exempt_from_layout?(template_name = default_template_name) 
    1260         extension = @template && @template.pick_template_extension(template_name) 
     1264        extension = @template && @template.finder.pick_template_extension(template_name) 
    12611265        name_with_extension = !template_name.include?('.') && extension ? "#{template_name}.#{extension}" : template_name 
    12621266        @@exempt_from_layout.any? { |ext| name_with_extension =~ ext } 
  • trunk/actionpack/lib/action_controller/dispatcher.rb

    r8676 r8683  
    140140      if unprepared || force 
    141141        run_callbacks :prepare_dispatch 
     142        ActionView::TemplateFinder.reload! unless ActionView::Base.cache_template_loading 
    142143        self.unprepared = false 
    143144      end 
  • trunk/actionpack/lib/action_controller/layout.rb

    r8676 r8683  
    207207        def normalize_conditions(conditions) 
    208208          conditions.inject({}) {|hash, (key, value)| hash.merge(key => [value].flatten.map {|action| action.to_s})} 
    209         end 
    210          
    211         def layout_directory_exists_cache 
    212           @@layout_directory_exists_cache ||= Hash.new do |h, dirname| 
    213             h[dirname] = File.directory? dirname 
    214           end 
    215209        end 
    216210         
     
    314308      end 
    315309       
    316       # Does a layout directory for this class exist? 
    317       # we cache this info in a class level hash 
    318310      def layout_directory?(layout_name) 
    319         view_paths.find do |path|  
    320           next unless template_path = Dir[File.join(path, 'layouts', layout_name) + ".*"].first 
    321           self.class.send!(:layout_directory_exists_cache)[File.dirname(template_path)] 
    322         end 
     311        @template.finder.find_template_extension_from_handler(File.join('layouts', layout_name)) 
    323312      end 
    324313  end 
  • trunk/actionpack/lib/action_view.rb

    r8676 r8683  
    2727require 'action_view/template_handlers/rjs' 
    2828 
     29require 'action_view/template_finder' 
     30 
    2931require 'action_view/base' 
    3032require 'action_view/partials' 
  • trunk/actionpack/lib/action_view/base.rb

    r8676 r8683  
    151151    include ERB::Util 
    152152 
    153     attr_reader   :first_render 
     153    attr_reader   :first_render, :finder 
    154154    attr_accessor :base_path, :assigns, :template_extension 
    155     attr_accessor :controller, :view_paths 
     155    attr_accessor :controller 
    156156 
    157157    attr_reader :logger, :response, :headers 
     
    205205    # Count the number of inline templates 
    206206    @@inline_template_count = 0 
    207     # Maps template paths without extension to their file extension returned by pick_template_extension. 
    208     # If for a given path, path.ext1 and path.ext2 exist on the file system, the order of extensions 
    209     # used by pick_template_extension determines whether ext1 or ext2 will be stored. 
    210     @@cached_template_extension = {} 
    211     # Maps template paths / extensions to  
    212     @@cached_base_paths = {} 
    213207 
    214208    # Cache public asset paths 
     
    242236    def self.register_template_handler(extension, klass) 
    243237      @@template_handlers[extension.to_sym] = klass 
     238      TemplateFinder.update_extension_cache_for(extension.to_s) 
    244239    end 
    245240 
    246241    def self.template_handler_extensions 
    247       @@template_handler_extensions ||= @@template_handlers.keys.map(&:to_s).sort 
     242      @@template_handlers.keys.map(&:to_s).sort 
    248243    end 
    249244 
     
    266261 
    267262    def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc: 
    268       @view_paths = view_paths.respond_to?(:find) ? view_paths.dup : [*view_paths].compact 
    269263      @assigns = assigns_for_first_render 
    270264      @assigns_added = nil 
    271265      @controller = controller 
    272       @logger = controller && controller.logger  
     266      @logger = controller && controller.logger 
     267      @finder = TemplateFinder.new(self, view_paths) 
    273268    end 
    274269 
     
    290285      end 
    291286       
     287      # Clear the forward slash at the beginning if exists 
     288      template_path = template_path.sub(/^\//, '') if use_full_path 
     289 
    292290      @first_render ||= template_path 
    293       template_path_without_extension, template_extension = path_and_extension(template_path) 
     291      template_path_without_extension, template_extension = @finder.path_and_extension(template_path) 
    294292      if use_full_path 
    295293        if template_extension 
    296           template_file_name = full_template_path(template_path_without_extension, template_extension) 
     294          template_file_name = @finder.pick_template(template_path_without_extension, template_extension) 
    297295        else 
    298           template_extension = pick_template_extension(template_path).to_s 
     296          template_extension = @finder.pick_template_extension(template_path).to_s 
    299297          unless template_extension 
    300             raise ActionViewError, "No template found for #{template_path} in #{view_paths.inspect}" 
     298            raise ActionViewError, "No template found for #{template_path} in #{@finder.view_paths.inspect}" 
    301299          end 
    302           template_file_name = full_template_path(template_path, template_extension) 
     300          template_file_name = @finder.pick_template(template_path, template_extension) 
    303301          template_extension = template_extension.gsub(/^.+\./, '') # strip off any formats 
    304302        end 
     
    310308 
    311309      if template_file_name.blank? 
    312         raise ActionViewError, "Couldn't find template file for #{template_path} in #{view_paths.inspect}" 
     310        raise ActionViewError, "Couldn't find template file for #{template_path} in #{@finder.view_paths.inspect}" 
    313311      end 
    314312 
     
    320318          raise e 
    321319        else 
    322           raise TemplateError.new(find_base_path_for("#{template_path_without_extension}.#{template_extension}") || view_paths.first, template_file_name, @assigns, template_source, e) 
     320          raise TemplateError.new(@finder.find_base_path_for("#{template_path_without_extension}.#{template_extension}") || 
     321                @finder.view_paths.first, template_file_name, @assigns, template_source, e) 
    323322        end 
    324323      end 
     
    372371    end 
    373372 
    374     # Gets the full template path with base path for the given template_path and extension. 
    375     # 
    376     #   full_template_path('users/show', 'html.erb') 
    377     #   # => '~/rails/app/views/users/show.html.erb 
    378     # 
    379     def full_template_path(template_path, extension) 
    380       if @@cache_template_extensions 
    381         (@@cached_base_paths[template_path] ||= {})[extension.to_s] ||= find_full_template_path(template_path, extension) 
    382       else 
    383         find_full_template_path(template_path, extension) 
    384       end 
    385     end 
    386  
    387     # Gets the extension for an existing template with the given template_path. 
    388     # Returns the format with the extension if that template exists. 
    389     # 
    390     #   pick_template_extension('users/show') 
    391     #   # => 'html.erb' 
    392     # 
    393     #   pick_template_extension('users/legacy') 
    394     #   # => "rhtml" 
    395     # 
    396     def pick_template_extension(template_path)#:nodoc: 
    397       if @@cache_template_extensions 
    398         (@@cached_template_extension[template_path] ||= {})[template_format] ||= find_template_extension_for(template_path) 
    399       else 
    400         find_template_extension_for(template_path) 
    401       end 
    402     end 
    403  
    404     def file_exists?(template_path)#:nodoc: 
    405       template_file_name, template_file_extension = path_and_extension(template_path) 
    406       if template_file_extension 
    407         template_exists?(template_file_name, template_file_extension) 
    408       else 
    409         template_exists?(template_file_name, pick_template_extension(template_path)) 
    410       end 
    411     end 
    412  
    413373    # Returns true is the file may be rendered implicitly. 
    414374    def file_public?(template_path)#:nodoc: 
     
    423383    end 
    424384 
    425     # Adds a view_path to the front of the view_paths array. 
    426     # This change affects the current request only. 
    427     # 
    428     #   @template.prepend_view_path("views/default") 
    429     #   @template.prepend_view_path(["views/default", "views/custom"]) 
    430     # 
    431     def prepend_view_path(path) 
    432       @view_paths.unshift(*path) 
    433     end 
    434      
    435     # Adds a view_path to the end of the view_paths array. 
    436     # This change affects the current request only. 
    437     # 
    438     #   @template.append_view_path("views/default") 
    439     #   @template.append_view_path(["views/default", "views/custom"]) 
    440     # 
    441     def append_view_path(path) 
    442       @view_paths.push(*path) 
    443     end 
    444  
    445385    private 
    446386      def wrap_content_for_layout(content) 
     
    448388        @content_for_layout = content 
    449389        returning(yield) { @content_for_layout = original_content_for_layout } 
    450       end 
    451    
    452       def find_full_template_path(template_path, extension) 
    453         file_name = "#{template_path}.#{extension}" 
    454         base_path = find_base_path_for(file_name) 
    455         base_path.blank? ? "" : "#{base_path}/#{file_name}" 
    456       end 
    457  
    458       # Asserts the existence of a template. 
    459       def template_exists?(template_path, extension) 
    460         file_path = full_template_path(template_path, extension) 
    461         !file_path.blank? && @@method_names.has_key?(file_path) || File.exist?(file_path) 
    462       end 
    463  
    464       # Splits the path and extension from the given template_path and returns as an array. 
    465       def path_and_extension(template_path) 
    466         template_path_without_extension = template_path.sub(/\.(\w+)$/, '') 
    467         [ template_path_without_extension, $1 ] 
    468       end 
    469        
    470       # Returns the view path that contains the given relative template path. 
    471       def find_base_path_for(template_file_name) 
    472         view_paths.find { |p| File.file?(File.join(p, template_file_name)) } 
    473       end 
    474  
    475       # Returns the view path that the full path resides in. 
    476       def extract_base_path_from(full_path) 
    477         view_paths.find { |p| full_path[0..p.size - 1] == p } 
    478       end 
    479  
    480       # Determines the template's file extension, such as rhtml, rxml, or rjs. 
    481       def find_template_extension_for(template_path) 
    482         find_template_extension_from_handler(template_path, true) || 
    483         find_template_extension_from_handler(template_path) || 
    484         find_template_extension_from_first_render() 
    485       end 
    486  
    487       def find_template_extension_from_handler(template_path, formatted = nil) 
    488         checked_template_path = formatted ? "#{template_path}.#{template_format}" : template_path 
    489  
    490         self.class.template_handler_extensions.each do |extension| 
    491           if template_exists?(checked_template_path, extension) 
    492             return formatted ? "#{template_format}.#{extension}" : extension.to_s 
    493           end 
    494         end 
    495         nil 
    496       end 
    497        
    498       # Determine the template extension from the <tt>@first_render</tt> filename 
    499       def find_template_extension_from_first_render 
    500         File.basename(@first_render.to_s)[/^[^.]+\.(.+)$/, 1] 
    501390      end 
    502391 
     
    604493          end 
    605494 
    606           raise TemplateError.new(extract_base_path_from(file_name) || view_paths.first, file_name || template, @assigns, template, e) 
     495          raise TemplateError.new(@finder.extract_base_path_from(file_name) || 
     496                @finder.view_paths.first, file_name || template, @assigns, template, e) 
    607497        end 
    608498 
  • trunk/actionpack/lib/action_view/helpers/cache_helper.rb

    r8676 r8683  
    3333      #    <% end %> 
    3434      def cache(name = {}, options = nil, &block) 
    35         template_extension = find_template_extension_for(first_render)[/\.?(\w+)$/, 1].to_sym 
     35        template_extension = @finder.pick_template_extension(first_render)[/\.?(\w+)$/, 1].to_sym 
    3636        handler = Base.handler_class_for_extension(template_extension) 
    3737        handler.new(@controller).cache_fragment(block, name, options) 
  • trunk/actionpack/test/controller/mime_responds_test.rb

    r8676 r8683  
    438438 
    439439class AbstractPostController < ActionController::Base 
    440   class << self 
    441     def view_paths 
    442       [ File.dirname(__FILE__) + "/../fixtures/post_test/" ] 
    443     end 
    444   end 
     440  self.view_paths = File.dirname(__FILE__) + "/../fixtures/post_test/" 
    445441end 
    446442 
  • trunk/actionpack/test/controller/new_render_test.rb

    r8676 r8683  
    7878    @secret = 'in the sauce' 
    7979    render :file => 'test/render_file_with_ivar', :use_full_path => true 
    80   end 
    81   
    82   def render_file_not_using_full_path_with_relative_path 
    83     @secret = 'in the sauce' 
    84     render :file => 'test/../test/render_file_with_ivar', :use_full_path => true 
    8580  end 
    8681   
     
    489484  end 
    490485 
    491   def test_render_file_not_using_full_path_with_relative_path 
    492     get :render_file_not_using_full_path_with_relative_path 
    493     assert_equal "The secret is in the sauce\n", @response.body 
    494   end 
    495  
    496486  def test_render_file_not_using_full_path_with_dot_in_path 
    497487    get :render_file_not_using_full_path_with_dot_in_path 
  • trunk/actionpack/test/controller/render_test.rb

    r8577 r8683  
    1919  def render_hello_world 
    2020    render :template => "test/hello_world" 
     21  end 
     22 
     23  def render_hello_world_with_forward_slash 
     24    render :template => "/test/hello_world" 
    2125  end 
    2226 
     
    222226  end 
    223227 
     228  def test_render_with_forward_slash 
     229    get :render_hello_world_with_forward_slash 
     230    assert_template "test/hello_world" 
     231  end 
     232 
    224233  def test_render_from_variable 
    225234    get :render_hello_world_from_variable 
  • trunk/actionpack/test/controller/view_paths_test.rb

    r8676 r8683  
    1717    private 
    1818    def add_view_path 
    19       self.class.view_paths.unshift "#{LOAD_PATH_ROOT}/override" 
     19      prepend_view_path "#{LOAD_PATH_ROOT}/override" 
    2020    end 
    2121  end 
     
    2828  def setup 
    2929    TestController.view_paths = nil 
    30     ActionView::Base.cache_template_extensions = false 
    3130 
    3231    @request  = ActionController::TestRequest.new 
     
    4645  def teardown 
    4746    ActiveSupport::Deprecation.behavior = @old_behavior 
    48     ActionView::Base.cache_template_extensions = true 
    4947  end 
    5048   
     
    10098   
    10199  def test_view_paths_override 
    102     TestController.view_paths.unshift "#{LOAD_PATH_ROOT}/override" 
     100    TestController.prepend_view_path "#{LOAD_PATH_ROOT}/override" 
    103101    get :hello_world 
    104102    assert_response :success