Skip to content

Commit c771954

Browse files
committed
get pr up top date
1 parent 52607fc commit c771954

File tree

6 files changed

+40
-70
lines changed

6 files changed

+40
-70
lines changed

lib/view_component/base.rb

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def config
7575
# Config option that strips trailing whitespace in templates before compiling them.
7676

7777
class_attribute :__vc_response_format, instance_accessor: false, instance_predicate: false, default: nil
78-
class_attribute :__vc_cache_dependencies, instance_accessor: false, instance_predicate: false, default: []
78+
7979
class_attribute :__vc_strip_trailing_whitespace, instance_accessor: false, instance_predicate: false
8080
self.__vc_strip_trailing_whitespace = false # class_attribute:default doesn't work until Rails 5.2
8181

@@ -318,15 +318,6 @@ def virtual_path
318318
self.class.virtual_path
319319
end
320320

321-
# For caching, such as #cache_if
322-
#
323-
# @private
324-
def view_cache_dependencies
325-
return unless __vc_cache_dependencies.present? && __vc_cache_dependencies.any?
326-
327-
__vc_cache_dependencies.filter_map { |dep| send(dep) }
328-
end
329-
330321
# For caching, such as #cache_if
331322
#
332323
# @private
@@ -348,15 +339,6 @@ def __vc_request
348339
@__vc_request ||= controller.request if controller.respond_to?(:request)
349340
end
350341

351-
# For caching, such as #cache_if
352-
#
353-
# @private
354-
def view_cache_dependencies
355-
return unless __vc_cache_dependencies.present? && __vc_cache_dependencies.any?
356-
357-
__vc_cache_dependencies.filter_map { |dep| send(dep) }
358-
end
359-
360342
# The content passed to the component instance as a block.
361343
#
362344
# @return [String]
@@ -372,7 +354,7 @@ def content
372354
end
373355
end
374356

375-
# Whether `content` has been passed to the component.
357+
# Whether f render?`content` has been passed to the component.
376358
#
377359
# @return [Boolean]
378360
def content?
@@ -543,16 +525,6 @@ def sidecar_files(extensions)
543525
(sidecar_files - [identifier] + sidecar_directory_files + nested_component_files).uniq
544526
end
545527

546-
def cache_on(*args)
547-
__vc_cache_dependencies.push(*args)
548-
end
549-
550-
def view_cache_dependencies
551-
return unless __vc_cache_dependencies.any?
552-
553-
__vc_cache_dependencies.filter_map { |dep| send(dep) }
554-
end
555-
556528
# Render a component for each element in a collection ([documentation](/guide/collections)):
557529
#
558530
# ```ruby

lib/view_component/cache_digestor.rb

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
# # frozen_string_literal: true
22

3-
require 'view_component/templat_dependency_extractor'
3+
require "view_component/templat_dependency_extractor"
44

55
module ViewComponent
66
class CacheDigestor
77
def initialize(component:)
8-
@component= component.class
8+
@component = component
99
end
1010

1111
def digest
12-
gather_templates
13-
@templates.map do |template|
14-
if template.type == :file
15-
template_string = template.source
16-
ViewComponent::TemplateDependencyExtractor.new(template_string, template.extension.to_sym).extract
17-
else
18-
# A digest cant be built for inline calls as there is no template to parse
19-
[]
20-
end
12+
template = @component.current_template
13+
if template.nil? && template == :inline_call
14+
[]
15+
else
16+
template_string = template.source
17+
ViewComponent::TemplateDependencyExtractor.new(template_string, template.details.handler).extract
2118
end
2219
end
23-
24-
def gather_templates
25-
@templates = @component.compiler.send(:gather_templates)
26-
end
2720
end
2821
end

lib/view_component/cacheable.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# frozen_string_literal: true
22

3-
require "set"
43
require "view_component/cache_registry"
54
require "view_component/cache_digestor"
65

76
module ViewComponent::Cacheable
87
extend ActiveSupport::Concern
98

109
included do
11-
1210
class_attribute :__vc_cache_options, default: Set[:identifier]
1311
class_attribute :__vc_cache_dependencies, default: Set.new
1412

@@ -29,32 +27,32 @@ def view_cache_options
2927
# Render component from cache if possible
3028
#
3129
# @private
32-
def __vc_render_cacheable(rendered_template)
33-
if __vc_cache_options.any?
30+
def __vc_render_cacheable(safe_call)
31+
if (__vc_cache_options - [:identifier]).any?
3432
ViewComponent::CachingRegistry.track_caching do
35-
template_fragment(rendered_template)
33+
template_fragment(safe_call)
3634
end
3735
else
38-
__vc_render_template(rendered_template)
36+
instance_exec(&safe_call)
3937
end
4038
end
4139

42-
def template_fragment(rendered_template)
40+
def template_fragment
4341
if content = read_fragment
4442
@view_renderer.cache_hits[@current_template&.virtual_path] = :hit if defined?(@view_renderer)
4543
content
4644
else
4745
@view_renderer.cache_hits[@current_template&.virtual_path] = :miss if defined?(@view_renderer)
48-
write_fragment(rendered_template)
46+
write_fragment
4947
end
5048
end
5149

5250
def read_fragment
5351
Rails.cache.fetch(view_cache_options)
5452
end
5553

56-
def write_fragment(rendered_template)
57-
content = __vc_render_template(rendered_template)
54+
def write_fragment
55+
content = instance_exec(&safe_call)
5856
Rails.cache.fetch(view_cache_options) do
5957
content
6058
end
@@ -81,7 +79,7 @@ def cache_on(*args)
8179

8280
def inherited(child)
8381
child.__vc_cache_options = __vc_cache_options.dup
84-
82+
8583
super
8684
end
8785
end

lib/view_component/compiler.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,21 @@ def define_render_template_for
9797
safe_call = template.safe_method_name_call
9898
@component.define_method(:render_template_for) do |_|
9999
@current_template = template
100-
instance_exec(&safe_call)
100+
if @component.respond_to?(:__vc_render_cacheable)
101+
@component.__vc_render_cacheable(safe_call)
102+
else
103+
instance_exec(&safe_call)
104+
end
101105
end
102106
else
103107
compiler = self
104108
@component.define_method(:render_template_for) do |details|
105109
if (@current_template = compiler.find_templates_for(details).first)
106-
instance_exec(&@current_template.safe_method_name_call)
110+
if @component.respond_to?(:__vc_render_cacheable)
111+
@component.__vc_render_cacheable(@current_template.safe_method_name_call)
112+
else
113+
instance_exec(&@current_template.safe_method_name_call)
114+
end
107115
else
108116
raise MissingTemplateError.new(self.class.name, details)
109117
end

lib/view_component/templat_dependency_extractor.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
21
# frozen_string_literal: true
32

4-
require_relative 'template_ast_builder'
5-
require_relative 'prism_render_dependency_extractor'
3+
require_relative "template_ast_builder"
4+
require_relative "prism_render_dependency_extractor"
65

76
module ViewComponent
87
class TemplateDependencyExtractor
@@ -14,7 +13,8 @@ def initialize(template_string, engine)
1413

1514
def extract
1615
ast = TemplateAstBuilder.build(@template_string, @engine)
17-
walk(ast.split(';'))
16+
return @dependencies unless ast.present?
17+
walk(ast.split(";"))
1818
@dependencies.uniq
1919
end
2020

@@ -35,7 +35,7 @@ def extract_from_ruby(ruby_code)
3535

3636
def extract_partial_or_layout(code)
3737
partial_match = code.match(/partial:\s*["']([^"']+)["']/)
38-
layout_match = code.match(/layout:\s*["']([^"']+)["']/)
38+
layout_match = code.match(/layout:\s*["']([^"']+)["']/)
3939
direct_render = code.match(/render\s*\(?\s*["']([^"']+)["']/)
4040

4141
@dependencies << partial_match[1] if partial_match

lib/view_component/template_ast_builder.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
# frozen_string_literal: true
32

4-
require 'temple'
5-
require 'slim'
6-
require 'haml'
7-
require 'erb'
3+
require "temple"
4+
require "slim"
5+
require "haml"
6+
require "erb"
87

98
module ViewComponent
109
class TemplateAstBuilder
@@ -25,12 +24,12 @@ def call(template)
2524
ENGINE_MAP = {
2625
slim: -> { Slim::Engine.new },
2726
haml: -> { HamlTempleWrapper.new },
28-
erb: -> { ErbTempleWrapper.new }
27+
erb: -> { ErbTempleWrapper.new }
2928
}
3029

3130
def self.build(template_string, engine_name)
3231
engine = ENGINE_MAP.fetch(engine_name.to_sym) do
33-
raise ArgumentError, "Unsupported engine: #{engine_name.inspect}"
32+
return nil
3433
end.call
3534

3635
engine.call(template_string)

0 commit comments

Comments
 (0)