15
15
require "view_component/translatable"
16
16
require "view_component/with_content_helper"
17
17
require "view_component/use_helpers"
18
- require "view_component/cache_on"
19
18
20
19
module ViewComponent
21
20
class Base < ActionView ::Base
@@ -35,7 +34,6 @@ def config
35
34
include ViewComponent ::Slotable
36
35
include ViewComponent ::Translatable
37
36
include ViewComponent ::WithContentHelper
38
- include ViewComponent ::CacheOn
39
37
40
38
RESERVED_PARAMETER = :content
41
39
VC_INTERNAL_DEFAULT_FORMAT = :html
@@ -52,6 +50,19 @@ def config
52
50
53
51
attr_accessor :__vc_original_view_context
54
52
53
+ # TODO
54
+ #
55
+ # @return [String]
56
+ def cache_key
57
+ if defined? ( __vc_cache_args )
58
+ @vc_cache_key = Digest ::MD5 . hexdigest (
59
+ __vc_cache_args . map { |method | send ( method ) } . join ( "-" )
60
+ )
61
+ else
62
+ @vc_cache_key = nil
63
+ end
64
+ end
65
+
55
66
# Components render in their own view context. Helpers and other functionality
56
67
# require a reference to the original Rails view context, an instance of
57
68
# `ActionView::Base`. Use this method to set a reference to the original
@@ -112,11 +123,12 @@ def render_in(view_context, &block)
112
123
if render?
113
124
rendered_template = render_template_for ( @__vc_variant , __vc_request &.format &.to_sym ) . to_s
114
125
115
- # Avoid allocating new string when output_preamble and output_postamble are blank
116
- if output_preamble . blank? && output_postamble . blank?
117
- rendered_template
126
+ if cache_key . present?
127
+ Rails . cache . fetch ( @vc_cache_key ) do
128
+ __vc_render_template ( rendered_template )
129
+ end
118
130
else
119
- safe_output_preamble + rendered_template + safe_output_postamble
131
+ __vc_render_template ( rendered_template )
120
132
end
121
133
else
122
134
""
@@ -321,6 +333,17 @@ def __vc_render_in_block_provided?
321
333
defined? ( @view_context ) && @view_context && @__vc_render_in_block
322
334
end
323
335
336
+
337
+ # TODO
338
+ def __vc_render_template ( rendered_template )
339
+ # Avoid allocating new string when output_preamble and output_postamble are blank
340
+ if output_preamble . blank? && output_postamble . blank?
341
+ rendered_template
342
+ else
343
+ safe_output_preamble + rendered_template + safe_output_postamble
344
+ end
345
+ end
346
+
324
347
def __vc_content_set_by_with_content_defined?
325
348
defined? ( @__vc_content_set_by_with_content )
326
349
end
@@ -501,6 +524,14 @@ def sidecar_files(extensions)
501
524
( sidecar_files - [ identifier ] + sidecar_directory_files + nested_component_files ) . uniq
502
525
end
503
526
527
+ def cache_on ( *args )
528
+ class_eval <<~RUBY , __FILE__ , __LINE__ + 1
529
+ def __vc_cache_args
530
+ #{ args }
531
+ end
532
+ RUBY
533
+ end
534
+
504
535
# Render a component for each element in a collection ([documentation](/guide/collections)):
505
536
#
506
537
# ```ruby
0 commit comments