Skip to content

Commit

Permalink
Reduce the number of callsites for new TemplateFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Jul 3, 2008
1 parent 8a442e0 commit 1a47892
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
5 changes: 3 additions & 2 deletions actionpack/lib/action_controller/base.rb
Expand Up @@ -1235,8 +1235,9 @@ def template_public?(template_name = default_template_name)
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)
Expand Down
28 changes: 17 additions & 11 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -284,6 +284,21 @@ def file_exists?(template_path)
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.
Expand Down Expand Up @@ -336,19 +351,10 @@ def execute(template)

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))
Expand Down
22 changes: 13 additions & 9 deletions actionpack/lib/action_view/view_load_paths.rb
Expand Up @@ -29,12 +29,10 @@ def reload!
@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
Expand Down Expand Up @@ -81,10 +79,10 @@ def find_load_path_for_path(file)
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
Expand All @@ -95,5 +93,11 @@ def find_template_file_for_path(file)
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

0 comments on commit 1a47892

Please sign in to comment.