Skip to content

Commit 3d3bf91

Browse files
committed
fix cacahe implementatation to work with all methods
1 parent a6ab562 commit 3d3bf91

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

lib/view_component/base.rb

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
require "view_component/translatable"
1616
require "view_component/with_content_helper"
1717
require "view_component/use_helpers"
18-
require "view_component/cache_on"
1918

2019
module ViewComponent
2120
class Base < ActionView::Base
@@ -35,7 +34,6 @@ def config
3534
include ViewComponent::Slotable
3635
include ViewComponent::Translatable
3736
include ViewComponent::WithContentHelper
38-
include ViewComponent::CacheOn
3937

4038
RESERVED_PARAMETER = :content
4139
VC_INTERNAL_DEFAULT_FORMAT = :html
@@ -52,6 +50,19 @@ def config
5250

5351
attr_accessor :__vc_original_view_context
5452

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+
5566
# Components render in their own view context. Helpers and other functionality
5667
# require a reference to the original Rails view context, an instance of
5768
# `ActionView::Base`. Use this method to set a reference to the original
@@ -112,11 +123,12 @@ def render_in(view_context, &block)
112123
if render?
113124
rendered_template = render_template_for(@__vc_variant, __vc_request&.format&.to_sym).to_s
114125

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
118130
else
119-
safe_output_preamble + rendered_template + safe_output_postamble
131+
__vc_render_template(rendered_template)
120132
end
121133
else
122134
""
@@ -321,6 +333,17 @@ def __vc_render_in_block_provided?
321333
defined?(@view_context) && @view_context && @__vc_render_in_block
322334
end
323335

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+
324347
def __vc_content_set_by_with_content_defined?
325348
defined?(@__vc_content_set_by_with_content)
326349
end
@@ -501,6 +524,14 @@ def sidecar_files(extensions)
501524
(sidecar_files - [identifier] + sidecar_directory_files + nested_component_files).uniq
502525
end
503526

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+
504535
# Render a component for each element in a collection ([documentation](/guide/collections)):
505536
#
506537
# ```ruby

0 commit comments

Comments
 (0)