Skip to content

Commit

Permalink
Fix inline render_parent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron committed Jul 24, 2023
1 parent dcb9a03 commit 6b42040
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 34 deletions.
17 changes: 2 additions & 15 deletions docs/guide/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,26 +143,13 @@ class MyComponent < ViewComponent::Base
end
```

Finally, `#render_parent` also works inside `#call` methods:
Keep in mind that `#render_parent` doesn't return a string. If a string is desired, eg. inside a `#call` method, call `#render_parent_to_string` instead. For example:

```ruby
class MyComponent < ViewComponent::Base
def call
content_tag("div") do
render_parent
end
end
end
```

When composing `#call` methods, keep in mind that `#render_parent` does not return a string. If a string is desired, call `#render_parent_to_string` instead. For example:

```ruby
class MyComponent < ViewComponent::Base
# "phone" variant
def call_phone
content_tag("div") do
"<div>#{render_parent_to_string}</div>"
render_parent_to_string
end
end
end
Expand Down
24 changes: 12 additions & 12 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,8 @@ def render_in(view_context, &block)
# parent template considering the current variant and emits the result without
# double-rendering.
def render_parent
@__vc_parent_render_level ||= 0 # ensure a good starting value

begin
target_render = self.class.instance_variable_get(:@__vc_ancestor_calls)[@__vc_parent_render_level]
@__vc_parent_render_level += 1

target_render.bind_call(self, @__vc_variant)
nil
ensure
@__vc_parent_render_level -= 1
end
render_parent_to_string
nil
end

# Renders the parent component to a string and returns it. This method is meant
Expand All @@ -151,7 +142,16 @@ def render_parent
#
# When rendering the parent inside an .erb template, use `#render_parent` instead.
def render_parent_to_string
capture { render_parent }
@__vc_parent_render_level ||= 0 # ensure a good starting value

begin
target_render = self.class.instance_variable_get(:@__vc_ancestor_calls)[@__vc_parent_render_level]
@__vc_parent_render_level += 1

target_render.bind_call(self, @__vc_variant)
ensure
@__vc_parent_render_level -= 1
end
end

# Optional content to be returned after the rendered template.
Expand Down
4 changes: 3 additions & 1 deletion test/sandbox/app/components/inline_level1_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class InlineLevel1Component < ViewComponent::Base
def call
content_tag(:div, class: "level1-component")
content_tag(:div, class: "level1-component") do
"Level 1 component"
end
end
end
6 changes: 3 additions & 3 deletions test/sandbox/app/components/inline_level2_component.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# frozen_string_literal: true

class InlineLevel2Component < Level2Component
class InlineLevel2Component < InlineLevel1Component
def call
"<div level2-component base>#{render_parent_to_string}</div>"
"<div class='level2-component base'>#{render_parent_to_string}</div>".html_safe
end

def call_variant
"<div level2-component variant>#{render_parent_to_string}</div>"
"<div class='level2-component variant'>#{render_parent_to_string}</div>".html_safe
end
end
6 changes: 3 additions & 3 deletions test/sandbox/app/components/inline_level3_component.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

class InlineLevel3Component < Level2Component
class InlineLevel3Component < InlineLevel2Component
def call
content_tag(:div, class: "level3-component base") do
render_parent
render_parent_to_string
end
end

def call_variant
content_tag(:div, class: "level3-component variant") do
render_parent
render_parent_to_string
end
end
end

0 comments on commit 6b42040

Please sign in to comment.