Add after_compile hook for component extensions #2411
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Following up on the conversation with @joelhawksley and @fsateler in this comment, this PR introduces a minimal extension point in the
ViewComponent::Compiler.What are you trying to accomplish?
This PR creates a minimal extension point for
ViewComponent::Compilerto enable "sub-templates" functionality through a dedicated gem. The sub-templates gem will work by overriding the after_compile hook, receiving the compiled component, and then searching for additional template files (e.g., header.html.erb, row.html.erb) in the component's directory to generate correspondingcall_*methods with explicit locals validation.What approach did you choose and why?
I chose to implement a simple after_compile instance method hook as suggested by @joelhawksley. This approach is:
Simple and Direct: Following Joel's preference for simplicity over sophisticated callback systems
Easy to Document: A straightforward method override pattern that's familiar to Rails developers
Zero Overhead: No performance impact when the hook isn't overridden
Non-invasive: Doesn't modify ViewComponent's core compilation logic
Extensible: Components can easily override the method to add custom post-compilation behavior
The implementation uses allocate to call the instance method without triggering the component's constructor, avoiding the complexity of handling initialization arguments.
Anything you want to highlight for special attention from reviewers?
@component.allocate.after_compileto avoid constructor complexity, but I'm uncertain if this is the most elegant solution. Open to suggestions for alternative approaches that don't requireallocate.