diff options
author | Joshua Peek <josh@joshpeek.com> | 2008-07-03 14:01:45 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2008-07-03 14:01:45 -0500 |
commit | 1a478923dc909bf7b6aea4f2ad49cbeee6dea259 (patch) | |
tree | 44cf634937e470352b6eafde6d0169ab0b5fbf96 /actionpack/lib | |
parent | 8a442e0d57fabedcaf3ded836cfc12f7067ead68 (diff) | |
download | rails-1a478923dc909bf7b6aea4f2ad49cbeee6dea259.tar.gz rails-1a478923dc909bf7b6aea4f2ad49cbeee6dea259.tar.bz2 rails-1a478923dc909bf7b6aea4f2ad49cbeee6dea259.zip |
Reduce the number of callsites for new TemplateFiles
Diffstat (limited to 'actionpack/lib')
-rwxr-xr-x | actionpack/lib/action_controller/base.rb | 5 | ||||
-rw-r--r-- | actionpack/lib/action_view/base.rb | 28 | ||||
-rw-r--r-- | actionpack/lib/action_view/view_load_paths.rb | 22 |
3 files changed, 33 insertions, 22 deletions
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index 009c9eec99..a4bade1bd5 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -1235,8 +1235,9 @@ module ActionController #:nodoc: end def template_exempt_from_layout?(template_name = default_template_name) - template_name = @template.send(:template_file_from_name, template_name) if @template - @@exempt_from_layout.any? { |ext| template_name.to_s =~ ext } + extension = @template && @template.pick_template_extension(template_name) + name_with_extension = !template_name.include?('.') && extension ? "#{template_name}.#{extension}" : template_name + @@exempt_from_layout.any? { |ext| name_with_extension =~ ext } end def default_template_name(action_name = self.action_name) diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb index d4802d7965..5a3fc4182f 100644 --- a/actionpack/lib/action_view/base.rb +++ b/actionpack/lib/action_view/base.rb @@ -284,6 +284,21 @@ module ActionView #:nodoc: view_paths.template_exists?(template_file_from_name(template_path)) 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) + if template = template_file_from_name(template_path) + template.extension + end + end + private # Renders the template present at <tt>template_path</tt>. The hash in <tt>local_assigns</tt> # is made available as local variables. @@ -336,19 +351,10 @@ module ActionView #:nodoc: def template_file_from_name(template_name) template_name = TemplateFile.from_path(template_name) - pick_template_extension(template_name) unless template_name.extension + pick_template(template_name) unless template_name.extension 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(file) + def pick_template(file) if f = self.view_paths.find_template_file_for_path(file.dup_with_extension(template_format)) || file_from_first_render(file) f elsif template_format == :js && f = self.view_paths.find_template_file_for_path(file.dup_with_extension(:html)) diff --git a/actionpack/lib/action_view/view_load_paths.rb b/actionpack/lib/action_view/view_load_paths.rb index f23ac665f1..6e439a009c 100644 --- a/actionpack/lib/action_view/view_load_paths.rb +++ b/actionpack/lib/action_view/view_load_paths.rb @@ -29,12 +29,10 @@ module ActionView #:nodoc: @paths.freeze end - # Tries to find the extension for the template name. - # If it does not it exist, tries again without the format extension - # find_template_file_for_partial_path('users/show') => 'html.erb' - # find_template_file_for_partial_path('users/legacy') => 'rhtml' - def find_template_file_for_partial_path(file) - @paths[file.path] || @paths[file.path_without_extension] || @paths[file.path_without_format_and_extension] + def find_template_file_for_partial_path(template_path, template_format) + @paths["#{template_path}.#{template_format}"] || + @paths[template_path] || + @paths[template_path.gsub(/\..*$/, '')] end private @@ -81,10 +79,10 @@ module ActionView #:nodoc: find { |path| path.paths[file.to_s] } end - def find_template_file_for_path(file) - file = TemplateFile.from_path(file) + def find_template_file_for_path(template_path) + template_path_without_extension, template_extension = path_and_extension(template_path.to_s) each do |path| - if f = path.find_template_file_for_partial_path(file) + if f = path.find_template_file_for_partial_path(template_path_without_extension, template_extension) return f end end @@ -95,5 +93,11 @@ module ActionView #:nodoc: def delete_paths!(paths) paths.each { |p1| delete_if { |p2| p1.to_s == p2.to_s } } 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 end end |