Skip to content

Commit

Permalink
Raise error uncountable slot names in plural slots (#1904)
Browse files Browse the repository at this point in the history
* Raise error when uncountable slot names are used in `renders_many`

    *Hugo Chantelauze*
    *Reegan Viljoen*
  • Loading branch information
reeganviljoen authored Nov 10, 2023
1 parent 13a4722 commit 1680c6f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ nav_order: 5

## main

* Raise error when uncountable slot names are used in `renders_many`

*Hugo Chantelauze*
*Reegan Viljoen*

* Replace usage of `String#ends_with?` with `String#end_with?` to reduce the dependency on ActiveSupport core extensions.

*halo*
Expand Down
10 changes: 10 additions & 0 deletions lib/view_component/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ def initialize(klass_name, slot_name)
end
end

class UncountableSlotNameError < InvalidSlotNameError
MESSAGE =
"COMPONENT declares a slot named SLOT_NAME, which is an uncountable word\n\n" \
"To fix this issue, choose a different name."

def initialize(klass_name, slot_name)
super(MESSAGE.gsub("COMPONENT", klass_name.to_s).gsub("SLOT_NAME", slot_name.to_s))
end
end

class ContentAlreadySetForPolymorphicSlotError < StandardError
MESSAGE = "Content for slot SLOT_NAME has already been provided."

Expand Down
9 changes: 9 additions & 0 deletions lib/view_component/slotable.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "active_support/concern"
require "active_support/inflector/inflections"
require "view_component/slot"

module ViewComponent
Expand Down Expand Up @@ -295,6 +296,7 @@ def validate_plural_slot_name(slot_name)
raise ReservedPluralSlotNameError.new(name, slot_name)
end

raise_if_slot_name_uncountable(slot_name)
raise_if_slot_conflicts_with_call(slot_name)
raise_if_slot_ends_with_question_mark(slot_name)
raise_if_slot_registered(slot_name)
Expand Down Expand Up @@ -330,6 +332,13 @@ def raise_if_slot_conflicts_with_call(slot_name)
raise InvalidSlotNameError, "Slot cannot start with 'call_'. Please rename #{slot_name}"
end
end

def raise_if_slot_name_uncountable(slot_name)
slot_name = slot_name.to_s
if slot_name.pluralize == slot_name.singularize
raise UncountableSlotNameError.new(name, slot_name)
end
end
end

def get_slot(slot_name)
Expand Down
11 changes: 11 additions & 0 deletions test/sandbox/test/slotable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,17 @@ def test_slot_with_content_shorthand
assert component.title.content?
end

def test_slot_with_unplurialized_name
exception =
assert_raises ViewComponent::UncountableSlotNameError do
Class.new(ViewComponent::Base) do
renders_many :series
end
end

assert_includes exception.message, ""
end

def test_slot_names_cannot_start_with_call_
assert_raises ViewComponent::InvalidSlotNameError do
Class.new(ViewComponent::Base) do
Expand Down

0 comments on commit 1680c6f

Please sign in to comment.