Skip to content

Commit

Permalink
Fix Vale grammar issues; add #render_parent test back in; fix linting…
Browse files Browse the repository at this point in the history
… issues
  • Loading branch information
camertron committed Jul 17, 2023
1 parent 0bdd60c commit 4439760
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ nav_order: 5

*Chris Nitsas*

* Introduce `yield :parent` as a replacement for `#render_parent`, which respects variants and deep inheritance hierarchies.

*Cameron Dutro*

## 3.4.0

* Avoid including Rails `url_helpers` into `Preview` class when they're not defined.
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ To render a parent component's template from a subclass' template, use `yield :p
</div>
```

If the parent supports the current variant, the variant will automatically be rendered. `yield :parent` replaces the deprecated `#render_parent` method, which does not respect variants or multiple levels of inheritance.
If the parent supports the current variant, the variant will automatically be rendered. `yield :parent` replaces the deprecated `#render_parent` method, which doesn't respect variants or multiple levels of inheritance.

`yield :parent` also works with inline templates:

Expand All @@ -154,7 +154,7 @@ class MyComponent < ViewComponent::Base
end
```

`super` will attempt to call the `#call_phone` method on the parent class. If the parent class does not support the "phone" variant, Ruby will raise a `NoMethodError`. Consider using a template and `render :parent` to handle superclass variants automatically.
`super` will attempt to call the `#call_phone` method on the parent class. If the parent class doesn't support the "phone" variant, Ruby will raise a `NoMethodError`. Consider using a template and `render :parent` to handle superclass variants automatically.

### render_parent

Expand Down
2 changes: 2 additions & 0 deletions lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def define_compiled_template_methods(method_name, template_info)
component_class.silence_redefinition_of_method(method_name)
component_class.silence_redefinition_of_method(unique_method_name)

# rubocop:disable Style/EvalWithLocation
component_class.class_eval <<-RUBY, template_info[:path], template_info[:lineno]
private def #{unique_method_name}
if block_given?
Expand Down Expand Up @@ -124,6 +125,7 @@ def #{method_name}
#{unique_method_name}
end
RUBY
# rubocop:enable Style/EvalWithLocation
end
end

Expand Down
1 change: 1 addition & 0 deletions test/sandbox/app/components/super_base_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div class="base-component"></div>
4 changes: 4 additions & 0 deletions test/sandbox/app/components/super_base_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class SuperBaseComponent < ViewComponent::Base
end
3 changes: 3 additions & 0 deletions test/sandbox/app/components/super_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="derived-component">
<%= render_parent %>
</div>
4 changes: 4 additions & 0 deletions test/sandbox/app/components/super_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class SuperComponent < SuperBaseComponent
end
9 changes: 9 additions & 0 deletions test/sandbox/test/rendering_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,15 @@ def test_inherited_component_renders_when_lazy_loading
assert_selector("div", text: "hello, my own template")
end

def test_render_parent
render_inline(SuperComponent.new)

assert_selector(".base-component", count: 1)
assert_selector(".derived-component", count: 1) do |derived|
derived.assert_selector(".base-component", count: 1)
end
end

def test_child_components_can_render_parent
render_inline(Level3Component.new)

Expand Down

0 comments on commit 4439760

Please sign in to comment.