@@ -45,21 +45,13 @@ def config
45
45
delegate :content_security_policy_nonce , to : :helpers
46
46
47
47
# Config option that strips trailing whitespace in templates before compiling them.
48
+ class_attribute :__vc_cache_dependencies , instance_accessor : false , instance_predicate : false , default : [ ]
48
49
class_attribute :__vc_strip_trailing_whitespace , instance_accessor : false , instance_predicate : false
49
50
self . __vc_strip_trailing_whitespace = false # class_attribute:default doesn't work until Rails 5.2
50
51
51
52
attr_accessor :__vc_original_view_context
52
53
53
- # Compoents can have a cache key that is used to cache the rendered output.
54
- #
55
- # @return [String]
56
- def cache_key
57
- @vc_cache_key = if defined? ( __vc_cache_args )
58
- Digest ::MD5 . hexdigest (
59
- __vc_cache_args . map { |method | send ( method ) } . join ( "-" )
60
- )
61
- end
62
- end
54
+
63
55
64
56
# Components render in their own view context. Helpers and other functionality
65
57
# require a reference to the original Rails view context, an instance of
@@ -121,8 +113,8 @@ def render_in(view_context, &block)
121
113
if render?
122
114
rendered_template = render_template_for ( @__vc_variant , __vc_request &.format &.to_sym ) . to_s
123
115
124
- if cache_key . present?
125
- Rails . cache . fetch ( @vc_cache_key ) do
116
+ if view_cache_dependencies . present?
117
+ Rails . cache . fetch ( view_cache_dependencies ) do
126
118
__vc_render_template ( rendered_template )
127
119
end
128
120
else
@@ -276,9 +268,11 @@ def virtual_path
276
268
# For caching, such as #cache_if
277
269
# @private
278
270
def view_cache_dependencies
279
- [ ]
271
+ self . class . view_cache_dependencies
280
272
end
281
273
274
+ alias_method :component_cache_dependencies , :view_cache_dependencies
275
+
282
276
# For caching, such as #cache_if
283
277
#
284
278
# @private
@@ -520,12 +514,16 @@ def sidecar_files(extensions)
520
514
( sidecar_files - [ identifier ] + sidecar_directory_files + nested_component_files ) . uniq
521
515
end
522
516
517
+
518
+
523
519
def cache_on ( *args )
524
- class_eval <<~RUBY , __FILE__ , __LINE__ + 1
525
- def __vc_cache_args
526
- #{ args }
527
- end
528
- RUBY
520
+ self . __vc_cache_dependencies . push ( *args )
521
+ end
522
+
523
+ def view_cache_dependencies
524
+ return unless __vc_cache_dependencies . any?
525
+
526
+ __vc_cache_dependencies . map { |dep | send ( dep ) } . compact
529
527
end
530
528
531
529
# Render a component for each element in a collection ([documentation](/guide/collections)):
0 commit comments