Skip to content

Better configuration scaffolding #2188

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

Open
armchairdj opened this issue Dec 22, 2024 · 1 comment
Open

Better configuration scaffolding #2188

armchairdj opened this issue Dec 22, 2024 · 1 comment

Comments

@armchairdj
Copy link

armchairdj commented Dec 22, 2024

Feature request

Current the ViewComponent website has zero documentation about how to set configuration options. The available configuration keys are documented, but there isn't even a nod to how to set up an initializer.

Trial and error taught me that I have to configure this gem as if I'm configuring Rails itself:

# config/initializers/view_component.rb
Rails.application.configure do
  config.view_component.component_parent_class = "ApplicationComponent"
end

Many popular gems (RSpec, Kaminari) provide their ownconfigure hook for setting these values. This would be far more expected syntax:

# config/initializers/view_component.rb
ViewComponent.configure do
  config.component_parent_class = "ApplicationComponent"
end

It would be even cooler if there were a generator for creating a view_component initializer and if it came with a giant commented-out list of all available configurations, with examples. Again, this would bring ViewComponent inline w/ many popular Rails libraries.

Finally: The docs state:

It’s common practice to create and inherit from an ApplicationComponent that’s a subclass of ViewComponent::Base.

If that's the case, why not detect that such a file exists and automatically configure the generator to use it as a base class when present?

If an initializer generator were created, perhaps it could be something like rails g view_component:install and it could generate a bare ApplicationComponent alongside an initializer.

Motivation

As I prepared to create my second or third ViewComponent, I realized I didn't want to have to type --parent ApplicationComponent every single time I used the generator. So I sought to use provided configuration option and spent 15 minutes googling "ViewComponent initializer", reading the source code and generally looking for any information about how to actually set one of these configuration options — to no avail.

@boardfish
Copy link
Collaborator

Hi @armchairdj! Sorry for the delay in getting back to you on this, and thank you for the feedback – I think you raise a good point about the configure hook. I imagine the current behaviour of configuring ViewComponent next to Rails is probably related to the gem's history as originally having been intended as an Action View plugin of sorts: if you have a look back through its history, it was once called ActionView::Component!

I'm not sure if we can bring this into scope for the current work to update config in #2124 – the main aim of that is really to decompose config so that gems and engines aren't tied to the same configuration as a Rails app that they integrate with. But we should definitely execute on some of the things you've mentioned here, and even in the interim a small update to the documentation would go a long way here to make config clearer. It might be a nice way to contribute if that's something you'd be interested in doing!

Also, the behaviour with the parent component in the generator that you're experiencing seems unusual to me – I feel like we might already implicitly try and look for ApplicationComponent if it's defined, but perhaps that behaviour is broken?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants