Skip to content

Commit 4ec893c

Browse files
committed
modulerize code
1 parent a5cca65 commit 4ec893c

File tree

2 files changed

+61
-29
lines changed

2 files changed

+61
-29
lines changed

lib/view_component/base.rb

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "action_view"
4+
require "view_component/cacheable"
45
require "active_support/configurable"
56
require "view_component/collection"
67
require "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

lib/view_component/cacheable.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
module ViewComponent::Cacheable
4+
extend ActiveSupport::Concern
5+
6+
included do
7+
class_attribute :__vc_cache_dependencies, default: []
8+
9+
# For caching, such as #cache_if
10+
#
11+
# @private
12+
def view_cache_dependencies
13+
return unless __vc_cache_dependencies.present? && __vc_cache_dependencies.any?
14+
15+
__vc_cache_dependencies.map { |dep| send(dep) }.compact
16+
end
17+
18+
# For handeling the output_preamble and output_postamble
19+
#
20+
# @private
21+
def __vc_render_template(rendered_template)
22+
# Avoid allocating new string when output_preamble and output_postamble are blank
23+
if output_preamble.blank? && output_postamble.blank?
24+
rendered_template
25+
else
26+
safe_output_preamble + rendered_template + safe_output_postamble
27+
end
28+
end
29+
30+
# For determing if a template is rendered with cache or not
31+
#
32+
# @private
33+
def __vc_render_cacheable(rendered_template)
34+
if view_cache_dependencies.present?
35+
Rails.cache.fetch(view_cache_dependencies) do
36+
__vc_render_template(rendered_template)
37+
end
38+
else
39+
__vc_render_template(rendered_template)
40+
end
41+
end
42+
end
43+
44+
class_methods do
45+
46+
# For caching the component
47+
def cache_on(*args)
48+
__vc_cache_dependencies.push(*args)
49+
end
50+
51+
def inherited(child)
52+
child.__vc_cache_dependencies = __vc_cache_dependencies.dup
53+
54+
super
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)