Skip to content

Commit

Permalink
Add special case for renders_one :content (#1514)
Browse files Browse the repository at this point in the history
* Add special case for `renders_one :content`

* Add avatar to docs/index.md

* Add item to the CHANGELOG.md

* Update error messages
  • Loading branch information
danieldiekmeier authored Sep 13, 2022
1 parent ae04dc7 commit 95c23d5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ nav_order: 5

*Daniel Diekmeier*

* Add special exception message for `renders_one :content` explaining that content passed as a block will be assigned to the `content` accessor without having to create an explicit slot.

*Daniel Diekmeier*

## 2.71.0

**ViewComponent has moved to a new organization: [https://github.com/viewcomponent/view_component](https://github.com/viewcomponent/view_component). See [https://github.com/viewcomponent/view_component/issues/1424](https://github.com/viewcomponent/view_component/issues/1424) for more details.**
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ ViewComponent is built by over a hundred members of the community, including:
<img src="https://avatars.githubusercontent.com/cpjmcquillan?s=64" alt="cpjmcquillan" width="32" />
<img src="https://avatars.githubusercontent.com/czj?s=64" alt="czj" width="32" />
<img src="https://avatars.githubusercontent.com/dani-sc?s=64" alt="dani-sc" width="32" />
<img src="https://avatars.githubusercontent.com/danieldiekmeier?s=64" alt="danieldiekmeier" width="32" />
<img src="https://avatars.githubusercontent.com/dark-panda?s=64" alt="dark-panda" width="32" />
<img src="https://avatars.githubusercontent.com/davekaro?s=64" alt="davekaro" width="32" />
<img src="https://avatars.githubusercontent.com/dixpac?s=64" alt="dixpac" width="32" />
Expand Down
8 changes: 8 additions & 0 deletions lib/view_component/slotable_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,14 @@ def validate_plural_slot_name(slot_name)
end

def validate_singular_slot_name(slot_name)
if slot_name.to_sym == :content
raise ArgumentError.new(
"#{self} declares a slot named content, which is a reserved word in ViewComponent.\n\n" \
"Content passed to a ViewComponent as a block is captured and assigned to the `content` accessor without having to create an explicit slot.\n\n" \
"To fix this issue, either use the `content` accessor directly or choose a different slot name."
)
end

if RESERVED_NAMES[:singular].include?(slot_name.to_sym)
raise ArgumentError.new(
"#{self} declares a slot named #{slot_name}, which is a reserved word in the ViewComponent framework.\n\n" \
Expand Down
14 changes: 13 additions & 1 deletion test/sandbox/test/slotable_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def test_slot_with_nested_blocks_content_selectable_false
refute_selector("div.table div.table__header span", text: "Selectable")
end

def test_component_raises_when_given_invalid_slot_name
def test_component_raises_when_given_content_slot_name
exception =
assert_raises ArgumentError do
Class.new(ViewComponent::Base) do
Expand All @@ -342,6 +342,18 @@ def test_component_raises_when_given_invalid_slot_name
end

assert_includes exception.message, "declares a slot named content"
assert_includes exception.message, "without having to create"
end

def test_component_raises_when_given_invalid_slot_name
exception =
assert_raises ArgumentError do
Class.new(ViewComponent::Base) do
renders_one :render
end
end

assert_includes exception.message, "declares a slot named render"
end

def test_component_raises_when_given_one_slot_name_ending_with_question_mark
Expand Down

0 comments on commit 95c23d5

Please sign in to comment.