diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 04b1355bb..8c21daab1 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -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* diff --git a/lib/view_component/compiler.rb b/lib/view_component/compiler.rb index d7d14a350..a7e42a2bb 100644 --- a/lib/view_component/compiler.rb +++ b/lib/view_component/compiler.rb @@ -85,7 +85,7 @@ def find_templates_for(requested_details) # # @return [Array] def template_formats - gather_templates.map { |template| template.format || :html }.uniq + gather_templates.map { |template| template.format || Template::DEFAULT_FORMAT }.uniq end # @private @@ -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 diff --git a/lib/view_component/template.rb b/lib/view_component/template.rb index 4a323acb7..42439d216 100644 --- a/lib/view_component/template.rb +++ b/lib/view_component/template.rb @@ -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") diff --git a/test/sandbox/test/template_regression_test.rb b/test/sandbox/test/template_regression_test.rb index 64fad6f87..530f873c4 100644 --- a/test/sandbox/test/template_regression_test.rb +++ b/test/sandbox/test/template_regression_test.rb @@ -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,