De-dup Templates, introduce UnboundTemplate
Previously it's possible to have multiple copies of the "same" Template.
For example, if index.html.erb is found both the :en and :fr locale, it
will return a different Template object for each. The same can happen
with formats, variants, and handlers.
This commit de-duplicates templates, there will now only be one template
per file/virtual_path/locals tuple.
We need to consider virtual_path because both `render "index"`, and
`render "index.html"` can both find the same file but will have
different virtual_paths. IMO this is rare and should be
deprecated/removed, but it exists now so we need to consider it in order
to cache correctly.
This commit introduces a new UnboundTemplate class, which represents a
template with unknown locals. Template objects can be built from it by
using `#with_locals`. Currently, this is just a convenience around
caching templates, but I hope it's a helpful concept that could have
more utility in the future.
2019-04-11 20:14:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "concurrent/map"
|
|
|
|
|
|
|
|
module ActionView
|
|
|
|
class UnboundTemplate
|
2021-05-11 19:32:02 -04:00
|
|
|
attr_reader :virtual_path, :details
|
|
|
|
delegate :locale, :format, :variant, :handler, to: :@details
|
2021-04-20 16:48:12 -04:00
|
|
|
|
2021-05-11 19:32:02 -04:00
|
|
|
def initialize(source, identifier, details:, virtual_path:)
|
De-dup Templates, introduce UnboundTemplate
Previously it's possible to have multiple copies of the "same" Template.
For example, if index.html.erb is found both the :en and :fr locale, it
will return a different Template object for each. The same can happen
with formats, variants, and handlers.
This commit de-duplicates templates, there will now only be one template
per file/virtual_path/locals tuple.
We need to consider virtual_path because both `render "index"`, and
`render "index.html"` can both find the same file but will have
different virtual_paths. IMO this is rare and should be
deprecated/removed, but it exists now so we need to consider it in order
to cache correctly.
This commit introduces a new UnboundTemplate class, which represents a
template with unknown locals. Template objects can be built from it by
using `#with_locals`. Currently, this is just a convenience around
caching templates, but I hope it's a helpful concept that could have
more utility in the future.
2019-04-11 20:14:16 -04:00
|
|
|
@source = source
|
2019-07-19 09:17:43 -04:00
|
|
|
@identifier = identifier
|
2021-05-11 19:32:02 -04:00
|
|
|
@details = details
|
2021-04-20 16:48:12 -04:00
|
|
|
@virtual_path = virtual_path
|
De-dup Templates, introduce UnboundTemplate
Previously it's possible to have multiple copies of the "same" Template.
For example, if index.html.erb is found both the :en and :fr locale, it
will return a different Template object for each. The same can happen
with formats, variants, and handlers.
This commit de-duplicates templates, there will now only be one template
per file/virtual_path/locals tuple.
We need to consider virtual_path because both `render "index"`, and
`render "index.html"` can both find the same file but will have
different virtual_paths. IMO this is rare and should be
deprecated/removed, but it exists now so we need to consider it in order
to cache correctly.
This commit introduces a new UnboundTemplate class, which represents a
template with unknown locals. Template objects can be built from it by
using `#with_locals`. Currently, this is just a convenience around
caching templates, but I hope it's a helpful concept that could have
more utility in the future.
2019-04-11 20:14:16 -04:00
|
|
|
|
|
|
|
@templates = Concurrent::Map.new(initial_capacity: 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
def bind_locals(locals)
|
|
|
|
@templates[locals] ||= build_template(locals)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def build_template(locals)
|
|
|
|
Template.new(
|
|
|
|
@source,
|
2019-07-19 09:17:43 -04:00
|
|
|
@identifier,
|
2021-05-11 19:32:02 -04:00
|
|
|
details.handler_class,
|
2021-04-20 16:48:12 -04:00
|
|
|
|
2021-05-11 19:32:02 -04:00
|
|
|
format: details.format_or_default,
|
|
|
|
variant: variant&.to_s,
|
2021-04-20 16:48:12 -04:00
|
|
|
virtual_path: @virtual_path,
|
|
|
|
|
|
|
|
locals: locals
|
De-dup Templates, introduce UnboundTemplate
Previously it's possible to have multiple copies of the "same" Template.
For example, if index.html.erb is found both the :en and :fr locale, it
will return a different Template object for each. The same can happen
with formats, variants, and handlers.
This commit de-duplicates templates, there will now only be one template
per file/virtual_path/locals tuple.
We need to consider virtual_path because both `render "index"`, and
`render "index.html"` can both find the same file but will have
different virtual_paths. IMO this is rare and should be
deprecated/removed, but it exists now so we need to consider it in order
to cache correctly.
This commit introduces a new UnboundTemplate class, which represents a
template with unknown locals. Template objects can be built from it by
using `#with_locals`. Currently, this is just a convenience around
caching templates, but I hope it's a helpful concept that could have
more utility in the future.
2019-04-11 20:14:16 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|