Skip to content

Conversation

@jsolas
Copy link

@jsolas jsolas commented Aug 2, 2025

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::Compiler to 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 corresponding call_* 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?

  • The hook is implemented as an instance method (not class method) to provide maximum flexibility for extensions while avoiding Ruby inheritance edge cases
  • Currently uses @component.allocate.after_compile to avoid constructor complexity, but I'm uncertain if this is the most elegant solution. Open to suggestions for alternative approaches that don't require allocate.

@jsolas jsolas force-pushed the add-activesupport-notifications-to-compiler- branch from 7aee501 to a0eeb0f Compare August 2, 2025 04:29
@jsolas jsolas force-pushed the add-activesupport-notifications-to-compiler- branch 2 times, most recently from 909437c to 889e015 Compare August 18, 2025 21:41
@jsolas jsolas force-pushed the add-activesupport-notifications-to-compiler- branch from 889e015 to 8ed08c8 Compare August 18, 2025 21:56
@jsolas jsolas changed the title Add ActiveSupport::Notifications instrumentation to compiler Add after_compile hook for component extensions Aug 18, 2025
@jsolas jsolas requested a review from joelhawksley August 20, 2025 00:56
@jsolas
Copy link
Author

jsolas commented Nov 4, 2025

Friendly ping @joelhawksley! Any chance to get feedback on this? Happy to make changes if needed

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.

3 participants