Skip to content

Commit d2ea35f

Browse files
committed
refactor digetor a bit
1 parent f24abe0 commit d2ea35f

File tree

2 files changed

+4
-61
lines changed

2 files changed

+4
-61
lines changed

lib/view_component/cache_digestor.rb

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def digest
1212
gather_templates
1313
@templates.map do |template|
1414
if template.type == :file
15-
template_string = template.send(:source)
15+
template_string = template.source
1616
ViewComponent::TemplateDependencyExtractor.new(template_string, template.extension.to_sym).extract
1717
else
1818
# A digest cant be built for inline calls as there is no template to parse
@@ -22,64 +22,7 @@ def digest
2222
end
2323

2424
def gather_templates
25-
@templates ||=
26-
begin
27-
templates = @component.sidecar_files(
28-
ActionView::Template.template_handler_extensions
29-
).map do |path|
30-
# Extract format and variant from template filename
31-
this_format, variant =
32-
File
33-
.basename(path) # "variants_component.html+mini.watch.erb"
34-
.split(".")[1..-2] # ["html+mini", "watch"]
35-
.join(".") # "html+mini.watch"
36-
.split("+") # ["html", "mini.watch"]
37-
.map(&:to_sym) # [:html, :"mini.watch"]
38-
39-
out = Template.new(
40-
component: @component,
41-
type: :file,
42-
path: path,
43-
lineno: 0,
44-
extension: path.split(".").last,
45-
this_format: this_format.to_s.split(".").last&.to_sym, # strip locale from this_format, see #2113
46-
variant: variant
47-
)
48-
49-
out
50-
end
51-
52-
component_instance_methods_on_self = @component.instance_methods(false)
53-
54-
(
55-
@component.ancestors.take_while { |ancestor| ancestor != ViewComponent::Base } - @component.included_modules
56-
).flat_map { |ancestor| ancestor.instance_methods(false).grep(/^call(_|$)/) }
57-
.uniq
58-
.each do |method_name|
59-
templates << Template.new(
60-
component: @component,
61-
type: :inline_call,
62-
this_format: ViewComponent::Base::VC_INTERNAL_DEFAULT_FORMAT,
63-
variant: method_name.to_s.include?("call_") ? method_name.to_s.sub("call_", "").to_sym : nil,
64-
method_name: method_name,
65-
defined_on_self: component_instance_methods_on_self.include?(method_name)
66-
)
67-
end
68-
69-
if @component.inline_template.present?
70-
templates << Template.new(
71-
component: @component,
72-
type: :inline,
73-
path: @component.inline_template.path,
74-
lineno: @component.inline_template.lineno,
75-
source: @component.inline_template.source.dup,
76-
extension: @component.inline_template.language
77-
)
78-
end
79-
80-
templates
81-
end
25+
@templates = @component.compiler.send(:gather_templates)
8226
end
83-
8427
end
8528
end

lib/view_component/template.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ def defined_on_self?
9999
@defined_on_self
100100
end
101101

102-
private
103-
104102
def source
105103
if @source_originally_nil
106104
# Load file each time we look up #source in case the file has been modified
@@ -110,6 +108,8 @@ def source
110108
end
111109
end
112110

111+
private
112+
113113
def compiled_source
114114
handler = ActionView::Template.handler_for_extension(@extension)
115115
this_source = source

0 commit comments

Comments
 (0)