Skip to content

Commit

Permalink
release 3.5.0 (ViewComponent#1808)
Browse files Browse the repository at this point in the history
* release 3.5.0

* Fix inline render_parent tests
  • Loading branch information
camertron authored and claudiob committed Jan 3, 2024
1 parent 221ea07 commit 6f21879
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
view_component (3.4.0)
view_component (3.5.0)
activesupport (>= 5.2.0, < 8.0)
concurrent-ruby (~> 1.0)
method_source (~> 1.0)
Expand Down
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ nav_order: 5

## main

## 3.5.0

* Add Skroutz to users list.

*Chris Nitsas*
Expand Down
2 changes: 1 addition & 1 deletion docs/_data/library.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version: 3.4.0
version: 3.5.0
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
2 changes: 1 addition & 1 deletion lib/view_component/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module ViewComponent
module VERSION
MAJOR = 3
MINOR = 4
MINOR = 5
PATCH = 0
PRE = nil

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 6f21879

Please sign in to comment.