11# frozen_string_literal: true
22
33require "action_view"
4+ require "view_component/cacheable"
45require "active_support/configurable"
56require "view_component/collection"
67require "view_component/compile_cache"
@@ -34,6 +35,7 @@ def config
3435 include ViewComponent ::Slotable
3536 include ViewComponent ::Translatable
3637 include ViewComponent ::WithContentHelper
38+ include ViewComponent ::Cacheable
3739
3840 RESERVED_PARAMETER = :content
3941 VC_INTERNAL_DEFAULT_FORMAT = :html
@@ -45,7 +47,7 @@ def config
4547 delegate :content_security_policy_nonce , to : :helpers
4648
4749 # Config option that strips trailing whitespace in templates before compiling them.
48- class_attribute :__vc_cache_dependencies , default : [ ]
50+ # class_attribute :__vc_cache_dependencies, default: []
4951 class_attribute :__vc_strip_trailing_whitespace , instance_accessor : false , instance_predicate : false
5052 self . __vc_strip_trailing_whitespace = false # class_attribute:default doesn't work until Rails 5.2
5153
@@ -110,14 +112,7 @@ def render_in(view_context, &block)
110112
111113 if render?
112114 rendered_template = render_template_for ( @__vc_variant , __vc_request &.format &.to_sym ) . to_s
113-
114- if view_cache_dependencies . present?
115- Rails . cache . fetch ( view_cache_dependencies ) do
116- __vc_render_template ( rendered_template )
117- end
118- else
119- __vc_render_template ( rendered_template )
120- end
115+ __vc_render_cacheable ( rendered_template )
121116 else
122117 ""
123118 end
@@ -263,15 +258,6 @@ def virtual_path
263258 self . class . virtual_path
264259 end
265260
266- # For caching, such as #cache_if
267- #
268- # @private
269- def view_cache_dependencies
270- return unless __vc_cache_dependencies . present? && __vc_cache_dependencies . any?
271-
272- __vc_cache_dependencies . map { |dep | send ( dep ) } . compact
273- end
274-
275261 # For caching, such as #cache_if
276262 #
277263 # @private
@@ -324,15 +310,6 @@ def __vc_render_in_block_provided?
324310 defined? ( @view_context ) && @view_context && @__vc_render_in_block
325311 end
326312
327- def __vc_render_template ( rendered_template )
328- # Avoid allocating new string when output_preamble and output_postamble are blank
329- if output_preamble . blank? && output_postamble . blank?
330- rendered_template
331- else
332- safe_output_preamble + rendered_template + safe_output_postamble
333- end
334- end
335-
336313 def __vc_content_set_by_with_content_defined?
337314 defined? ( @__vc_content_set_by_with_content )
338315 end
@@ -581,8 +558,6 @@ def render_template_for(variant = nil, format = nil)
581558 child . instance_variable_set ( :@__vc_ancestor_calls , vc_ancestor_calls )
582559 end
583560
584- child . __vc_cache_dependencies = __vc_cache_dependencies . dup
585-
586561 super
587562 end
588563
0 commit comments