diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-01-19 03:20:39 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-01-19 03:20:39 +0000 |
commit | 84b0f9c739ffb82f3b8e0571c400014c34d0e369 (patch) | |
tree | cae7ff7cf03695f0ad9483a933a097e5c898d5b2 /actionpack/lib/action_view/base.rb | |
parent | 4a644575c83d78e751f49cb6ba47e1c98af693a8 (diff) | |
download | rails-84b0f9c739ffb82f3b8e0571c400014c34d0e369.tar.gz rails-84b0f9c739ffb82f3b8e0571c400014c34d0e369.tar.bz2 rails-84b0f9c739ffb82f3b8e0571c400014c34d0e369.zip |
Introduce TemplateFinder to handle view paths and lookups. Closes #10800.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8669 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/base.rb')
-rw-r--r-- | actionpack/lib/action_view/base.rb | 145 |
1 files changed, 16 insertions, 129 deletions
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index 34572e442d..bcc6d2ab37 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -150,9 +150,9 @@ module ActionView #:nodoc: class Base include ERB::Util - attr_reader :first_render + attr_reader :first_render, :finder attr_accessor :base_path, :assigns, :template_extension - attr_accessor :controller, :view_paths + attr_accessor :controller attr_reader :logger, :response, :headers attr_internal :cookies, :flash, :headers, :params, :request, :response, :session @@ -204,12 +204,6 @@ module ActionView #:nodoc: @@template_args = {} # Count the number of inline templates @@inline_template_count = 0 - # Maps template paths without extension to their file extension returned by pick_template_extension. - # If for a given path, path.ext1 and path.ext2 exist on the file system, the order of extensions - # used by pick_template_extension determines whether ext1 or ext2 will be stored. - @@cached_template_extension = {} - # Maps template paths / extensions to - @@cached_base_paths = {} # Cache public asset paths cattr_reader :computed_public_paths @@ -241,10 +235,11 @@ module ActionView #:nodoc: # return the rendered template as a string. def self.register_template_handler(extension, klass) @@template_handlers[extension.to_sym] = klass + TemplateFinder.update_extension_cache_for(extension.to_s) end def self.template_handler_extensions - @@template_handler_extensions ||= @@template_handlers.keys.map(&:to_s).sort + @@template_handlers.keys.map(&:to_s).sort end def self.register_default_template_handler(extension, klass) @@ -265,11 +260,11 @@ module ActionView #:nodoc: register_template_handler :rxml, TemplateHandlers::Builder def initialize(view_paths = [], assigns_for_first_render = {}, controller = nil)#:nodoc: - @view_paths = view_paths.respond_to?(:find) ? view_paths.dup : [*view_paths].compact @assigns = assigns_for_first_render @assigns_added = nil @controller = controller - @logger = controller && controller.logger + @logger = controller && controller.logger + @finder = TemplateFinder.new(self, view_paths) end # Renders the template present at <tt>template_path</tt>. If <tt>use_full_path</tt> is set to true, @@ -290,16 +285,16 @@ If you are rendering a subtemplate, you must now use controller-like partial syn end @first_render ||= template_path - template_path_without_extension, template_extension = path_and_extension(template_path) + template_path_without_extension, template_extension = @finder.path_and_extension(template_path) if use_full_path if template_extension - template_file_name = full_template_path(template_path_without_extension, template_extension) + template_file_name = @finder.pick_template(template_path_without_extension, template_extension) else - template_extension = pick_template_extension(template_path).to_s + template_extension = @finder.pick_template_extension(template_path).to_s unless template_extension - raise ActionViewError, "No template found for #{template_path} in #{view_paths.inspect}" + raise ActionViewError, "No template found for #{template_path} in #{@finder.view_paths.inspect}" end - template_file_name = full_template_path(template_path, template_extension) + template_file_name = @finder.pick_template(template_path, template_extension) template_extension = template_extension.gsub(/^.+\./, '') # strip off any formats end else @@ -309,7 +304,7 @@ If you are rendering a subtemplate, you must now use controller-like partial syn template_source = nil # Don't read the source until we know that it is required if template_file_name.blank? - raise ActionViewError, "Couldn't find template file for #{template_path} in #{view_paths.inspect}" + raise ActionViewError, "Couldn't find template file for #{template_path} in #{@finder.view_paths.inspect}" end begin @@ -319,7 +314,8 @@ If you are rendering a subtemplate, you must now use controller-like partial syn e.sub_template_of(template_file_name) raise e else - raise TemplateError.new(find_base_path_for("#{template_path_without_extension}.#{template_extension}") || view_paths.first, template_file_name, @assigns, template_source, e) + raise TemplateError.new(@finder.find_base_path_for("#{template_path_without_extension}.#{template_extension}") || + @finder.view_paths.first, template_file_name, @assigns, template_source, e) end end end @@ -371,45 +367,6 @@ If you are rendering a subtemplate, you must now use controller-like partial syn end end - # Gets the full template path with base path for the given template_path and extension. - # - # full_template_path('users/show', 'html.erb') - # # => '~/rails/app/views/users/show.html.erb - # - def full_template_path(template_path, extension) - if @@cache_template_extensions - (@@cached_base_paths[template_path] ||= {})[extension.to_s] ||= find_full_template_path(template_path, extension) - else - find_full_template_path(template_path, extension) - end - end - - # Gets the extension for an existing template with the given template_path. - # Returns the format with the extension if that template exists. - # - # pick_template_extension('users/show') - # # => 'html.erb' - # - # pick_template_extension('users/legacy') - # # => "rhtml" - # - def pick_template_extension(template_path)#:nodoc: - if @@cache_template_extensions - (@@cached_template_extension[template_path] ||= {})[template_format] ||= find_template_extension_for(template_path) - else - find_template_extension_for(template_path) - end - end - - def file_exists?(template_path)#:nodoc: - template_file_name, template_file_extension = path_and_extension(template_path) - if template_file_extension - template_exists?(template_file_name, template_file_extension) - else - template_exists?(template_file_name, pick_template_extension(template_path)) - end - end - # Returns true is the file may be rendered implicitly. def file_public?(template_path)#:nodoc: template_path.split('/').last[0,1] != '_' @@ -422,83 +379,12 @@ If you are rendering a subtemplate, you must now use controller-like partial syn @template_format = format.blank? ? :html : format.to_sym end - # Adds a view_path to the front of the view_paths array. - # This change affects the current request only. - # - # @template.prepend_view_path("views/default") - # @template.prepend_view_path(["views/default", "views/custom"]) - # - def prepend_view_path(path) - @view_paths.unshift(*path) - end - - # Adds a view_path to the end of the view_paths array. - # This change affects the current request only. - # - # @template.append_view_path("views/default") - # @template.append_view_path(["views/default", "views/custom"]) - # - def append_view_path(path) - @view_paths.push(*path) - end - private def wrap_content_for_layout(content) original_content_for_layout = @content_for_layout @content_for_layout = content returning(yield) { @content_for_layout = original_content_for_layout } end - - def find_full_template_path(template_path, extension) - file_name = "#{template_path}.#{extension}" - base_path = find_base_path_for(file_name) - base_path.blank? ? "" : "#{base_path}/#{file_name}" - end - - # Asserts the existence of a template. - def template_exists?(template_path, extension) - file_path = full_template_path(template_path, extension) - !file_path.blank? && @@method_names.has_key?(file_path) || File.exist?(file_path) - end - - # Splits the path and extension from the given template_path and returns as an array. - def path_and_extension(template_path) - template_path_without_extension = template_path.sub(/\.(\w+)$/, '') - [ template_path_without_extension, $1 ] - end - - # Returns the view path that contains the given relative template path. - def find_base_path_for(template_file_name) - view_paths.find { |p| File.file?(File.join(p, template_file_name)) } - end - - # Returns the view path that the full path resides in. - def extract_base_path_from(full_path) - view_paths.find { |p| full_path[0..p.size - 1] == p } - end - - # Determines the template's file extension, such as rhtml, rxml, or rjs. - def find_template_extension_for(template_path) - find_template_extension_from_handler(template_path, true) || - find_template_extension_from_handler(template_path) || - find_template_extension_from_first_render() - end - - def find_template_extension_from_handler(template_path, formatted = nil) - checked_template_path = formatted ? "#{template_path}.#{template_format}" : template_path - - self.class.template_handler_extensions.each do |extension| - if template_exists?(checked_template_path, extension) - return formatted ? "#{template_format}.#{extension}" : extension.to_s - end - end - nil - end - - # Determine the template extension from the <tt>@first_render</tt> filename - def find_template_extension_from_first_render - File.basename(@first_render.to_s)[/^[^.]+\.(.+)$/, 1] - end # This method reads a template file. def read_template_file(template_path, extension) @@ -603,7 +489,8 @@ If you are rendering a subtemplate, you must now use controller-like partial syn logger.debug "Backtrace: #{e.backtrace.join("\n")}" end - raise TemplateError.new(extract_base_path_from(file_name) || view_paths.first, file_name || template, @assigns, template, e) + raise TemplateError.new(@finder.extract_base_path_from(file_name) || + @finder.view_paths.first, file_name || template, @assigns, template, e) end @@compile_time[render_symbol] = Time.now |