Skip to content

Commit

Permalink
Fix render_parent when variants are involved
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron committed Jul 13, 2023
1 parent 095f76f commit 8654142
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,18 @@ def render_in(view_context, &block)
#
# Calls `super`, returning `nil` to avoid rendering the result twice.
def render_parent
mtd = @__vc_variant ? "call_#{@__vc_variant}" : "call"
method(mtd).super_method.call
mtd = if @__vc_variant
mtd_variant_name = "call_#{@__vc_variant}"

if respond_to?(mtd_variant_name)
super_mtd = method(mtd_variant_name).super_method
super_mtd ? super_mtd : nil
end
end

mtd ||= method(:call).super_method
mtd.call

nil
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="render-parent-wrapper phone">
<%= render(SuperComponent.new) %>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="render-parent-wrapper">
<%= render(SuperComponent.new) %>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class RenderParentWrapperComponent < ViewComponent::Base
end
3 changes: 3 additions & 0 deletions test/sandbox/app/components/super_component.html+variant.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="derived-component variant">
<%= render_parent %>
</div>
16 changes: 16 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,22 @@ def test_inherited_component_calls_super
end
end

def test_inherited_component_falls_back_to_call_method_when_rendering_variant
with_variant :phone do
render_inline(RenderParentWrapperComponent.new)
end

assert_selector ".render-parent-wrapper.phone .derived-component .base-component"
end

def test_inherited_component_falls_back_to_super_call_method_when_rendering_variant
with_variant :variant do
render_inline(SuperComponent.new)
end

assert_selector ".derived-component.variant .base-component"
end

def test_component_renders_without_trailing_whitespace
template = File.read(Rails.root.join("app/components/trailing_whitespace_component.html.erb"))
assert template =~ /\s+\z/, "Template does not contain any trailing whitespace"
Expand Down

0 comments on commit 8654142

Please sign in to comment.