Skip to content

Commit aab5a46

Browse files
Raise ArgumentError when conflicting slots are defined. (#1455)
* Raise `ArgumentError` when conflicting slots are defined. * Update lib/view_component/slotable_v2.rb Co-authored-by: Blake Williams <blakewilliams@github.com> Co-authored-by: Blake Williams <blakewilliams@github.com>
1 parent b8febaf commit aab5a46

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

docs/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@ title: Changelog
99

1010
## main
1111

12+
* Raise `ArgumentError` when conflicting Slots are defined.
13+
14+
Before this change it was possible to define Slots with conflicting names, for example:
15+
16+
```ruby
17+
class MyComponent < ViewComponent::Base
18+
renders_one :item
19+
renders_many :items
20+
end
21+
```
22+
23+
*Joel Hawksley*
24+
1225
## 2.64.0
1326

1427
* Add `warn_on_deprecated_slot_setter` flag to opt-in to deprecation warning.

lib/view_component/slotable_v2.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def warn_on_deprecated_slot_setter
8080
# <% end %>
8181
def renders_one(slot_name, callable = nil)
8282
validate_singular_slot_name(slot_name)
83+
validate_plural_slot_name(ActiveSupport::Inflector.pluralize(slot_name).to_sym)
8384

8485
define_method :"with_#{slot_name}" do |*args, &block|
8586
set_slot(slot_name, nil, *args, &block)
@@ -147,9 +148,9 @@ def renders_one(slot_name, callable = nil)
147148
# <% end %>
148149
# <% end %>
149150
def renders_many(slot_name, callable = nil)
150-
validate_plural_slot_name(slot_name)
151-
152151
singular_name = ActiveSupport::Inflector.singularize(slot_name)
152+
validate_plural_slot_name(slot_name)
153+
validate_singular_slot_name(ActiveSupport::Inflector.singularize(slot_name).to_sym)
153154

154155
# Define setter for singular names
155156
# for example `renders_many :items` allows fetching all tabs with

test/sandbox/test/slotable_v2_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,4 +588,26 @@ def test_lambda_slot_content_can_be_provided_via_a_block
588588

589589
assert_selector("h1.some-class", text: "This is a header!")
590590
end
591+
592+
def test_raises_error_on_conflicting_slot_names
593+
error = assert_raises ArgumentError do
594+
Class.new(ViewComponent::Base) do
595+
renders_one :conflicting_item
596+
renders_many :conflicting_items
597+
end
598+
end
599+
600+
assert_includes error.message, "conflicting_item slot multiple times"
601+
end
602+
603+
def test_raises_error_on_conflicting_slot_names_in_reverse_order
604+
error = assert_raises ArgumentError do
605+
Class.new(ViewComponent::Base) do
606+
renders_many :conflicting_items
607+
renders_one :conflicting_item
608+
end
609+
end
610+
611+
assert_includes error.message, "conflicting_items slot multiple times"
612+
end
591613
end

0 commit comments

Comments
 (0)