Store details on unbound template

Previously we just stored handler, format, and variant and assigned a
default format if none existed.

Now we want to also store locale, and move the default format behaviour
into unbound template.
This commit is contained in:
John Hawthorn 2021-04-20 13:48:12 -07:00
parent 8944bef657
commit 2be8d3ebf8
2 changed files with 23 additions and 23 deletions

@ -252,16 +252,17 @@ def source_for_template(template)
end
def build_unbound_template(template, virtual_path)
handler, format, variant = extract_handler_and_format_and_variant(template)
details = @path_parser.parse(template)
source = source_for_template(template)
UnboundTemplate.new(
source,
template,
handler,
details.handler,
virtual_path: virtual_path,
format: format,
variant: variant,
locale: details.locale,
format: details.format,
variant: details.variant,
)
end
@ -283,20 +284,6 @@ def escape_entry(entry)
entry.gsub(/[*?{}\[\]]/, '\\\\\\&')
end
# 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
# to the resolver.
def extract_handler_and_format_and_variant(path)
details = @path_parser.parse(path)
handler = Template.handler_for_extension(details.handler)
format = details.format || handler.try(:default_format)
variant = details.variant
# Template::Types[format] and handler.default_format can return nil
[handler, format, variant]
end
def find_template_paths_from_details(path, details)
if path.name.include?(".")
return []

@ -4,11 +4,17 @@
module ActionView
class UnboundTemplate
def initialize(source, identifier, handler, options)
attr_reader :handler, :format, :variant, :locale, :virtual_path
def initialize(source, identifier, handler, format:, variant:, locale:, virtual_path:)
@source = source
@identifier = identifier
@handler = handler
@options = options
@format = format
@variant = variant
@locale = locale
@virtual_path = virtual_path
@templates = Concurrent::Map.new(initial_capacity: 2)
end
@ -19,12 +25,19 @@ def bind_locals(locals)
private
def build_template(locals)
options = @options.merge(locals: locals)
handler = Template.handler_for_extension(@handler)
format = @format || handler.try(:default_format)
Template.new(
@source,
@identifier,
@handler,
**options
handler,
format: format,
variant: @variant,
virtual_path: @virtual_path,
locals: locals
)
end
end