mirror of
https://github.com/github/rails.git
synced 2026-04-04 03:00:58 -04:00
Reduce the number of callsites for new TemplateFiles
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user