Skip to content

Commit

Permalink
Merge pull request #41997 from jhawthorn/unbound_template_details
Browse files Browse the repository at this point in the history
Cleanups to Resolver/UnboundTemplate details
  • Loading branch information
jhawthorn committed Apr 20, 2021
2 parents b9788b7 + bf9edcb commit bc1bc32
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 37 deletions.
52 changes: 20 additions & 32 deletions actionview/lib/action_view/template/resolver.rb
Expand Up @@ -35,6 +35,8 @@ def to_str
alias :to_s :to_str
end

TemplateDetails = Struct.new(:path, :locale, :handler, :format, :variant)

class PathParser # :nodoc:
def build_path_regex
handlers = Template::Handlers.extensions.map { |x| Regexp.escape(x) }.join("|")
Expand All @@ -58,15 +60,14 @@ def build_path_regex
def parse(path)
@regex ||= build_path_regex
match = @regex.match(path)
{
prefix: match[:prefix] || "",
action: match[:action],
partial: !!match[:partial],
locale: match[:locale]&.to_sym,
handler: match[:handler]&.to_sym,
format: match[:format]&.to_sym,
variant: match[:variant]
}
path = Path.build(match[:action], match[:prefix] || "", !!match[:partial])
TemplateDetails.new(
path,
match[:locale]&.to_sym,
match[:handler]&.to_sym,
match[:format]&.to_sym,
match[:variant]
)
end
end

Expand Down Expand Up @@ -235,11 +236,11 @@ def query(path, details, formats, locals, cache:)
template_paths.map do |template|
unbound_template =
if cache
@unbound_templates.compute_if_absent([template, path.virtual]) do
build_unbound_template(template, path.virtual)
@unbound_templates.compute_if_absent(template) do
build_unbound_template(template)
end
else
build_unbound_template(template, path.virtual)
build_unbound_template(template)
end

unbound_template.bind_locals(locals)
Expand All @@ -250,17 +251,18 @@ def source_for_template(template)
Template::Sources::File.new(template)
end

def build_unbound_template(template, virtual_path)
handler, format, variant = extract_handler_and_format_and_variant(template)
def build_unbound_template(template)
details = @path_parser.parse(template.from(@path.size + 1))
source = source_for_template(template)

UnboundTemplate.new(
source,
template,
handler,
virtual_path: virtual_path,
format: format,
variant: variant,
details.handler,
virtual_path: details.path.virtual,
locale: details.locale,
format: details.format,
variant: details.variant,
)
end

Expand All @@ -282,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 []
Expand Down
23 changes: 18 additions & 5 deletions actionview/lib/action_view/unbound_template.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit bc1bc32

Please sign in to comment.