@@ -46,7 +46,7 @@ def config
4646 end
4747
4848 include ActionView ::Helpers
49- include Rails . application . routes . url_helpers if defined? ( Rails ) && Rails . application
49+ include Rails . application . routes . url_helpers if defined? ( Rails . application . routes )
5050 include ERB ::Escape
5151 include ActiveSupport ::CoreExt ::ERBUtil
5252
@@ -131,15 +131,14 @@ def render_in(view_context, &block)
131131
132132 @__vc_content_evaluated = false
133133 @__vc_render_in_block = block
134+ @view_context . instance_variable_set ( :@virtual_path , virtual_path )
134135
135136 before_render
136137
137138 if render?
138139 value = nil
139140
140141 @output_buffer . with_buffer do
141- @view_context . instance_variable_set ( :@virtual_path , virtual_path )
142-
143142 rendered_template =
144143 around_render do
145144 render_template_for ( @__vc_requested_details ) . to_s
@@ -260,14 +259,17 @@ def render(options = {}, args = {}, &block)
260259 @view_context . render ( options , args , &block )
261260 elsif block
262261 __vc_original_view_context . render ( options , args ) do
262+ # capture the block output in the view context of the component
263+ output = capture ( &block )
264+
263265 # Partials are rendered to their own buffer and do not append to the
264266 # original @output_buffer we retain a reference to in #render_in. This
265267 # is a problem since the block passed to us here in the #render method
266268 # is evaluated within the context of ViewComponent::Base, and thus
267269 # appends to the original @output_buffer. To avoid this, we evaluate the
268270 # block in the view context instead, which will append to the output buffer
269271 # created for the partial.
270- __vc_original_view_context . instance_exec ( & block )
272+ __vc_original_view_context . capture { output }
271273 end
272274 else
273275 __vc_original_view_context . render ( options , args )
@@ -301,7 +303,7 @@ def helpers
301303 @__vc_helpers ||= __vc_original_view_context || controller . view_context
302304 end
303305
304- if ::Rails . env . development? || ::Rails . env . test?
306+ if defined? ( Rails . env ) && ( ::Rails . env . development? || ::Rails . env . test? )
305307 # @private
306308 def method_missing ( method_name , *args ) # rubocop:disable Style/MissingRespondToMissing
307309 super
@@ -330,7 +332,7 @@ def view_cache_dependencies
330332 [ ]
331333 end
332334
333- if Rails ::VERSION ::MAJOR == 7 && Rails ::VERSION ::MINOR == 1
335+ if defined? ( Rails :: VERSION ) && Rails ::VERSION ::MAJOR == 7 && Rails ::VERSION ::MINOR == 1
334336 # Rails expects us to define `format` on all renderables,
335337 # but we do not know the `format` of a ViewComponent until runtime.
336338 def format
@@ -610,6 +612,16 @@ def __vc_compiled?
610612 __vc_compiler . compiled?
611613 end
612614
615+ # Hook called by the compiler after a component is compiled.
616+ #
617+ # Extensions can override this class method to run logic after
618+ # compilation (e.g., generate helpers, register metadata, etc.).
619+ #
620+ # By default, this is a no-op.
621+ def after_compile
622+ # no-op by default
623+ end
624+
613625 # @private
614626 def __vc_ensure_compiled
615627 __vc_compile unless __vc_compiled?
0 commit comments