Skip to content

Commit

Permalink
Merge branch 'main' into rv-remove-unsupported-rails-ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
reeganviljoen authored Nov 14, 2023
2 parents e2a085f + 1680c6f commit 4540da6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require:
- standard
- "rubocop-md"
inherit_gem:
standard: config/base.yml
5 changes: 1 addition & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,6 @@ DEPENDENCIES
jbuilder (~> 2)
m (~> 1)
minitest (~> 5.18)
net-imap
net-pop
net-smtp
pry (~> 0.13)
puma (~> 6)
rails (~> 7.0.0)
Expand All @@ -353,4 +350,4 @@ DEPENDENCIES
yard-activesupport-concern (~> 0.0.1)

BUNDLED WITH
2.2.33
2.3.7
9 changes: 9 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ nav_order: 5

*Reegan Viljoen*

* 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*

* Don't add ActionDispatch::Static middleware unless `public_file_server.enabled`.

*Daniel Gonzalez*
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
11 changes: 10 additions & 1 deletion 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 @@ -322,14 +324,21 @@ def raise_if_slot_registered(slot_name)
end

def raise_if_slot_ends_with_question_mark(slot_name)
raise SlotPredicateNameError.new(name, slot_name) if slot_name.to_s.ends_with?("?")
raise SlotPredicateNameError.new(name, slot_name) if slot_name.to_s.end_with?("?")
end

def raise_if_slot_conflicts_with_call(slot_name)
if slot_name.start_with?("call_")
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 4540da6

Please sign in to comment.