Skip to content

Commit 56108a3

Browse files
committed
fix cache
1 parent 58c36d2 commit 56108a3

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

lib/view_component/base.rb

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ def config
4545
delegate :content_security_policy_nonce, to: :helpers
4646

4747
# 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+
class_attribute :__vc_cache_dependencies, default: []
4949
class_attribute :__vc_strip_trailing_whitespace, instance_accessor: false, instance_predicate: false
5050
self.__vc_strip_trailing_whitespace = false # class_attribute:default doesn't work until Rails 5.2
5151

5252
attr_accessor :__vc_original_view_context
5353

54-
55-
5654
# Components render in their own view context. Helpers and other functionality
5755
# require a reference to the original Rails view context, an instance of
5856
# `ActionView::Base`. Use this method to set a reference to the original
@@ -295,6 +293,12 @@ def __vc_request
295293
@__vc_request ||= controller.request if controller.respond_to?(:request)
296294
end
297295

296+
def view_cache_dependencies
297+
return unless __vc_cache_dependencies.present? && __vc_cache_dependencies.any?
298+
299+
__vc_cache_dependencies.map { |dep| send(dep) }.compact
300+
end
301+
298302
# The content passed to the component instance as a block.
299303
#
300304
# @return [String]
@@ -458,7 +462,8 @@ def safe_output_postamble
458462
# config.view_component.generate.preview = true
459463
# ```
460464
#
461-
# Defaults to `false`.
465+
# Defaults to `false`
466+
#
462467

463468
class << self
464469
# The file path of the component Ruby file.
@@ -472,6 +477,8 @@ class << self
472477
# @private
473478
attr_accessor :virtual_path
474479

480+
481+
475482
# Find sidecar files for the given extensions.
476483
#
477484
# The provided array of extensions is expected to contain
@@ -514,16 +521,8 @@ def sidecar_files(extensions)
514521
(sidecar_files - [identifier] + sidecar_directory_files + nested_component_files).uniq
515522
end
516523

517-
518-
519524
def cache_on(*args)
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
525+
__vc_cache_dependencies.push(*args)
527526
end
528527

529528
# Render a component for each element in a collection ([documentation](/guide/collections)):
@@ -591,6 +590,8 @@ def render_template_for(variant = nil, format = nil)
591590
child.instance_variable_set(:@__vc_ancestor_calls, vc_ancestor_calls)
592591
end
593592

593+
child.__vc_cache_dependencies = self.__vc_cache_dependencies.dup
594+
594595
super
595596
end
596597

test/sandbox/test/rendering_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,16 +1245,16 @@ def test_cache_component
12451245
component = CacheComponent.new(foo: "foo", bar: "bar")
12461246
render_inline(component)
12471247

1248-
assert_selector(".cache-component__cache-key", text: component.component_cache_dependencies)
1248+
assert_selector(".cache-component__cache-key", text: component.view_cache_dependencies)
12491249
assert_selector(".cache-component__cache-message", text: "foo bar")
12501250

12511251
render_inline(CacheComponent.new(foo: "foo", bar: "bar"))
12521252

1253-
assert_selector(".cache-component__cache-key", text: component.component_cache_dependencies)
1253+
assert_selector(".cache-component__cache-key", text: component.view_cache_dependencies)
12541254

12551255
render_inline(CacheComponent.new(foo: "foo", bar: "baz"))
12561256

1257-
refute_selector(".cache-component__cache-key", text: component.component_cache_dependencies)
1257+
refute_selector(".cache-component__cache-key", text: component.view_cache_dependencies)
12581258
refute_selector(".cache-component__cache-message", text: "foo bar")
12591259
end
12601260
end

0 commit comments

Comments
 (0)