Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Use ActionView::TemplateDetails for template priority ordering #2145

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,40 +69,45 @@ def define_render_template_for
template.compile_to_component
end

method_body =
if @templates.one?
@templates.first.safe_method_name
elsif (template = @templates.find(&:inline?))
template.safe_method_name
else
branches = []

@templates.each do |template|
conditional =
if template.inline_call?
"variant&.to_sym == #{template.variant.inspect}"
else
[
template.default_format? ? "(format == #{ViewComponent::Base::VC_INTERNAL_DEFAULT_FORMAT.inspect} || format.nil?)" : "format == #{template.format.inspect}",
template.variant.nil? ? "variant.nil?" : "variant&.to_sym == #{template.variant.inspect}"
].join(" && ")
end

branches << [conditional, template.safe_method_name]
@component.silence_redefinition_of_method(:render_template_for)

if @templates.one?
@component.class_eval <<~RUBY, __FILE__, __LINE__ + 1
def render_template_for(variant = nil, format = nil)
#{@templates.first.safe_method_name}
end
RUBY
elsif (template = @templates.find(&:inline?))
@component.class_eval <<~RUBY, __FILE__, __LINE__ + 1
def render_template_for(variant = nil, format = nil)
#{template.safe_method_name}
end
RUBY
else
templates = @templates
@component.define_method(:render_template_for) do |*|
# Note: using a private method to extract details from lookup context
# This last changed in 2016 when it was changed from protected to
# private but it may not be stable in the future.
requested_details = ActionView::TemplateDetails::Requested.new(
# TODO should options ({}) allow user arguments for format/variant/locale?
# could we use the cache key that is returned as the second argument?
**lookup_context.send(:detail_args_for, {}).first
)

filtered_templates = templates.select do |template|
template.details.matches?(requested_details)
end

out = branches.each_with_object(+"") do |(conditional, branch_body), memo|
memo << "#{(!memo.present?) ? "if" : "elsif"} #{conditional}\n #{branch_body}\n"
if filtered_templates.count > 1
filtered_templates.sort_by! do |template|
template.details.sort_key_for(requested_details)
end
end
out << "else\n #{templates.find { _1.variant.nil? && _1.default_format? }.safe_method_name}\nend"
end

@component.silence_redefinition_of_method(:render_template_for)
@component.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def render_template_for(variant = nil, format = nil)
#{method_body}
send(filtered_templates.first.safe_method_name)
end
end
RUBY
end

def template_errors
Expand Down
9 changes: 8 additions & 1 deletion lib/view_component/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Template
DataWithSource = Struct.new(:format, :identifier, :short_identifier, :type, keyword_init: true)
DataNoSource = Struct.new(:source, :identifier, :type, keyword_init: true)

attr_reader :variant, :this_format, :type
attr_reader :variant, :this_format, :type, :details

def initialize(
component:,
Expand All @@ -32,6 +32,13 @@ def initialize(

@source_originally_nil = @source.nil?

@details = ActionView::TemplateDetails.new(
nil, # currently view components doesn't extract locale from the file path
extension&.to_sym, # e.g. erb, jbuilder, etc
@this_format,
@variant
)

@call_method_name =
if @method_name
@method_name
Expand Down
Loading