Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ nav_order: 6

## main

## 4.0.3

* Add `after_compile` hook to enable extensions to run logic after component compilation. Extensions can override this instance method to add custom post-compilation behavior.

*Jose Solás*

## 4.0.2

* Share the view context in tests to prevent out-of-order rendering issues for certain advanced use-cases, eg. testing instances of Rails' `FormBuilder`.
Expand Down
12 changes: 12 additions & 0 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,18 @@ def __vc_compiled?
__vc_compiler.compiled?
end

# Called after a component class has been compiled.
#
# Extensions can override this instance method to run logic after
# compilation (e.g., generate helpers, register metadata, etc.).
#
# By default, this is a no-op. The compiler will invoke this method on an
# uninitialized instance using `allocate` to avoid requiring initializer
# arguments.
def after_compile
# no-op by default
end

# @private
def __vc_ensure_compiled
__vc_compile unless __vc_compiled?
Expand Down
7 changes: 7 additions & 0 deletions lib/view_component/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def compile(raise_errors: false, force: false)
@component.__vc_build_i18n_backend

CompileCache.register(@component)

# Invoke instance-level after_compile hook without calling initialize.
begin
@component.allocate.after_compile
rescue NoMethodError
# no-op
end
end
end

Expand Down
55 changes: 55 additions & 0 deletions test/sandbox/test/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,59 @@ def test_uses_module_configuration
assert_equal false, TestAlreadyConfigurableModule::SomeComponent.instrumentation_enabled
assert_equal false, TestAlreadyConfiguredModule::SomeComponent.instrumentation_enabled
end

def test_after_compile_hook_called_on_compile
klass = Class.new(ViewComponent::Base) do
@@calls = 0

def self.calls
@@calls
end

def after_compile
@@calls += 1
end

erb_template ""
end

self.class.const_set(:TempHookComponent, klass)
ViewComponent::CompileCache.invalidate_class!(klass)

ViewComponent::Compiler.new(klass).compile(force: true)
assert_equal 1, klass.calls
ensure
self.class.send(:remove_const, :TempHookComponent) if self.class.const_defined?(:TempHookComponent)
ViewComponent::CompileCache.invalidate_class!(klass) if defined?(klass)
end

def test_after_compile_not_called_on_cached_compile
klass = Class.new(ViewComponent::Base) do
@@calls = 0

def self.calls
@@calls
end

def after_compile
@@calls += 1
end

erb_template ""
end

self.class.const_set(:TempHookCachedComponent, klass)
ViewComponent::CompileCache.invalidate_class!(klass)

compiler = ViewComponent::Compiler.new(klass)
compiler.compile(force: true)
assert_equal 1, klass.calls

# compile again without force -> should not call hook again
compiler.compile
assert_equal 1, klass.calls
ensure
self.class.send(:remove_const, :TempHookCachedComponent) if self.class.const_defined?(:TempHookCachedComponent)
ViewComponent::CompileCache.invalidate_class!(klass) if defined?(klass)
end
end
Loading