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
return if compiled? && !force

gather_templates
ActiveSupport::Notifications.instrument(
Copy link
Member

Choose a reason for hiding this comment

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

After thinking about how we might write the documentation for this feature, my gut tells me that we might be better served with something simpler.

What if we instead added a no-op public after_compile method to Base and call it inside https://github.com/ViewComponent/view_component/blob/main/lib/view_component/base.rb#L539? That way, consumers could define their own after_compile that takes additional actions.

Copy link
Author

Choose a reason for hiding this comment

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

The after_compile hook seems much cleaner and avoids the Rails 7.1 issues. I’ll update the PR to use that approach.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps using ActiveSupport::Callbacks would make it easier to inject multiple extensions into the base gem

Copy link
Author

Choose a reason for hiding this comment

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

@joelhawksley would you be open to exploring that direction, or do you prefer to keep it minimal for now?

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer the minimal approach. I don't think we need any more complexity.

Copy link
Author

Choose a reason for hiding this comment

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

@joelhawksley I've updated the PR to use the minimal after_compile approach as you suggested

@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
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