Skip to content
Merged
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
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 6

## main

* Fix inherited `#call` methods overriding subclass sidecar templates for non-HTML requests.

*Joel Hawksley*

* Pin Chrome in all Primer ViewComponents compatibility suites to match upstream system tests.

*Joel Hawksley*
Expand Down
7 changes: 5 additions & 2 deletions lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def find_templates_for(requested_details)
#
# @return [Array<Symbol>]
def template_formats
gather_templates.map { |template| template.format || :html }.uniq
gather_templates.map { |template| template.format || Template::DEFAULT_FORMAT }.uniq
end

# @private
Expand Down Expand Up @@ -255,7 +255,10 @@ def gather_templates
if inherited.any?
# A format and a variant can share a public call_* alias.
inherited.each do |template|
next if templates.any? { |own| own.variant == template.variant && own.format == template.format }
next if templates.any? do |own|
own.variant == template.variant &&
(own.format || Template::DEFAULT_FORMAT) == (template.format || Template::DEFAULT_FORMAT)
end

templates << Template::Inherited.new(component: @component, template: template)
end
Expand Down
1 change: 0 additions & 1 deletion lib/view_component/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
module ViewComponent
class Template
DEFAULT_FORMAT = :html
private_constant :DEFAULT_FORMAT

# Rails 8.1 added a newline to compiled ERB output (rails/rails#53731).
ERB_NEWLINE_RAILS_VERSION = Gem::Version.new("8.1.0.alpha")
Expand Down
27 changes: 27 additions & 0 deletions test/sandbox/test/template_regression_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@ module TemplateRegressionFixtures
end

class TemplateRegressionTest < ViewComponent::TestCase
def test_sidecar_template_overrides_inherited_call_for_every_format
with_component_files(
"parent_component.rb" => <<~RUBY,
class TemplateRegressionFixtures::ParentComponent < ViewComponent::Base
def call = tag.div("parent call")
end
RUBY
"child_component.rb" => "class TemplateRegressionFixtures::ChildComponent < TemplateRegressionFixtures::ParentComponent; end",
"child_component.html.erb" => "child template",
"grandchild_component.rb" => "class TemplateRegressionFixtures::GrandchildComponent < TemplateRegressionFixtures::ChildComponent; end"
) do
context = vc_test_controller.view_context

2.times do
[TemplateRegressionFixtures::ChildComponent, TemplateRegressionFixtures::GrandchildComponent].each do |component|
[:turbo_stream, :html, :xml].each do |format|
context.lookup_context.formats = [format]
assert_includes component.new.render_in(context), "child template"
end
end

ViewComponent::CompileCache.invalidate!
TemplateRegressionFixtures::ParentComponent.__vc_compile
end
end
end

def test_ordered_format_fallbacks_do_not_share_output_cache
with_component_files(
"formats_component.rb" => <<~RUBY,
Expand Down
Loading