Skip to content

Conversation

boardfish
Copy link
Collaborator

@boardfish boardfish commented Mar 16, 2025

Adds a mechanism for inheriting config from a parent module. Since Base is generally used when referring to "global" config options, this doesn't do anything right now beyond exposing the ability to configure parent modules.

Modules can be configured by requiring and including ViewComponent::Configurable, which will expose a config accessor with a view_component configuration on the module. These will not interfere with existing configuration that might be granted to, e.g., a Rails::Engine by default.

This would give us a starting point to begin migrating options into engine-local config.


The separation between:

isn't perfect right now, but this PR and #2230 put us on the road to this.

It should only be possible to set options in each area as appropriate. Particularly with this PR, we should be concerned with making sure that module-local and exclusively Rails app-local config don't merge.

Because ViewComponent::Base.config is named as it is right now, the difference between component-local config (#2230) and module-local config isn't super clear, but we can rename parts of this interface in v4.

Adds a mechanism for inheriting config from a parent module. There's definitely more that needs to be done to iron this out, including:
- if the module does not already respond to `config`, provide a concern that sets it up using `ActiveSupport::Configurable`
- if the module _does_ already respond to `config` and doesn't have `view_component` set on it, set it up.
- if the module's config does have `view_component` set up, leave it alone?

I also need to consider whether the current way of having things refer back to the Rails app config is appropriate.

Since `Base` is generally used when referring to "global" config options, this probably doesn't do anything right now, but settings like
`test_controller` and `generate` should opt into it.
This can be included on a module to provide module-local configuration for ViewComponent. It avoids overwriting existing configuration that may already exist.
Comment on lines +151 to +191
module TestModuleWithoutConfig
class SomeComponent < ViewComponent::Base
end
end

# Config defined on top-level module as opposed to engine.
module TestModuleWithConfig
include ViewComponent::Configurable

configure do |config|
config.view_component.test_controller = "AnotherController"
end

class SomeComponent < ViewComponent::Base
end
end

module TestAlreadyConfigurableModule
include ActiveSupport::Configurable
include ViewComponent::Configurable

configure do |config|
config.view_component.test_controller = "AnotherController"
end

class SomeComponent < ViewComponent::Base
end
end

module TestAlreadyConfiguredModule
include ActiveSupport::Configurable

configure do |config|
config.view_component = ActiveSupport::InheritableOptions[test_controller: "AnotherController"]
end

include ViewComponent::Configurable

class SomeComponent < ViewComponent::Base
end
end
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to move this setup out into other files if you'd rather.

@boardfish boardfish marked this pull request as ready for review March 17, 2025 21:04
@boardfish boardfish changed the title Delegate config to parent modules Delegate config to parent modules (no-op) Mar 17, 2025
@joelhawksley joelhawksley merged commit 7538b59 into main Mar 18, 2025
23 checks passed
@joelhawksley joelhawksley deleted the module-local-configuration branch March 18, 2025 18:50
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

Successfully merging this pull request may close these issues.

2 participants