1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Always pass a format to the ActionView::Template constructor

This means we can eliminate nil checks and remove some mutations from
the `decorate` method.
This commit is contained in:
Aaron Patterson 2019-02-25 11:53:55 -08:00
parent 5330a34285
commit 2f128a82e6
No known key found for this signature in database
GPG key ID: 953170BCB4FFAFC6
6 changed files with 26 additions and 13 deletions

View file

@ -39,7 +39,7 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
def call(env) def call(env)
env["action_dispatch.show_detailed_exceptions"] = @detailed env["action_dispatch.show_detailed_exceptions"] = @detailed
req = ActionDispatch::Request.new(env) req = ActionDispatch::Request.new(env)
template = ActionView::Template.new(File.read(__FILE__), __FILE__, ActionView::Template::Handlers::Raw.new, {}) template = ActionView::Template.new(File.read(__FILE__), __FILE__, ActionView::Template::Handlers::Raw.new, format: :html)
case req.path case req.path
when "/pass" when "/pass"

View file

@ -128,8 +128,11 @@ module ActionView
attr_reader :variable attr_reader :variable
def initialize(source, identifier, handler, details) def initialize(source, identifier, handler, format: nil, **details)
format = details[:format] || (handler.default_format if handler.respond_to?(:default_format)) unless format
ActiveSupport::Deprecation.warn "ActionView::Template#initialize requires a format parameter"
format = :html
end
@source = source @source = source
@identifier = identifier @identifier = identifier

View file

@ -196,7 +196,6 @@ module ActionView
cached = nil cached = nil
templates.each do |t| templates.each do |t|
t.locals = locals t.locals = locals
t.formats = details[:formats] || [:html] if t.formats.empty?
t.variants = details[:variants] || [] if t.variants.empty? t.variants = details[:variants] || [] if t.variants.empty?
t.virtual_path ||= (cached ||= build_path(*path_info)) t.virtual_path ||= (cached ||= build_path(*path_info))
end end
@ -225,7 +224,7 @@ module ActionView
template_paths = reject_files_external_to_app(template_paths) unless outside_app_allowed template_paths = reject_files_external_to_app(template_paths) unless outside_app_allowed
template_paths.map do |template| template_paths.map do |template|
handler, format, variant = extract_handler_and_format_and_variant(template) handler, format, variant = extract_handler_and_format_and_variant(template, formats.first)
FileTemplate.new(File.expand_path(template), handler, FileTemplate.new(File.expand_path(template), handler,
virtual_path: path.virtual, virtual_path: path.virtual,
@ -292,7 +291,7 @@ module ActionView
# Extract handler, formats and variant from path. If a format cannot be found neither # Extract handler, formats and variant from path. If a format cannot be found neither
# from the path, or the handler, we should return the array of formats given # from the path, or the handler, we should return the array of formats given
# to the resolver. # to the resolver.
def extract_handler_and_format_and_variant(path) def extract_handler_and_format_and_variant(path, query_format)
pieces = File.basename(path).split(".") pieces = File.basename(path).split(".")
pieces.shift pieces.shift
@ -300,9 +299,18 @@ module ActionView
handler = Template.handler_for_extension(extension) handler = Template.handler_for_extension(extension)
format, variant = pieces.last.split(EXTENSIONS[:variants], 2) if pieces.last format, variant = pieces.last.split(EXTENSIONS[:variants], 2) if pieces.last
format &&= Template::Types[format] format = if format
Template::Types[format]
else
if handler.respond_to?(:default_format) # default_format can return nil
handler.default_format
else
query_format
end
end
[handler, format, variant] # Template::Types[format] and handler.default_format can return nil
[handler, format || query_format, variant]
end end
end end

View file

@ -34,7 +34,7 @@ module ActionView #:nodoc:
@hash.each do |_path, array| @hash.each do |_path, array|
source, updated_at = array source, updated_at = array
next unless query.match?(_path) next unless query.match?(_path)
handler, format, variant = extract_handler_and_format_and_variant(_path) handler, format, variant = extract_handler_and_format_and_variant(_path, :html)
templates << Template.new(source, _path, handler, templates << Template.new(source, _path, handler,
virtual_path: path.virtual, virtual_path: path.virtual,
format: format, format: format,
@ -49,7 +49,7 @@ module ActionView #:nodoc:
class NullResolver < PathResolver class NullResolver < PathResolver
def query(path, exts, _, _) def query(path, exts, _, _)
handler, format, variant = extract_handler_and_format_and_variant(path) handler, format, variant = extract_handler_and_format_and_variant(path, :html)
[ActionView::Template.new("Template generated by Null Resolver", path.virtual, handler, virtual_path: path.virtual, format: format, variant: variant)] [ActionView::Template.new("Template generated by Null Resolver", path.virtual, handler, virtual_path: path.virtual, format: format, variant: variant)]
end end
end end

View file

@ -60,7 +60,7 @@ module RenderERBUtils
string.strip, string.strip,
"test template", "test template",
ActionView::Template.handler_for_extension(:erb), ActionView::Template.handler_for_extension(:erb),
{}) format: :html)
view = ActionView::Base.with_empty_template_cache view = ActionView::Base.with_empty_template_cache
template.render(view.empty, {}).strip template.render(view.empty, {}).strip

View file

@ -38,7 +38,8 @@ class TestERBTemplate < ActiveSupport::TestCase
"<%= @virtual_path %>", "<%= @virtual_path %>",
"partial", "partial",
ERBHandler, ERBHandler,
virtual_path: "partial" virtual_path: "partial",
format: :html
) )
end end
@ -55,7 +56,8 @@ class TestERBTemplate < ActiveSupport::TestCase
end end
end end
def new_template(body = "<%= hello %>", details = { format: :html }) def new_template(body = "<%= hello %>", details = {})
details = { format: :html }.merge details
ActionView::Template.new(body.dup, "hello template", details.fetch(:handler) { ERBHandler }, { virtual_path: "hello" }.merge!(details)) ActionView::Template.new(body.dup, "hello template", details.fetch(:handler) { ERBHandler }, { virtual_path: "hello" }.merge!(details))
end end