view_component 4.19.0 → 4.23.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 +180 -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 +127 -101
- data/lib/view_component/cache_digest/dependency_tracking.rb +27 -11
- data/lib/view_component/cache_digest/digestor_lookup.rb +47 -1
- data/lib/view_component/cache_digest/resolver.rb +146 -52
- data/lib/view_component/cache_digest.rb +346 -56
- data/lib/view_component/cacheable.rb +16 -18
- data/lib/view_component/collection.rb +4 -9
- data/lib/view_component/compile_cache.rb +62 -2
- data/lib/view_component/compiler.rb +76 -13
- data/lib/view_component/config.rb +114 -8
- data/lib/view_component/engine.rb +10 -6
- data/lib/view_component/errors.rb +16 -0
- data/lib/view_component/request_details.rb +5 -10
- data/lib/view_component/slot.rb +12 -0
- data/lib/view_component/slotable.rb +32 -8
- data/lib/view_component/template.rb +33 -15
- data/lib/view_component/test_helpers.rb +40 -16
- data/lib/view_component/translatable.rb +35 -1
- data/lib/view_component/version.rb +1 -1
- metadata +17 -1
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
require "active_support/dependencies/autoload"
|
|
4
4
|
require "action_view/digestor"
|
|
5
5
|
require "action_view/render_parser"
|
|
6
|
+
require "prism"
|
|
7
|
+
require "strscan"
|
|
6
8
|
|
|
7
9
|
module ViewComponent
|
|
8
10
|
# Integrates ViewComponents into Rails' template digest tree.
|
|
@@ -38,23 +40,19 @@ module ViewComponent
|
|
|
38
40
|
# application partial.
|
|
39
41
|
VIRTUAL_PATH_PREFIX = "view_component/cache_digest"
|
|
40
42
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
# third-party Haml/Slim trackers.
|
|
47
|
-
RENDER_CALL = /
|
|
48
|
-
\brender(?:_to_string)?\b # render or render_to_string
|
|
49
|
-
\s*\(?\s* # optional opening paren
|
|
50
|
-
(?<const>
|
|
51
|
-
(?:::)?[A-Z]\w* # a constant
|
|
52
|
-
(?:::[A-Z]\w*)* # optionally namespaced
|
|
53
|
-
)
|
|
54
|
-
/x
|
|
43
|
+
RENDER_METHODS = %i[render render_to_string].freeze
|
|
44
|
+
|
|
45
|
+
COMPILED_SOURCES = :view_component_cache_digest_compiled_sources
|
|
46
|
+
MEMO = :view_component_cache_digest_memo
|
|
47
|
+
private_constant :COMPILED_SOURCES, :MEMO
|
|
55
48
|
|
|
56
49
|
# Rails' escape hatch for dependencies static analysis can't see.
|
|
57
50
|
EXPLICIT_DEPENDENCY = /#\s*Template Dependency:\s*(\S+)/
|
|
51
|
+
|
|
52
|
+
# A constant path made only of constants, such as `Foo` or `::Foo::Bar`,
|
|
53
|
+
# rather than one hanging off an expression, such as `self.class::Bar`.
|
|
54
|
+
CONSTANT_NAME = /\A(?:::)?[A-Z]\w*(?:::[A-Z]\w*)*\z/
|
|
55
|
+
|
|
58
56
|
class << self
|
|
59
57
|
# Virtual paths of components, mapped to their class names.
|
|
60
58
|
#
|
|
@@ -102,46 +100,108 @@ module ViewComponent
|
|
|
102
100
|
constantize_component(name)
|
|
103
101
|
end
|
|
104
102
|
|
|
105
|
-
#
|
|
103
|
+
# Compile a template to Ruby with its own handler, so ERB, Haml, Slim and
|
|
104
|
+
# any other template language can be analyzed the same way. Reuses the
|
|
105
|
+
# Ruby compiled at boot, when there is some.
|
|
106
106
|
#
|
|
107
|
-
# @return [
|
|
108
|
-
def
|
|
109
|
-
|
|
107
|
+
# @return [String] Ruby source
|
|
108
|
+
def compiled_source(template)
|
|
109
|
+
source = template.source
|
|
110
|
+
Thread.current[COMPILED_SOURCES]&.dig(template.handler, source) || template.handler.call(template, source)
|
|
110
111
|
end
|
|
111
112
|
|
|
112
|
-
#
|
|
113
|
-
#
|
|
113
|
+
# Skip literal templates and ERB markup before compiling Ruby for analysis.
|
|
114
|
+
# Other handlers keep a conservative check because their code syntax varies.
|
|
115
|
+
#
|
|
116
|
+
# @param source [String]
|
|
117
|
+
# @param handler [#call]
|
|
114
118
|
#
|
|
115
|
-
# @return [
|
|
116
|
-
def
|
|
117
|
-
return
|
|
119
|
+
# @return [Boolean]
|
|
120
|
+
def template_dependencies?(source, handler)
|
|
121
|
+
return false unless source.match?(/render|[A-Z]/)
|
|
122
|
+
|
|
123
|
+
case handler
|
|
124
|
+
when ActionView::Template::Handlers::Raw, ActionView::Template::Handlers::Html
|
|
125
|
+
false
|
|
126
|
+
when ActionView::Template::Handlers::ERB
|
|
127
|
+
# Never restart the search at another opener after an unterminated tag.
|
|
128
|
+
scanner = StringScanner.new(source)
|
|
129
|
+
while scanner.skip_until(/<%/)
|
|
130
|
+
code = scanner.scan_until(/%>/)
|
|
131
|
+
return false unless code
|
|
132
|
+
next if code.start_with?("#", "%")
|
|
133
|
+
|
|
134
|
+
return true if code.match?(/render|[A-Z]/)
|
|
135
|
+
end
|
|
136
|
+
false
|
|
137
|
+
else
|
|
138
|
+
true
|
|
139
|
+
end
|
|
140
|
+
end
|
|
118
141
|
|
|
119
|
-
|
|
120
|
-
|
|
142
|
+
# Find the components Ruby source references and the partials it
|
|
143
|
+
# renders.
|
|
144
|
+
#
|
|
145
|
+
# Every constant that names a component counts, not only one passed
|
|
146
|
+
# straight to `render`: a component can also be rendered from a method
|
|
147
|
+
# that builds it, from a local variable, or from a slot declared with its
|
|
148
|
+
# class. A component referenced without being rendered only makes the
|
|
149
|
+
# digest change more often than it needs to.
|
|
150
|
+
#
|
|
151
|
+
# Action View's render parser only sees the calls that don't render a
|
|
152
|
+
# component, so it can't infer partials from them.
|
|
153
|
+
#
|
|
154
|
+
# @param source [String] Ruby source
|
|
155
|
+
# @param name [String] virtual path the source is being digested under
|
|
156
|
+
# @param view_paths [ActionView::PathSet, nil] paths to check dynamic partial guesses against
|
|
157
|
+
# @param nesting [Array<Module>] lexical scope of compiled template source
|
|
158
|
+
# @return [Array(Array<String>, Array<String>)] synthetic virtual paths of
|
|
159
|
+
# components and partial virtual paths
|
|
160
|
+
def renders_in(source, name, view_paths: nil, nesting: [])
|
|
161
|
+
# Shared by every component that includes a module or inherits from
|
|
162
|
+
# the same superclass, so it's scanned once per digest computation.
|
|
163
|
+
memoize([:renders_in, source, name, view_paths, nesting]) do
|
|
164
|
+
components, _component_calls, other_calls = partition_render_calls(Prism.parse(source).value, nesting)
|
|
165
|
+
partials = partials_rendered_by(other_calls, name) - unresolved_partials_rendered_by(other_calls, name, view_paths)
|
|
166
|
+
[component_paths(components), partials]
|
|
121
167
|
end
|
|
122
168
|
end
|
|
123
169
|
|
|
124
|
-
#
|
|
125
|
-
#
|
|
170
|
+
# Find the components Ruby source renders, along with partial guesses
|
|
171
|
+
# Action View should discard.
|
|
126
172
|
#
|
|
127
|
-
#
|
|
128
|
-
#
|
|
129
|
-
#
|
|
130
|
-
#
|
|
131
|
-
# `things/_thing` entries the parser infers from dynamic renders like
|
|
132
|
-
# `render @thing` or `render FooComponent.new`. Those would resolve to
|
|
133
|
-
# nothing and only add log noise; components rendered from Ruby are
|
|
134
|
-
# already found precisely by `component_paths_in`.
|
|
173
|
+
# Action View reads `render FooComponent.new` as a dynamic render of a
|
|
174
|
+
# partial named after the last method, inferring `news/new`. Partials also
|
|
175
|
+
# rendered some other way aren't returned. Unresolved guesses from other
|
|
176
|
+
# dynamic renders are discarded when view paths are available.
|
|
135
177
|
#
|
|
136
178
|
# @param source [String] Ruby source
|
|
137
179
|
# @param name [String] virtual path the source is being digested under
|
|
138
|
-
# @
|
|
139
|
-
|
|
140
|
-
|
|
180
|
+
# @param view_paths [ActionView::PathSet, nil] paths to check dynamic partial guesses against
|
|
181
|
+
# @return [Array(Array<String>, Array<String>)] synthetic virtual paths of
|
|
182
|
+
# components and inferred partial virtual paths
|
|
183
|
+
def template_renders_in(source, name, view_paths: nil)
|
|
184
|
+
components, component_calls, other_calls = partition_render_calls(Prism.parse(source).value)
|
|
185
|
+
|
|
186
|
+
[
|
|
187
|
+
component_paths(components),
|
|
188
|
+
(partials_rendered_by(component_calls, name) - partials_rendered_by(other_calls, name)) |
|
|
189
|
+
unresolved_partials_rendered_by(other_calls, name, view_paths)
|
|
190
|
+
]
|
|
191
|
+
end
|
|
141
192
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
193
|
+
# Find the components a component's slots render, from the class each
|
|
194
|
+
# slot was declared with: `renders_one :header, HeaderComponent` or
|
|
195
|
+
# `renders_many :items, "ItemComponent"`. Read from the slot definitions
|
|
196
|
+
# rather than from source, since a class named by a string, or by a name
|
|
197
|
+
# relative to the component, resolves against the component at runtime.
|
|
198
|
+
#
|
|
199
|
+
# @param component [Class]
|
|
200
|
+
# @return [Array<String>] synthetic virtual paths of components
|
|
201
|
+
def slot_renders_of(component)
|
|
202
|
+
definitions = component.registered_slots.values.flat_map { |slot| slot[:renderable_hash]&.values || [slot] }
|
|
203
|
+
|
|
204
|
+
component_paths(definitions.filter_map { |slot| slot_component(component, slot) })
|
|
145
205
|
end
|
|
146
206
|
|
|
147
207
|
# Action View has shipped its render parser as a class (Rails 7.1, and
|
|
@@ -178,24 +238,28 @@ module ViewComponent
|
|
|
178
238
|
# Compute the digest of a component using Rails' digest tree.
|
|
179
239
|
#
|
|
180
240
|
# @param component [Class] a component class
|
|
181
|
-
# @param finder [ActionView::LookupContext]
|
|
241
|
+
# @param finder [ActionView::LookupContext, nil]
|
|
182
242
|
# @param format [Symbol]
|
|
243
|
+
#
|
|
183
244
|
# @return [String]
|
|
184
|
-
def digest(component, finder:
|
|
245
|
+
def digest(component, finder: nil, format: :html)
|
|
185
246
|
register(component)
|
|
186
247
|
virtual_path = virtual_path_for(component)
|
|
187
248
|
return "" unless virtual_path
|
|
188
249
|
|
|
250
|
+
finder ||= default_finder(format: format)
|
|
189
251
|
ActionView::Digestor.digest(name: virtual_path, format: format, finder: finder)
|
|
190
252
|
end
|
|
191
253
|
|
|
192
254
|
# A lookup context for digesting components outside a request, where no
|
|
193
255
|
# view context (and therefore no finder) exists.
|
|
194
256
|
#
|
|
257
|
+
# @param format [Symbol]
|
|
258
|
+
#
|
|
195
259
|
# @return [ActionView::LookupContext]
|
|
196
|
-
def default_finder
|
|
260
|
+
def default_finder(format: :html)
|
|
197
261
|
# Not memoized across reloads: view paths change when the app reloads.
|
|
198
|
-
ActionView::LookupContext.new(ActionController::Base.view_paths)
|
|
262
|
+
ActionView::LookupContext.new(ActionController::Base.view_paths, formats: [format])
|
|
199
263
|
end
|
|
200
264
|
|
|
201
265
|
# Wire the tracker and resolver into Action View.
|
|
@@ -208,6 +272,59 @@ module ViewComponent
|
|
|
208
272
|
DigestorLookup.install!
|
|
209
273
|
end
|
|
210
274
|
|
|
275
|
+
# Keep the Ruby that templates compile to while the block runs, so
|
|
276
|
+
# `precompute!` can analyze templates components just compiled without
|
|
277
|
+
# compiling them again. Kept in a thread local rather than on the module,
|
|
278
|
+
# which isn't writable from other Ractors.
|
|
279
|
+
#
|
|
280
|
+
# @private
|
|
281
|
+
def retaining_compiled_sources
|
|
282
|
+
Thread.current[COMPILED_SOURCES] = {}
|
|
283
|
+
yield
|
|
284
|
+
ensure
|
|
285
|
+
Thread.current[COMPILED_SOURCES] = nil
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
# Share synthetic templates between every lookup in the block.
|
|
289
|
+
#
|
|
290
|
+
# The Digestor looks a template up once per edge of the tree, before it
|
|
291
|
+
# checks whether it has seen the template already. Action View's own
|
|
292
|
+
# resolvers cache their templates, but building a synthetic one reads and
|
|
293
|
+
# parses the component's sources, so a component rendered from many
|
|
294
|
+
# places was rebuilt once per place. Kept for one digest computation
|
|
295
|
+
# only, so a source edited between requests is still seen.
|
|
296
|
+
#
|
|
297
|
+
# @private
|
|
298
|
+
def memoizing
|
|
299
|
+
return yield if Thread.current[MEMO]
|
|
300
|
+
|
|
301
|
+
begin
|
|
302
|
+
Thread.current[MEMO] = {}
|
|
303
|
+
yield
|
|
304
|
+
ensure
|
|
305
|
+
Thread.current[MEMO] = nil
|
|
306
|
+
end
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# @private
|
|
310
|
+
def memo
|
|
311
|
+
Thread.current[MEMO]
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# @private
|
|
315
|
+
def memoize(key)
|
|
316
|
+
memo = Thread.current[MEMO]
|
|
317
|
+
return yield unless memo
|
|
318
|
+
|
|
319
|
+
memo.fetch(key) { memo[key] = yield }
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
# @private
|
|
323
|
+
def compiled(handler, source, compiled_source)
|
|
324
|
+
sources = Thread.current[COMPILED_SOURCES]
|
|
325
|
+
(sources[handler] ||= {})[source] = compiled_source if sources
|
|
326
|
+
end
|
|
327
|
+
|
|
211
328
|
# Compute component digests at boot, so requests read them from memory
|
|
212
329
|
# instead of reading and scanning source files.
|
|
213
330
|
#
|
|
@@ -222,22 +339,176 @@ module ViewComponent
|
|
|
222
339
|
# digest, so registering midway would discard digests already warmed.
|
|
223
340
|
components.each { |component| register(component) }
|
|
224
341
|
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
342
|
+
memoizing do
|
|
343
|
+
components.each do |component|
|
|
344
|
+
component.__vc_cache_digest_source = Resolver.instance.source_for(component).freeze
|
|
345
|
+
end
|
|
228
346
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
347
|
+
# Warms the same digest cache requests read, since a lookup context's
|
|
348
|
+
# cache is shared by every context with the same details.
|
|
349
|
+
components.each do |component|
|
|
350
|
+
component.__vc_template_formats.each do |format|
|
|
351
|
+
finder = ActionView::LookupContext.new(ActionController::Base.view_paths, formats: [format])
|
|
352
|
+
component.cache_digest(finder: finder, format: format)
|
|
353
|
+
end
|
|
235
354
|
end
|
|
236
355
|
end
|
|
237
356
|
end
|
|
238
357
|
|
|
239
358
|
private
|
|
240
359
|
|
|
360
|
+
# Component references and receiverless render calls, split into those
|
|
361
|
+
# that render a component directly and the rest.
|
|
362
|
+
#
|
|
363
|
+
# @return [Array(Array<Class>, Array<Prism::CallNode>, Array<Prism::CallNode>)]
|
|
364
|
+
def partition_render_calls(program, nesting = [])
|
|
365
|
+
names = []
|
|
366
|
+
component_calls = []
|
|
367
|
+
other_calls = []
|
|
368
|
+
|
|
369
|
+
each_node(program, nesting) do |node, scope|
|
|
370
|
+
if node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)
|
|
371
|
+
name = node.slice
|
|
372
|
+
names << [name, scope] if CONSTANT_NAME.match?(name)
|
|
373
|
+
elsif render_call?(node)
|
|
374
|
+
if rendered_component(node, scope)
|
|
375
|
+
component_calls << node
|
|
376
|
+
else
|
|
377
|
+
other_calls << node
|
|
378
|
+
end
|
|
379
|
+
end
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
components = names.uniq.filter_map { |constant_name, scope| constantize_component(constant_name, scope) }
|
|
383
|
+
[components, component_calls, other_calls]
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
def render_call?(node)
|
|
387
|
+
node.is_a?(Prism::CallNode) && node.receiver.nil? && RENDER_METHODS.include?(node.name)
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
# Class/module bodies add one lexical scope, even for qualified names:
|
|
391
|
+
# `class UI::Card` adds Card, not UI, to Module.nesting.
|
|
392
|
+
def each_node(program, nesting = [])
|
|
393
|
+
# Keep scopes and nodes in one stack rather than allocating a pair per node.
|
|
394
|
+
nodes = [nesting, program]
|
|
395
|
+
while (node = nodes.pop)
|
|
396
|
+
scope = nodes.pop
|
|
397
|
+
yield node, scope
|
|
398
|
+
|
|
399
|
+
body = nil
|
|
400
|
+
body_scope = scope
|
|
401
|
+
if node.is_a?(Prism::ClassNode) || node.is_a?(Prism::ModuleNode)
|
|
402
|
+
body = node.body
|
|
403
|
+
namespace = resolve_constant(node.constant_path.slice, scope)
|
|
404
|
+
body_scope = [namespace, *scope] if namespace.is_a?(Module)
|
|
405
|
+
end
|
|
406
|
+
|
|
407
|
+
node.compact_child_nodes.reverse_each do |child|
|
|
408
|
+
nodes.push(child.equal?(body) ? body_scope : scope, child)
|
|
409
|
+
end
|
|
410
|
+
end
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
# The component a render call renders, from the constant at the root of
|
|
414
|
+
# its first argument: `FooComponent`, `FooComponent.new(...)`,
|
|
415
|
+
# `FooComponent.with_collection(...)`, and so on.
|
|
416
|
+
#
|
|
417
|
+
# @return [Class, nil]
|
|
418
|
+
def rendered_component(call, nesting = [])
|
|
419
|
+
node = call.arguments&.arguments&.first
|
|
420
|
+
node = node.receiver while node.is_a?(Prism::CallNode) && node.receiver
|
|
421
|
+
|
|
422
|
+
case node
|
|
423
|
+
when Prism::ConstantReadNode, Prism::ConstantPathNode
|
|
424
|
+
constantize_component(node.slice, nesting)
|
|
425
|
+
end
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
def component_paths(components)
|
|
429
|
+
components.uniq.filter_map { |component| registered_path_for(component) }
|
|
430
|
+
end
|
|
431
|
+
|
|
432
|
+
# The component a slot renders, resolving a class name the way the slot
|
|
433
|
+
# does when it's set. A name that doesn't resolve is left to fail there.
|
|
434
|
+
def slot_component(component, slot)
|
|
435
|
+
renderable = slot[:renderable]
|
|
436
|
+
name = slot[:renderable_class_name]
|
|
437
|
+
renderable ||= component.const_get(name) if name && component.const_defined?(name)
|
|
438
|
+
|
|
439
|
+
renderable if renderable.is_a?(Class) && renderable < ViewComponent::Base
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
# Partials Action View's render parser finds in the given calls,
|
|
443
|
+
# normalized to the form its trackers report. The calls are parsed
|
|
444
|
+
# together, so the parser runs once rather than once per call. Blocks are
|
|
445
|
+
# left out, as any renders inside them are calls of their own.
|
|
446
|
+
def partials_rendered_by(calls, name)
|
|
447
|
+
return [] if calls.empty?
|
|
448
|
+
|
|
449
|
+
source = calls.map { |call| "#{call.name}(#{call.arguments&.slice})" }.join("\n")
|
|
450
|
+
partial_paths_in(source, name)
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
def partial_paths_in(source, name)
|
|
454
|
+
RENDER_PARSER.new(name, source).render_calls
|
|
455
|
+
.map { |path| path.gsub(%r{/_}, "/") }
|
|
456
|
+
.uniq
|
|
457
|
+
end
|
|
458
|
+
|
|
459
|
+
# Dynamic renders can return components without naming their classes,
|
|
460
|
+
# such as `render UI.button`. Keep Action View's guess only if it resolves,
|
|
461
|
+
# without executing application methods to discover what they return.
|
|
462
|
+
# Interpolated paths are discarded if Rails cannot expand their wildcards.
|
|
463
|
+
def unresolved_partials_rendered_by(calls, name, view_paths)
|
|
464
|
+
targets = calls.flat_map { |call| render_targets(call) }
|
|
465
|
+
literal, dynamic = targets.partition do |target|
|
|
466
|
+
target.is_a?(Prism::StringNode) || target.is_a?(Prism::InterpolatedStringNode)
|
|
467
|
+
end
|
|
468
|
+
interpolated = literal.grep(Prism::InterpolatedStringNode)
|
|
469
|
+
unresolved = []
|
|
470
|
+
if interpolated.any?
|
|
471
|
+
paths = partial_paths_in(interpolated.map { |target| "render(#{target.slice})" }.join("\n"), name)
|
|
472
|
+
# Rails expands only directory wildcards with a literal prefix.
|
|
473
|
+
# Older parsers can return truncated paths without any wildcard.
|
|
474
|
+
unresolved = paths.reject { |path| path.match?(%r{\A[^*]+/\*\z}) }
|
|
475
|
+
static = literal.grep(Prism::StringNode)
|
|
476
|
+
unresolved -= partial_paths_in(static.map { |target| "render(#{target.slice})" }.join("\n"), name)
|
|
477
|
+
end
|
|
478
|
+
return unresolved unless view_paths && dynamic.any?
|
|
479
|
+
|
|
480
|
+
inferred = partial_paths_in(dynamic.map { |target| "render(#{target.slice})" }.join("\n"), name)
|
|
481
|
+
declared = partial_paths_in(literal.map { |target| "render(#{target.slice})" }.join("\n"), name)
|
|
482
|
+
finder = ActionView::LookupContext.new(view_paths, variants: :any, locale: I18n.available_locales)
|
|
483
|
+
|
|
484
|
+
unresolved | inferred.reject do |path|
|
|
485
|
+
declared.include?(path) || finder.exists?(path, [], true)
|
|
486
|
+
end
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
def render_targets(call)
|
|
490
|
+
arguments = call.arguments&.arguments || []
|
|
491
|
+
targets = arguments.flat_map do |argument|
|
|
492
|
+
if argument.is_a?(Prism::KeywordHashNode) || argument.is_a?(Prism::HashNode)
|
|
493
|
+
argument.elements.filter_map do |element|
|
|
494
|
+
next unless element.is_a?(Prism::AssocNode) && element.key.is_a?(Prism::SymbolNode)
|
|
495
|
+
next unless %w[partial template layout spacer_template].include?(element.key.unescaped)
|
|
496
|
+
|
|
497
|
+
element.value
|
|
498
|
+
end
|
|
499
|
+
elsif argument.equal?(arguments.first)
|
|
500
|
+
argument
|
|
501
|
+
end
|
|
502
|
+
end.compact
|
|
503
|
+
|
|
504
|
+
targets.map do |target|
|
|
505
|
+
while target.is_a?(Prism::ParenthesesNode) && target.body.is_a?(Prism::StatementsNode) && target.body.body.one?
|
|
506
|
+
target = target.body.body.first
|
|
507
|
+
end
|
|
508
|
+
target
|
|
509
|
+
end
|
|
510
|
+
end
|
|
511
|
+
|
|
241
512
|
# Record the actual class name rather than trying to reverse a virtual
|
|
242
513
|
# path, which may contain acronyms or be overridden by the component.
|
|
243
514
|
def registered_path_for(component)
|
|
@@ -259,12 +530,31 @@ module ViewComponent
|
|
|
259
530
|
# Returns nil for anything else, including constants that don't exist.
|
|
260
531
|
# Autoloading here is safe: the template is about to render this constant
|
|
261
532
|
# anyway.
|
|
262
|
-
def constantize_component(constant_name)
|
|
263
|
-
component = constant_name
|
|
533
|
+
def constantize_component(constant_name, nesting = [])
|
|
534
|
+
component = resolve_constant(constant_name, nesting)
|
|
264
535
|
return unless component.is_a?(Class) && component < ViewComponent::Base
|
|
265
536
|
|
|
266
537
|
component
|
|
267
538
|
end
|
|
539
|
+
|
|
540
|
+
def resolve_constant(name, nesting)
|
|
541
|
+
return unless CONSTANT_NAME.match?(name)
|
|
542
|
+
return name.safe_constantize if nesting.empty? || name.start_with?("::")
|
|
543
|
+
|
|
544
|
+
first, *rest = name.split("::")
|
|
545
|
+
# Lexical constants precede inherited constants. Once a name resolves,
|
|
546
|
+
# do not fall back to a top-level component if it names something else.
|
|
547
|
+
owner = nesting.find { |namespace| namespace.const_defined?(first, false) }
|
|
548
|
+
owner ||= nesting.first if nesting.first.const_defined?(first)
|
|
549
|
+
owner ||= Object if Object.const_defined?(first)
|
|
550
|
+
return unless owner
|
|
551
|
+
|
|
552
|
+
constant = owner.const_get(first)
|
|
553
|
+
return constant if rest.empty?
|
|
554
|
+
return unless constant.is_a?(Module) && constant.name
|
|
555
|
+
|
|
556
|
+
"#{constant.name}::#{rest.join("::")}".safe_constantize
|
|
557
|
+
end
|
|
268
558
|
end
|
|
269
559
|
|
|
270
560
|
# Resolved once at load time rather than memoized, so no class-level state
|
|
@@ -117,25 +117,32 @@ module ViewComponent
|
|
|
117
117
|
# The cache key for this rendering of the component.
|
|
118
118
|
#
|
|
119
119
|
# Combines the component's identity, its source digest, the requested
|
|
120
|
-
#
|
|
121
|
-
# `cache_on`. Override for full control.
|
|
120
|
+
# ordered formats, variants and partial lookup prefixes, the current locale,
|
|
121
|
+
# and the values declared with `cache_on`. Override for full control.
|
|
122
122
|
#
|
|
123
123
|
# @param view_context [ActionView::Base]
|
|
124
124
|
# @return [String]
|
|
125
125
|
def cache_key(view_context = nil)
|
|
126
126
|
lookup_context = view_context&.lookup_context
|
|
127
|
-
|
|
127
|
+
formats = Array(lookup_context&.formats).presence || [:html]
|
|
128
|
+
values = __vc_cache_on_values.map do |value|
|
|
129
|
+
value.nil? ? nil : ActiveSupport::Cache.expand_cache_key(value)
|
|
130
|
+
end
|
|
128
131
|
|
|
129
132
|
parts = [
|
|
130
133
|
"view_component",
|
|
131
134
|
self.class.virtual_path,
|
|
132
|
-
self.class.cache_digest(finder: lookup_context, format:
|
|
135
|
+
self.class.cache_digest(finder: lookup_context, format: formats.first),
|
|
133
136
|
# Included in its own right, not just as a digest input: components that
|
|
134
137
|
# render every format from one template have the same digest for each.
|
|
135
|
-
format
|
|
136
|
-
|
|
138
|
+
# Encode each list to keep format and variant boundaries distinct.
|
|
139
|
+
formats.to_json,
|
|
140
|
+
__vc_cache_variants(lookup_context).to_json,
|
|
141
|
+
Array(lookup_context&.prefixes).to_json,
|
|
137
142
|
I18n.locale,
|
|
138
|
-
|
|
143
|
+
# Expand records using Rails' versioned keys, then preserve the boundaries
|
|
144
|
+
# between declared values, including nils and strings containing slashes.
|
|
145
|
+
values.to_json
|
|
139
146
|
]
|
|
140
147
|
|
|
141
148
|
# Positions are significant, so nils are substituted rather than removed.
|
|
@@ -175,11 +182,6 @@ module ViewComponent
|
|
|
175
182
|
end
|
|
176
183
|
end
|
|
177
184
|
|
|
178
|
-
def __vc_render_uncached(view_context)
|
|
179
|
-
@__vc_cache_bypassed = true
|
|
180
|
-
render_in(view_context)
|
|
181
|
-
end
|
|
182
|
-
|
|
183
185
|
# Slots set by the caller via `with_*`. Checked before rendering, so slots
|
|
184
186
|
# a component fills in for itself with a `default_*` method — which resolve
|
|
185
187
|
# lazily during the render — aren't counted.
|
|
@@ -227,12 +229,8 @@ module ViewComponent
|
|
|
227
229
|
end
|
|
228
230
|
end
|
|
229
231
|
|
|
230
|
-
def
|
|
231
|
-
Array(lookup_context&.
|
|
232
|
-
end
|
|
233
|
-
|
|
234
|
-
def __vc_cache_variant(lookup_context)
|
|
235
|
-
Array(lookup_context&.variants).first
|
|
232
|
+
def __vc_cache_variants(lookup_context)
|
|
233
|
+
Array(lookup_context&.variants)
|
|
236
234
|
end
|
|
237
235
|
end
|
|
238
236
|
end
|
|
@@ -41,12 +41,12 @@ module ViewComponent
|
|
|
41
41
|
def components
|
|
42
42
|
component.__vc_validate_collection_parameter!(validate_default: true)
|
|
43
43
|
|
|
44
|
-
iterator = ActionView::PartialIteration.new(@collection.size)
|
|
45
44
|
collection_param = component.__vc_collection_parameter
|
|
46
45
|
counter_present = component.__vc_counter_argument_present?
|
|
47
46
|
counter_param = component.__vc_collection_counter_parameter if counter_present
|
|
48
47
|
iteration_present = component.__vc_iteration_argument_present?
|
|
49
48
|
iteration_param = component.__vc_collection_iteration_parameter if iteration_present
|
|
49
|
+
iterator = ActionView::PartialIteration.new(@collection.size) if counter_present || iteration_present
|
|
50
50
|
item_options = @options.dup
|
|
51
51
|
|
|
52
52
|
@collection.map do |item|
|
|
@@ -54,7 +54,7 @@ module ViewComponent
|
|
|
54
54
|
item_options[counter_param] = iterator.index if counter_present
|
|
55
55
|
item_options[iteration_param] = iterator.dup if iteration_present
|
|
56
56
|
instance = component.new(**item_options)
|
|
57
|
-
iterator
|
|
57
|
+
iterator&.iterate!
|
|
58
58
|
instance
|
|
59
59
|
end
|
|
60
60
|
end
|
|
@@ -74,16 +74,11 @@ module ViewComponent
|
|
|
74
74
|
end
|
|
75
75
|
end
|
|
76
76
|
|
|
77
|
-
#
|
|
78
|
-
# times does not reuse (and trip the single-render guard on) the spacer
|
|
79
|
-
# instance passed by the caller.
|
|
77
|
+
# Keep the caller's spacer unrendered, including its nested slot components.
|
|
80
78
|
def rendered_spacer(view_context)
|
|
81
79
|
return EMPTY_SPACER unless @spacer_component
|
|
82
80
|
|
|
83
|
-
spacer = @spacer_component.dup
|
|
84
|
-
if spacer.instance_variable_defined?(:@__vc_rendered)
|
|
85
|
-
spacer.remove_instance_variable(:@__vc_rendered)
|
|
86
|
-
end
|
|
81
|
+
spacer = @spacer_component.is_a?(Base) ? @spacer_component.__vc_dup_for_render : @spacer_component.dup
|
|
87
82
|
spacer.render_in(view_context)
|
|
88
83
|
end
|
|
89
84
|
end
|