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

Conversation

hchtlz
Copy link

@hchtlz hchtlz commented Oct 26, 2023

I'm attempting to address an unexpected test failure that occurred while using the renders_one :tag feature in my component. The issue seems to be connected to the use of the image_tag method within the same component. The specific error message I encountered is:

Error:
SlotableTest#test_slotable_component:
ArgumentError: wrong number of arguments (given 2, expected 0)
    /view_component/lib/view_component/slotable.rb:91:in `block in renders_one'
    /.rbenv/versions/3.1.4/lib/ruby/gems/3.1.0/gems/actionview-7.0.8/lib/action_view/helpers/asset_tag_helper.rb:413:in `image_tag'

I suspect that the issue may be related to the naming conflict between renders_one :tag and the image_tag method within the same component. When I changed the name "tag" to something different, the test case ran without any issues.

@hchtlz hchtlz changed the title failing test case using image_tag and tag slot Failing test case using image_tag and tag slot Oct 26, 2023
@Spone
Copy link
Collaborator

Spone commented Oct 26, 2023

Thanks @hchtlz for opening this!

I think I understand what happens here:

  • We create a slot named tag, so the tag method is defined in the component.
  • When we call image_tag, the tag helper from Rails is called here... with 2 arguments, whereas the slot tag method doesn't expect any.

Now... I'm not sure how we want to address this:

  • raise an error when you try to define a slot that has the same name as a Rails' helper? (but this would mean a lot of reserved names)
  • only add tag to the list of reserved names (it's a pretty generic name so there are more chances of collision with this helper than, say, image_tag)
  • figure out a way to prevent the collision (maybe related to the idea of allow-listing the helpers, see Replace helpers with explicit api in v4 #1848)

@Spone
Copy link
Collaborator

Spone commented Oct 30, 2023

What would be your preferred approach for handling this @camertron @BlakeWilliams @boardfish?

@camertron
Copy link
Contributor

@Spone what if we just raised an error if you tried to define a slot with the same name as an already-defined method? We actually already do this for polymorphic slot setters.

@Spone
Copy link
Collaborator

Spone commented Nov 3, 2023

Sounds good. I'll work with @hchtlz on this.

@Spone Spone self-assigned this Nov 6, 2023
@Spone Spone added bug Something isn't working slots Related to the Slots feature labels Nov 6, 2023
@Spone
Copy link
Collaborator

Spone commented Nov 6, 2023

Let's see if we need to do that for renders_many as well.

docs/CHANGELOG.md Outdated Show resolved Hide resolved
hchtlz and others added 4 commits November 7, 2023 11:27
Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com>
Co-authored-by: Hans Lemuet <Spone@users.noreply.github.com>
@Spone
Copy link
Collaborator

Spone commented Nov 7, 2023

The failure in the Primer ViewComponents check comes from this component in Lookbook: https://github.com/ViewComponent/lookbook/blob/main/app/components/lookbook/icon_button/component.rb#L3C5-L3C51

We're calling renders_many :icons, which raises an exception since in this PR, we no longer allow slot names to overwrite existing method names.

But actually, I'm not sure whether we need this check for renders_many slots, since the singular slot name is never defined as a method by ViewComponent, right? So it would be legal to call both renders_many :icons and attr_reader :icon as you do in this component.

@allmarkedup mind chiming in?

@Spone
Copy link
Collaborator

Spone commented Nov 7, 2023

Currently, validate_singular_slot_name is called on both renders_one and renders_manyslots.

validate_singular_slot_name(ActiveSupport::Inflector.singularize(slot_name).to_sym)

The check for plural slot name conflicting with singular slot name has been introduced in #1455, but I'm not sure if I understand why that would be an issue (aside from being very confusing when you read the component).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working slots Related to the Slots feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants