99require "view_component/errors"
1010require "view_component/inline_template"
1111require "view_component/preview"
12+ require "view_component/request_details"
1213require "view_component/slotable"
1314require "view_component/slotable_default"
1415require "view_component/template"
@@ -63,6 +64,8 @@ def set_original_view_context(view_context)
6364 self . __vc_original_view_context = view_context
6465 end
6566
67+ using RequestDetails
68+
6669 # Entrypoint for rendering components.
6770 #
6871 # - `view_context`: ActionView context from calling view
@@ -90,13 +93,12 @@ def render_in(view_context, &block)
9093 # For i18n
9194 @virtual_path ||= virtual_path
9295
93- # For template variants (+phone, +desktop, etc. )
94- @__vc_variant ||= @lookup_context . variants . first
96+ # Describes the inferred request constraints (locales, formats, variants )
97+ @__vc_requested_details ||= @lookup_context . vc_requested_details
9598
9699 # For caching, such as #cache_if
97100 @current_template = nil unless defined? ( @current_template )
98101 old_current_template = @current_template
99- @current_template = self
100102
101103 if block && defined? ( @__vc_content_set_by_with_content )
102104 raise DuplicateContentError . new ( self . class . name )
@@ -108,7 +110,7 @@ def render_in(view_context, &block)
108110 before_render
109111
110112 if render?
111- rendered_template = render_template_for ( @__vc_variant , __vc_request &. format &. to_sym ) . to_s
113+ rendered_template = render_template_for ( @__vc_requested_details ) . to_s
112114
113115 # Avoid allocating new string when output_preamble and output_postamble are blank
114116 if output_preamble . blank? && output_postamble . blank?
@@ -156,7 +158,7 @@ def render_parent_to_string
156158 target_render = self . class . instance_variable_get ( :@__vc_ancestor_calls ) [ @__vc_parent_render_level ]
157159 @__vc_parent_render_level += 1
158160
159- target_render . bind_call ( self , @__vc_variant )
161+ target_render . bind_call ( self , @__vc_requested_details )
160162 ensure
161163 @__vc_parent_render_level -= 1
162164 end
@@ -267,11 +269,10 @@ def view_cache_dependencies
267269 [ ]
268270 end
269271
270- # For caching, such as #cache_if
271- #
272- # @private
272+ # Rails expects us to define `format` on all renderables,
273+ # but we do not know the `format` of a ViewComponent until runtime.
273274 def format
274- @__vc_variant if defined? ( @__vc_variant )
275+ nil
275276 end
276277
277278 # The current request. Use sparingly as doing so introduces coupling that
@@ -328,7 +329,7 @@ def content_evaluated?
328329 end
329330
330331 def maybe_escape_html ( text )
331- return text if __vc_request && !__vc_request . format . html?
332+ return text if @current_template && !@current_template . html?
332333 return text if text . blank?
333334
334335 if text . html_safe?
@@ -517,12 +518,12 @@ def inherited(child)
517518 # meaning it will not be called for any children and thus not compile their templates.
518519 if !child . instance_methods ( false ) . include? ( :render_template_for ) && !child . compiled?
519520 child . class_eval <<~RUBY , __FILE__ , __LINE__ + 1
520- def render_template_for(variant = nil, format = nil )
521+ def render_template_for(requested_details )
521522 # Force compilation here so the compiler always redefines render_template_for.
522523 # This is mostly a safeguard to prevent infinite recursion.
523524 self.class.compile(raise_errors: true, force: true)
524525 # .compile replaces this method; call the new one
525- render_template_for(variant, format )
526+ render_template_for(requested_details )
526527 end
527528 RUBY
528529 end
0 commit comments