Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failing test case using image_tag and tag slot #1885

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ nav_order: 5

## main

* Raise the error `RedefinedExistingMethodError` when declaring a slot which conflicts with an existing method.

*Hugo Chantelauze*

* Resolve an issue where slots starting with `call` would cause a `NameError`

*Blake Williams*
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ ViewComponent is built by over a hundred members of the community, including:
<img src="https://avatars.githubusercontent.com/fsateler?s=64" alt="fsateler" width="32" />
<img src="https://avatars.githubusercontent.com/fugufish?s=64" alt="fugufish" width="32" />
<img src="https://avatars.githubusercontent.com/g13ydson?s=64" alt="g13ydson" width="32" />
<img src="https://avatars.githubusercontent.com/hchtlz?s=64" alt="hchtlz" width="32" />
<img src="https://avatars.githubusercontent.com/horacio?s=64" alt="horacio" width="32" />
<img src="https://avatars.githubusercontent.com/horiaradu?s=64" alt="horiaradu" width="32" />
<img src="https://avatars.githubusercontent.com/jacob-carlborg-apoex?s=64" alt="yykamei" width="32" />
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 @@ -129,6 +129,16 @@ def initialize(klass_name, slot_name)
end
end

class RedefinedExistingMethodError < InvalidSlotNameError
MESSAGE =
"COMPONENT declares a slot named SLOT_NAME, which conflicts with an existing method.\n\n" \
"To fix this issue, choose a different slot 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 ReservedSingularSlotNameError < InvalidSlotNameError
MESSAGE =
"COMPONENT declares a slot named SLOT_NAME, which is a reserved word in the ViewComponent framework.\n\n" \
Expand Down
11 changes: 9 additions & 2 deletions lib/view_component/slotable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ def validate_singular_slot_name(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)
raise_if_method_exists(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?("?")
end

def raise_if_slot_registered(slot_name)
Expand All @@ -321,8 +326,10 @@ def raise_if_slot_registered(slot_name)
end
end

def raise_if_slot_ends_with_question_mark(slot_name)
raise SlotPredicateNameError.new(name, slot_name) if slot_name.to_s.ends_with?("?")
def raise_if_method_exists(slot_name)
if instance_methods.include?(slot_name)
raise RedefinedExistingMethodError.new(name, slot_name)
end
end

def raise_if_slot_conflicts_with_call(slot_name)
Expand Down
20 changes: 20 additions & 0 deletions test/sandbox/test/slotable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,24 @@ def test_slot_names_can_start_with_call
end
end
end

def test_raises_error_on_slot_name_conflicting_with_existing_method_for_renders_one
exception = assert_raises ViewComponent::RedefinedExistingMethodError do
Class.new(ViewComponent::Base) do
renders_one :tag
end
end

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

def test_raises_error_on_slot_name_conflicting_with_existing_method_for_renders_many
exception = assert_raises ViewComponent::RedefinedExistingMethodError do
Class.new(ViewComponent::Base) do
renders_many :tags
end
end

assert_includes exception.message, "declares a slot named tag"
end
end
Loading