view_component 4.17.0 → 4.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/views/test_mailer/test_multipart_email.html.erb +1 -0
- data/app/views/test_mailer/test_multipart_email.text.erb +1 -0
- data/docs/CHANGELOG.md +234 -0
- data/lib/generators/view_component/abstract_generator.rb +24 -4
- data/lib/generators/view_component/component/component_generator.rb +8 -6
- data/lib/generators/view_component/component/templates/component.rb.tt +1 -1
- data/lib/generators/view_component/erb/erb_generator.rb +2 -2
- data/lib/generators/view_component/haml/haml_generator.rb +1 -1
- data/lib/generators/view_component/locale/locale_generator.rb +2 -6
- data/lib/generators/view_component/preview/preview_generator.rb +9 -6
- data/lib/generators/view_component/preview/templates/component_preview.rb.tt +2 -2
- data/lib/generators/view_component/rspec/rspec_generator.rb +1 -1
- data/lib/generators/view_component/rspec/templates/component_spec.rb.tt +1 -1
- data/lib/generators/view_component/slim/slim_generator.rb +1 -1
- data/lib/generators/view_component/stimulus/stimulus_generator.rb +3 -7
- data/lib/generators/view_component/test_unit/templates/component_test.rb.tt +2 -2
- data/lib/generators/view_component/test_unit/test_unit_generator.rb +8 -7
- data/lib/view_component/base.rb +169 -94
- data/lib/view_component/cache_digest/dependency_tracking.rb +43 -11
- data/lib/view_component/cache_digest/digestor_lookup.rb +77 -0
- data/lib/view_component/cache_digest/resolver.rb +163 -65
- data/lib/view_component/cache_digest.rb +384 -75
- data/lib/view_component/{experimentally_cacheable.rb → cacheable.rb} +50 -127
- data/lib/view_component/collection.rb +4 -9
- data/lib/view_component/compile_cache.rb +63 -3
- data/lib/view_component/compiler.rb +86 -15
- data/lib/view_component/config.rb +114 -8
- data/lib/view_component/engine.rb +18 -6
- data/lib/view_component/errors.rb +16 -0
- data/lib/view_component/request_details.rb +5 -10
- data/lib/view_component/slot.rb +17 -0
- data/lib/view_component/slotable.rb +32 -8
- data/lib/view_component/template.rb +38 -16
- data/lib/view_component/test_helpers.rb +39 -15
- data/lib/view_component/translatable.rb +35 -1
- data/lib/view_component/version.rb +1 -1
- data/lib/view_component.rb +1 -1
- metadata +10 -7
|
@@ -18,18 +18,26 @@ module ViewComponent
|
|
|
18
18
|
# Extensions whose contents are hashed into the synthetic source.
|
|
19
19
|
SIDECAR_EXTENSIONS = %w[yml yaml].freeze
|
|
20
20
|
|
|
21
|
+
RELATIVE_PARTIAL_PREFIX = "view_component/relative_partial/"
|
|
22
|
+
RELATIVE_DEPENDENCY = /<%# Relative Template Dependency: (\S+) %>/
|
|
23
|
+
private_constant :RELATIVE_PARTIAL_PREFIX, :RELATIVE_DEPENDENCY
|
|
24
|
+
|
|
25
|
+
# Where ViewComponent's own modules, such as the ones it includes into
|
|
26
|
+
# every component, are defined.
|
|
27
|
+
VIEW_COMPONENT_LIB = "#{::File.expand_path("..", __dir__)}/".freeze
|
|
28
|
+
|
|
21
29
|
class << self
|
|
22
30
|
def instance
|
|
23
31
|
INSTANCE
|
|
24
32
|
end
|
|
25
33
|
end
|
|
26
34
|
|
|
27
|
-
def find_templates(name, prefix, partial, details, locals = [])
|
|
35
|
+
def find_templates(name, prefix, partial, details, locals = [], view_paths: ActionController::Base.view_paths)
|
|
28
36
|
virtual_path = [prefix.presence, name].compact.join("/")
|
|
29
37
|
component = CacheDigest.component_for(virtual_path)
|
|
30
38
|
return [] unless component
|
|
31
39
|
|
|
32
|
-
[build_template(component, virtual_path, details)]
|
|
40
|
+
[build_template(component, virtual_path, details, view_paths)]
|
|
33
41
|
end
|
|
34
42
|
|
|
35
43
|
def to_s
|
|
@@ -42,91 +50,144 @@ module ViewComponent
|
|
|
42
50
|
end
|
|
43
51
|
alias_method :==, :eql?
|
|
44
52
|
|
|
45
|
-
private
|
|
46
|
-
|
|
47
|
-
def build_template(component, virtual_path, details)
|
|
48
|
-
ActionView::Template.new(
|
|
49
|
-
source_for(component),
|
|
50
|
-
"view_component cache digest for #{component.name}",
|
|
51
|
-
ActionView::Template.handler_for_extension(:erb),
|
|
52
|
-
locals: [],
|
|
53
|
-
format: Array(details[:formats]).first || :html,
|
|
54
|
-
virtual_path: virtual_path
|
|
55
|
-
)
|
|
56
|
-
end
|
|
57
|
-
|
|
58
53
|
# The synthetic source. Every section exists to change this string when
|
|
59
54
|
# something the component renders from changes.
|
|
55
|
+
#
|
|
56
|
+
# @param component [Class]
|
|
57
|
+
# @return [String]
|
|
60
58
|
def source_for(component)
|
|
61
59
|
parts = []
|
|
60
|
+
templates = template_sources(component)
|
|
62
61
|
|
|
63
62
|
# Safety net: this template should never be rendered, only digested.
|
|
64
63
|
parts << "<% raise ViewComponent::CacheDigestTemplateError.new(#{component.name.inspect}) %>"
|
|
65
64
|
|
|
66
65
|
# Content hashes of the Ruby files and sidecar files backing the
|
|
67
|
-
# component
|
|
68
|
-
# inlining keeps the source small and avoids
|
|
69
|
-
# tracker might misread as a render call.
|
|
66
|
+
# component, its component superclasses, and the modules they include.
|
|
67
|
+
# Hashing rather than inlining keeps the source small and avoids
|
|
68
|
+
# embedding Ruby that a tracker might misread as a render call.
|
|
70
69
|
source_files(component).each do |path|
|
|
71
70
|
parts << "<%# Resolved Dependency: #{path} #{file_digest(path)} %>"
|
|
72
71
|
end
|
|
73
72
|
|
|
74
|
-
# Everything the component renders
|
|
75
|
-
#
|
|
76
|
-
#
|
|
77
|
-
#
|
|
78
|
-
|
|
79
|
-
|
|
73
|
+
# Everything the component renders: `# Template Dependency:`
|
|
74
|
+
# declarations, the components its slots are declared with, and the
|
|
75
|
+
# components and partials referenced from its templates, in any
|
|
76
|
+
# language, and from its Ruby. Emitted as declarations so the Digestor
|
|
77
|
+
# resolves them as tree nodes without trackers having to find them in
|
|
78
|
+
# the template sources, where they'd also infer partials from component
|
|
79
|
+
# renders.
|
|
80
|
+
dependencies(component, templates).each do |dependency|
|
|
81
|
+
parts << if dependency.start_with?(RELATIVE_PARTIAL_PREFIX)
|
|
82
|
+
"<%# Relative Template Dependency: #{dependency.delete_prefix(RELATIVE_PARTIAL_PREFIX)} %>"
|
|
83
|
+
else
|
|
84
|
+
"<%# Template Dependency: #{dependency} %>"
|
|
85
|
+
end
|
|
80
86
|
end
|
|
81
87
|
|
|
82
|
-
#
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
parts << source
|
|
88
|
+
# Content hashes of the templates, including inline ones.
|
|
89
|
+
templates.each do |source, _extension|
|
|
90
|
+
parts << "<%# Template Digest: #{ActiveSupport::Digest.hexdigest(source)} %>"
|
|
86
91
|
end
|
|
87
92
|
|
|
88
93
|
parts.join("\n")
|
|
89
94
|
end
|
|
90
95
|
|
|
96
|
+
private
|
|
97
|
+
|
|
98
|
+
def build_template(component, virtual_path, details, view_paths)
|
|
99
|
+
source = component.__vc_cache_digest_source || source_for(component)
|
|
100
|
+
ActionView::Template.new(
|
|
101
|
+
resolve_relative_dependencies(source, view_paths),
|
|
102
|
+
"view_component cache digest for #{component.name}",
|
|
103
|
+
ActionView::Template.handler_for_extension(:erb),
|
|
104
|
+
locals: [],
|
|
105
|
+
format: Array(details[:formats]).first || :html,
|
|
106
|
+
virtual_path: virtual_path
|
|
107
|
+
)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# A relative render uses the calling controller's prefixes. Track all
|
|
111
|
+
# matching partials so precomputed sources work for every controller.
|
|
112
|
+
def resolve_relative_dependencies(source, view_paths)
|
|
113
|
+
return source unless source.include?("Relative Template Dependency:")
|
|
114
|
+
|
|
115
|
+
paths = view_paths.flat_map(&:all_template_paths).uniq
|
|
116
|
+
source.gsub(RELATIVE_DEPENDENCY) do
|
|
117
|
+
name = Regexp.last_match(1)
|
|
118
|
+
matches = paths.filter_map do |path|
|
|
119
|
+
path.to_s if path.partial? && ::File.fnmatch?(name.delete_prefix("_"), path.name)
|
|
120
|
+
end.sort
|
|
121
|
+
matches = [name] if matches.empty?
|
|
122
|
+
matches.map { |path| "<%# Template Dependency: #{path} %>" }.join("\n")
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
91
126
|
# The component and any component superclasses, nearest first. Including
|
|
92
127
|
# ancestors means editing `ApplicationComponent` invalidates every
|
|
93
128
|
# component that inherits from it.
|
|
94
129
|
def component_ancestors(component)
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
130
|
+
ancestors = []
|
|
131
|
+
until component == ViewComponent::Base
|
|
132
|
+
ancestors << component
|
|
133
|
+
component = component.superclass
|
|
99
134
|
end
|
|
135
|
+
ancestors
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Modules the component and its component superclasses include, such as
|
|
139
|
+
# a module of helpers that render other components. Modules included
|
|
140
|
+
# into `ViewComponent::Base` and above are left out, as superclasses
|
|
141
|
+
# are, and so are the modules ViewComponent adds to every component.
|
|
142
|
+
#
|
|
143
|
+
# @return [Array<String>] the files the modules are defined in
|
|
144
|
+
def module_files(component)
|
|
145
|
+
component.ancestors.take_while { |ancestor| ancestor != ViewComponent::Base }.filter_map { |ancestor|
|
|
146
|
+
next if ancestor.is_a?(Class) || ancestor.name.nil?
|
|
147
|
+
|
|
148
|
+
begin
|
|
149
|
+
path, _line = Object.const_source_location(ancestor.name)
|
|
150
|
+
rescue NameError
|
|
151
|
+
next
|
|
152
|
+
end
|
|
153
|
+
path unless path.nil? || path.start_with?(VIEW_COMPONENT_LIB)
|
|
154
|
+
}.uniq
|
|
100
155
|
end
|
|
101
156
|
|
|
102
157
|
def source_files(component)
|
|
103
|
-
component_ancestors(component).flat_map { |ancestor|
|
|
104
|
-
|
|
105
|
-
}.compact.uniq.select { |path| ::File.exist?(path) }
|
|
158
|
+
sidecars = component_ancestors(component).flat_map { |ancestor| ancestor.__vc_sidecar_files(SIDECAR_EXTENSIONS) }
|
|
159
|
+
(ruby_files(component) + sidecars).uniq.select { |path| ::File.exist?(path) }
|
|
106
160
|
end
|
|
107
161
|
|
|
108
|
-
def
|
|
109
|
-
|
|
110
|
-
.
|
|
111
|
-
|
|
112
|
-
|
|
162
|
+
def ruby_files(component)
|
|
163
|
+
CacheDigest.memoize([:ruby_files, component]) do
|
|
164
|
+
component_ancestors(component).map(&:identifier).concat(module_files(component))
|
|
165
|
+
.compact.uniq.select { |path| ::File.exist?(path) }
|
|
166
|
+
end
|
|
113
167
|
end
|
|
114
168
|
|
|
115
169
|
# Sidecar template files plus inline templates, which live in the Ruby
|
|
116
170
|
# file and so are invisible to Action View's trackers.
|
|
171
|
+
#
|
|
172
|
+
# @return [Array<Array(String, String, Class)>] source, extension, and defining component
|
|
117
173
|
def template_sources(component)
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
174
|
+
component_ancestors(component).flat_map do |ancestor|
|
|
175
|
+
sources = ancestor.__vc_sidecar_files(ActionView::Template::Handlers.extensions).filter_map do |path|
|
|
176
|
+
[CompileCache.read_file(path), ::File.extname(path).delete_prefix("."), ancestor] if ::File.exist?(path)
|
|
177
|
+
end
|
|
121
178
|
inline_template = ancestor.__vc_inline_template
|
|
122
|
-
sources << inline_template.source if inline_template
|
|
123
|
-
end
|
|
179
|
+
sources << [inline_template.source, inline_template.language.to_s, ancestor] if inline_template
|
|
124
180
|
|
|
125
|
-
|
|
181
|
+
sources
|
|
182
|
+
end.uniq
|
|
126
183
|
end
|
|
127
184
|
|
|
128
|
-
def explicit_dependencies(
|
|
129
|
-
|
|
185
|
+
def explicit_dependencies(sources)
|
|
186
|
+
sources.flat_map { |source|
|
|
187
|
+
# A cheap check first: most sources declare nothing, and the
|
|
188
|
+
# pattern is slow to scan for in sources with many comments.
|
|
189
|
+
next [] unless source.include?("Template Dependency:")
|
|
190
|
+
|
|
130
191
|
declared = source.scan(CacheDigest::EXPLICIT_DEPENDENCY).flatten
|
|
131
192
|
|
|
132
193
|
# Component class names are translated to the path they're digested
|
|
@@ -139,34 +200,71 @@ module ViewComponent
|
|
|
139
200
|
}.uniq
|
|
140
201
|
end
|
|
141
202
|
|
|
142
|
-
# Everything
|
|
143
|
-
#
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
203
|
+
# Everything but the component and its superclasses, which a component
|
|
204
|
+
# references in its own source but are digested as part of it already.
|
|
205
|
+
def dependencies(component, templates)
|
|
206
|
+
virtual_path = "#{RELATIVE_PARTIAL_PREFIX}template"
|
|
207
|
+
ruby = ruby_sources(component)
|
|
208
|
+
own_paths = component_ancestors(component).filter_map { |ancestor| CacheDigest.virtual_path_for(ancestor) }
|
|
148
209
|
|
|
149
|
-
explicit_dependencies(
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
210
|
+
dependencies = explicit_dependencies(ruby + templates.map(&:first)) |
|
|
211
|
+
ruby.flat_map { |source| ruby_dependencies(source, virtual_path) } |
|
|
212
|
+
CacheDigest.slot_renders_of(component) |
|
|
213
|
+
templates.flat_map { |source, extension, owner| template_dependencies(owner, source, extension, virtual_path) }
|
|
214
|
+
|
|
215
|
+
dependencies - own_paths
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# Components and partials rendered from Ruby, such as a `#call` method.
|
|
219
|
+
def ruby_dependencies(source, virtual_path)
|
|
220
|
+
CacheDigest.renders_in(source, virtual_path, view_paths: ActionController::Base.view_paths).flatten
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def template_dependencies(component, source, extension, virtual_path)
|
|
224
|
+
handler = ActionView::Template.handler_for_extension(extension)
|
|
225
|
+
return [] unless CacheDigest.template_dependencies?(source, handler)
|
|
226
|
+
|
|
227
|
+
template = ActionView::Template.new(
|
|
228
|
+
source,
|
|
229
|
+
"view_component cache digest for #{component.name}",
|
|
230
|
+
handler,
|
|
231
|
+
locals: [],
|
|
232
|
+
format: nil,
|
|
233
|
+
virtual_path: virtual_path
|
|
234
|
+
)
|
|
235
|
+
CacheDigest.renders_in(
|
|
236
|
+
CacheDigest.compiled_source(template), virtual_path,
|
|
237
|
+
view_paths: ActionController::Base.view_paths, nesting: [component]
|
|
238
|
+
).flatten
|
|
154
239
|
end
|
|
155
240
|
|
|
241
|
+
# Components loaded from encrypted files, as some commercial gems ship
|
|
242
|
+
# them, have binary identifiers that can't be scanned. They're still
|
|
243
|
+
# hashed by `file_digest`.
|
|
156
244
|
def ruby_sources(component)
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
245
|
+
ruby_files(component).filter_map { |path|
|
|
246
|
+
source = CompileCache.read_file(path)
|
|
247
|
+
source if source.valid_encoding?
|
|
160
248
|
}
|
|
161
249
|
end
|
|
162
250
|
|
|
163
251
|
def file_digest(path)
|
|
164
|
-
|
|
252
|
+
CacheDigest.memoize([:file_digest, path]) do
|
|
253
|
+
# Reuse source reads unless text mode can transcode or translate newlines.
|
|
254
|
+
source = if ::File::ALT_SEPARATOR.nil? &&
|
|
255
|
+
(Encoding.default_internal.nil? || Encoding.default_internal == Encoding.default_external)
|
|
256
|
+
CompileCache.read_file(path)
|
|
257
|
+
else
|
|
258
|
+
::File.binread(path)
|
|
259
|
+
end
|
|
260
|
+
ActiveSupport::Digest.hexdigest(source)
|
|
261
|
+
end
|
|
165
262
|
end
|
|
166
263
|
|
|
167
264
|
# Built once at load time rather than memoized, so no class-level state
|
|
168
|
-
# is written after boot.
|
|
169
|
-
# every call so it can't go stale when a
|
|
265
|
+
# is written after boot. Unless sources were precomputed at boot, the
|
|
266
|
+
# resolver reads from disk on every call so it can't go stale when a
|
|
267
|
+
# component changes.
|
|
170
268
|
INSTANCE = new
|
|
171
269
|
end
|
|
172
270
|
end
|