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
data/lib/view_component/base.rb
CHANGED
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
require "action_view"
|
|
4
4
|
require "action_view/base"
|
|
5
5
|
require "view_component/collection"
|
|
6
|
+
require "view_component/cache_digest"
|
|
6
7
|
require "view_component/compile_cache"
|
|
7
8
|
require "view_component/compiler"
|
|
8
9
|
require "view_component/config"
|
|
9
10
|
require "view_component/errors"
|
|
11
|
+
require "view_component/cacheable"
|
|
10
12
|
require "view_component/inline_template"
|
|
11
13
|
require "view_component/preview"
|
|
12
14
|
require "view_component/request_details"
|
|
@@ -30,7 +32,24 @@ end
|
|
|
30
32
|
|
|
31
33
|
module ViewComponent
|
|
32
34
|
class Base
|
|
35
|
+
EMPTY_OUTPUT = "".html_safe.freeze
|
|
36
|
+
private_constant :EMPTY_OUTPUT
|
|
37
|
+
|
|
33
38
|
class << self
|
|
39
|
+
# A digest of the component's source, ancestors, and rendered dependencies.
|
|
40
|
+
#
|
|
41
|
+
# @param finder [ActionView::LookupContext, nil]
|
|
42
|
+
# @param format [Symbol]
|
|
43
|
+
#
|
|
44
|
+
# @return [String]
|
|
45
|
+
def cache_digest(finder: nil, format: :html)
|
|
46
|
+
ViewComponent::CacheDigest.digest(
|
|
47
|
+
self,
|
|
48
|
+
finder: finder,
|
|
49
|
+
format: format
|
|
50
|
+
)
|
|
51
|
+
end
|
|
52
|
+
|
|
34
53
|
delegate(*ViewComponent::Config.defaults.keys, to: :config)
|
|
35
54
|
|
|
36
55
|
# Returns the current config.
|
|
@@ -105,82 +124,14 @@ module ViewComponent
|
|
|
105
124
|
# @return [String]
|
|
106
125
|
def render_in(view_context, **, &block)
|
|
107
126
|
self.class.__vc_compile(raise_errors: true) unless self.class.__vc_compiled?
|
|
108
|
-
|
|
109
127
|
__vc_check_reused_instance!
|
|
110
|
-
__vc_reset_render_state!
|
|
111
|
-
|
|
112
|
-
@view_context = view_context
|
|
113
|
-
@old_virtual_path = view_context.instance_variable_get(:@virtual_path)
|
|
114
|
-
self.__vc_original_view_context = view_context
|
|
115
|
-
|
|
116
|
-
@output_buffer = view_context.output_buffer
|
|
117
|
-
|
|
118
|
-
@lookup_context = view_context.lookup_context
|
|
119
|
-
|
|
120
|
-
# For content_for
|
|
121
|
-
@view_flow = view_context.view_flow
|
|
122
|
-
|
|
123
|
-
# For i18n
|
|
124
|
-
@virtual_path ||= virtual_path
|
|
125
|
-
|
|
126
|
-
# Describes the inferred request constraints (locales, formats, variants)
|
|
127
|
-
@__vc_requested_details = @lookup_context.vc_requested_details
|
|
128
|
-
|
|
129
|
-
# For caching, such as #cache_if
|
|
130
|
-
@current_template = nil unless defined?(@current_template)
|
|
131
|
-
old_current_template = @current_template
|
|
132
|
-
|
|
133
|
-
if block && defined?(@__vc_content_set_by_with_content)
|
|
134
|
-
raise DuplicateContentError.new(self.class.name)
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
@__vc_content_evaluated = false
|
|
138
|
-
remove_instance_variable(:@__vc_content) if defined?(@__vc_content)
|
|
139
|
-
@__vc_render_in_block = block
|
|
140
|
-
@view_context.instance_variable_set(:@virtual_path, virtual_path)
|
|
141
128
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if render?
|
|
145
|
-
value = nil
|
|
146
|
-
|
|
147
|
-
@output_buffer.with_buffer do
|
|
148
|
-
inner_rendered_template = nil
|
|
149
|
-
around_rendered_template = around_render do
|
|
150
|
-
inner_rendered_template = render_template_for(@__vc_requested_details).to_s
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
# If `around_render` returned the same object the block yielded, the inner
|
|
154
|
-
# template's escaping is authoritative and we can trust the result. If the
|
|
155
|
-
# user replaced/wrapped the value, re-check HTML safety to prevent
|
|
156
|
-
# bypassing the escaping applied to normal `#call` return values
|
|
157
|
-
# (GHSA-97jw-64cj-jc58).
|
|
158
|
-
rendered_template = if around_rendered_template.equal?(inner_rendered_template)
|
|
159
|
-
around_rendered_template
|
|
160
|
-
else
|
|
161
|
-
__vc_safe_around_render_output(around_rendered_template)
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
# Avoid allocating new string when output_preamble and output_postamble are blank
|
|
165
|
-
value = if output_preamble.blank? && output_postamble.blank?
|
|
166
|
-
rendered_template
|
|
167
|
-
else
|
|
168
|
-
__vc_safe_output_preamble + rendered_template + __vc_safe_output_postamble
|
|
169
|
-
end
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
if ActionView::Base.annotate_rendered_view_with_filenames && current_template.inline_call? && request&.format == :html
|
|
173
|
-
identifier = defined?(Rails.root) ? self.class.identifier.sub("#{Rails.root}/", "") : self.class.identifier
|
|
174
|
-
value = "<!-- BEGIN #{identifier} -->".html_safe + value + "<!-- END #{identifier} -->".html_safe
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
value
|
|
129
|
+
if self.class.__vc_cache_output
|
|
130
|
+
__vc_render_cached(view_context, block)
|
|
178
131
|
else
|
|
179
|
-
|
|
132
|
+
__vc_render_uncached(view_context, &block)
|
|
180
133
|
end
|
|
181
134
|
ensure
|
|
182
|
-
view_context.instance_variable_set(:@virtual_path, @old_virtual_path)
|
|
183
|
-
@current_template = old_current_template
|
|
184
135
|
@__vc_rendered = true
|
|
185
136
|
end
|
|
186
137
|
|
|
@@ -212,14 +163,21 @@ module ViewComponent
|
|
|
212
163
|
# When rendering the parent inside an .erb template, use `#render_parent` instead.
|
|
213
164
|
def render_parent_to_string
|
|
214
165
|
@__vc_parent_render_level ||= 0 # ensure a good starting value
|
|
166
|
+
old_current_template = @current_template
|
|
215
167
|
|
|
216
168
|
begin
|
|
217
169
|
target_render = self.class.instance_variable_get(:@__vc_ancestor_calls)[@__vc_parent_render_level]
|
|
218
170
|
@__vc_parent_render_level += 1
|
|
219
171
|
|
|
172
|
+
unless target_render.owner.__vc_compiled?
|
|
173
|
+
target_render.owner.__vc_compile(raise_errors: true)
|
|
174
|
+
target_render = target_render.owner.instance_method(:render_template_for)
|
|
175
|
+
end
|
|
176
|
+
|
|
220
177
|
target_render.bind_call(self, @__vc_requested_details)
|
|
221
178
|
ensure
|
|
222
179
|
@__vc_parent_render_level -= 1
|
|
180
|
+
@current_template = old_current_template
|
|
223
181
|
end
|
|
224
182
|
end
|
|
225
183
|
|
|
@@ -227,14 +185,14 @@ module ViewComponent
|
|
|
227
185
|
#
|
|
228
186
|
# @return [String]
|
|
229
187
|
def output_preamble
|
|
230
|
-
|
|
188
|
+
EMPTY_OUTPUT
|
|
231
189
|
end
|
|
232
190
|
|
|
233
191
|
# Optional content to be returned after the rendered template.
|
|
234
192
|
#
|
|
235
193
|
# @return [String]
|
|
236
194
|
def output_postamble
|
|
237
|
-
|
|
195
|
+
EMPTY_OUTPUT
|
|
238
196
|
end
|
|
239
197
|
|
|
240
198
|
# Called before rendering the component. Override to perform operations that
|
|
@@ -269,7 +227,7 @@ module ViewComponent
|
|
|
269
227
|
#
|
|
270
228
|
# @private
|
|
271
229
|
def render(options = {}, args = {}, &block)
|
|
272
|
-
if options.respond_to?(:set_original_view_context)
|
|
230
|
+
if options.is_a?(ViewComponent::Base) || options.respond_to?(:set_original_view_context)
|
|
273
231
|
options.set_original_view_context(__vc_original_view_context)
|
|
274
232
|
|
|
275
233
|
# We assume options is a component, so there's no need to evaluate the
|
|
@@ -423,10 +381,104 @@ module ViewComponent
|
|
|
423
381
|
@view_context.instance_variable_set(:@virtual_path, old_virtual_path)
|
|
424
382
|
end
|
|
425
383
|
|
|
384
|
+
# Use the component's source digest for fragments inside its own template.
|
|
385
|
+
#
|
|
386
|
+
# @private
|
|
387
|
+
def digest_path_from_template(template)
|
|
388
|
+
component_path = self.class.virtual_path
|
|
389
|
+
return super unless component_path && template.is_a?(ViewComponent::Template)
|
|
390
|
+
|
|
391
|
+
digest = self.class.cache_digest(
|
|
392
|
+
finder: lookup_context,
|
|
393
|
+
format: template.format || lookup_context.formats.first || :html
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
digest.present? ? "#{template.virtual_path}:#{digest}" : template.virtual_path
|
|
397
|
+
end
|
|
398
|
+
|
|
426
399
|
private
|
|
427
400
|
|
|
428
401
|
attr_reader :view_context
|
|
429
402
|
|
|
403
|
+
def __vc_render_uncached(view_context, &block)
|
|
404
|
+
old_virtual_path = view_context.instance_variable_get(:@virtual_path)
|
|
405
|
+
__vc_reset_render_state!
|
|
406
|
+
|
|
407
|
+
@view_context = view_context
|
|
408
|
+
@old_virtual_path = old_virtual_path
|
|
409
|
+
self.__vc_original_view_context = view_context
|
|
410
|
+
|
|
411
|
+
@output_buffer = view_context.output_buffer
|
|
412
|
+
|
|
413
|
+
@lookup_context = view_context.lookup_context
|
|
414
|
+
|
|
415
|
+
# For content_for
|
|
416
|
+
@view_flow = view_context.view_flow
|
|
417
|
+
|
|
418
|
+
# For i18n
|
|
419
|
+
component_virtual_path = virtual_path
|
|
420
|
+
@virtual_path ||= component_virtual_path
|
|
421
|
+
|
|
422
|
+
# Describes the inferred request constraints (locales, formats, variants)
|
|
423
|
+
@__vc_requested_details = @lookup_context.vc_requested_details
|
|
424
|
+
|
|
425
|
+
# For caching, such as #cache_if
|
|
426
|
+
@current_template = nil unless defined?(@current_template)
|
|
427
|
+
old_current_template = @current_template
|
|
428
|
+
|
|
429
|
+
if block && defined?(@__vc_content_set_by_with_content)
|
|
430
|
+
raise DuplicateContentError.new(self.class.name)
|
|
431
|
+
end
|
|
432
|
+
|
|
433
|
+
@__vc_content_evaluated = false
|
|
434
|
+
remove_instance_variable(:@__vc_content) if defined?(@__vc_content)
|
|
435
|
+
@__vc_render_in_block = block
|
|
436
|
+
@view_context.instance_variable_set(:@virtual_path, component_virtual_path)
|
|
437
|
+
|
|
438
|
+
before_render
|
|
439
|
+
|
|
440
|
+
if render?
|
|
441
|
+
value = nil
|
|
442
|
+
|
|
443
|
+
@output_buffer.with_buffer do
|
|
444
|
+
inner_rendered_template = nil
|
|
445
|
+
around_rendered_template = around_render do
|
|
446
|
+
inner_rendered_template = render_template_for(@__vc_requested_details).to_s
|
|
447
|
+
end
|
|
448
|
+
|
|
449
|
+
# If `around_render` returned the same object the block yielded, the inner
|
|
450
|
+
# template's escaping is authoritative and we can trust the result. If the
|
|
451
|
+
# user replaced/wrapped the value, re-check HTML safety to prevent
|
|
452
|
+
# bypassing the escaping applied to normal `#call` return values
|
|
453
|
+
# (GHSA-97jw-64cj-jc58).
|
|
454
|
+
rendered_template = if around_rendered_template.equal?(inner_rendered_template)
|
|
455
|
+
around_rendered_template
|
|
456
|
+
else
|
|
457
|
+
__vc_safe_around_render_output(around_rendered_template)
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
# Avoid allocating new string when output_preamble and output_postamble are blank
|
|
461
|
+
value = if output_preamble.blank? && output_postamble.blank?
|
|
462
|
+
rendered_template
|
|
463
|
+
else
|
|
464
|
+
__vc_safe_output_preamble + rendered_template + __vc_safe_output_postamble
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
if ActionView::Base.annotate_rendered_view_with_filenames && current_template.inline_call? && request&.format == :html
|
|
469
|
+
identifier = defined?(Rails.root) ? self.class.identifier.sub("#{Rails.root}/", "") : self.class.identifier
|
|
470
|
+
value = "<!-- BEGIN #{identifier} -->".html_safe + value + "<!-- END #{identifier} -->".html_safe
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
value
|
|
474
|
+
else
|
|
475
|
+
"".html_safe
|
|
476
|
+
end
|
|
477
|
+
ensure
|
|
478
|
+
view_context.instance_variable_set(:@virtual_path, old_virtual_path)
|
|
479
|
+
@current_template = old_current_template
|
|
480
|
+
end
|
|
481
|
+
|
|
430
482
|
def __vc_render_in_block_provided?
|
|
431
483
|
defined?(@view_context) && @view_context && @__vc_render_in_block
|
|
432
484
|
end
|
|
@@ -436,15 +488,12 @@ module ViewComponent
|
|
|
436
488
|
end
|
|
437
489
|
|
|
438
490
|
def __vc_maybe_escape_html(text)
|
|
491
|
+
return text if text.html_safe?
|
|
439
492
|
return text if @current_template && !@current_template.html?
|
|
440
493
|
return text if text.blank?
|
|
441
494
|
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
else
|
|
445
|
-
yield
|
|
446
|
-
html_escape(text)
|
|
447
|
-
end
|
|
495
|
+
yield
|
|
496
|
+
html_escape(text)
|
|
448
497
|
end
|
|
449
498
|
|
|
450
499
|
def __vc_safe_output_preamble
|
|
@@ -473,16 +522,10 @@ module ViewComponent
|
|
|
473
522
|
# slot setters or `with_content`); reuse of an instance that has slot or
|
|
474
523
|
# `with_content` state is guarded against separately by
|
|
475
524
|
# `__vc_check_reused_instance!`.
|
|
476
|
-
RENDER_STATE_IVARS = %i[
|
|
477
|
-
@__vc_controller
|
|
478
|
-
@__vc_helpers
|
|
479
|
-
@__vc_request
|
|
480
|
-
].freeze
|
|
481
|
-
|
|
482
525
|
def __vc_reset_render_state!
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
526
|
+
remove_instance_variable(:@__vc_controller) if defined?(@__vc_controller)
|
|
527
|
+
remove_instance_variable(:@__vc_helpers) if defined?(@__vc_helpers)
|
|
528
|
+
remove_instance_variable(:@__vc_request) if defined?(@__vc_request)
|
|
486
529
|
end
|
|
487
530
|
|
|
488
531
|
# Raises when a component instance is rendered more than once.
|
|
@@ -589,7 +632,10 @@ module ViewComponent
|
|
|
589
632
|
return [] unless identifier
|
|
590
633
|
return [] unless name
|
|
591
634
|
|
|
592
|
-
|
|
635
|
+
# One glob per directory, filtered by extension, rather than a brace
|
|
636
|
+
# pattern per extension: Dir expands braces into one directory listing
|
|
637
|
+
# per extension.
|
|
638
|
+
extensions = extensions.map(&:to_s)
|
|
593
639
|
|
|
594
640
|
# view files in a directory named like the component
|
|
595
641
|
directory = File.dirname(identifier)
|
|
@@ -608,19 +654,29 @@ module ViewComponent
|
|
|
608
654
|
# Without this, `MyOtherComponent` will not look for `my_component/my_other_component.html.erb`
|
|
609
655
|
nested_component_files =
|
|
610
656
|
if name.include?("::") && component_name != filename
|
|
611
|
-
|
|
657
|
+
__vc_files_ending_with(extensions, "#{directory}/#{filename}/#{component_name}.*")
|
|
612
658
|
else
|
|
613
659
|
[]
|
|
614
660
|
end
|
|
615
661
|
|
|
616
662
|
# view files in the same directory as the component
|
|
617
|
-
sidecar_files =
|
|
663
|
+
sidecar_files = __vc_files_ending_with(extensions, "#{directory}/#{component_name}.*")
|
|
618
664
|
|
|
619
|
-
sidecar_directory_files =
|
|
665
|
+
sidecar_directory_files = __vc_files_ending_with(extensions, "#{directory}/#{component_name}/#{filename}.*")
|
|
620
666
|
|
|
621
667
|
(sidecar_files - [identifier] + sidecar_directory_files + nested_component_files).uniq
|
|
622
668
|
end
|
|
623
669
|
|
|
670
|
+
# `sidecar_files`, looked up once per component during eager loading or a
|
|
671
|
+
# digest computation: every component looks up its superclasses' templates
|
|
672
|
+
# and translations too, when it compiles, loads its translations, and
|
|
673
|
+
# computes its cache digest.
|
|
674
|
+
#
|
|
675
|
+
# @private
|
|
676
|
+
def __vc_sidecar_files(extensions)
|
|
677
|
+
CompileCache.sidecar_files_for(self, extensions) { sidecar_files(extensions) }
|
|
678
|
+
end
|
|
679
|
+
|
|
624
680
|
# Render a component for each element in a collection ([documentation](/guide/collections)):
|
|
625
681
|
#
|
|
626
682
|
# ```ruby
|
|
@@ -668,6 +724,7 @@ module ViewComponent
|
|
|
668
724
|
# owner is included in a prefix like `ApplicationComponent.inherited`.
|
|
669
725
|
child.identifier = caller_locations(1, 10).reject { |l| l.base_label == "inherited" }[0].path
|
|
670
726
|
child.virtual_path = child.name&.underscore
|
|
727
|
+
ViewComponent::CacheDigest.register(child)
|
|
671
728
|
|
|
672
729
|
# Set collection parameter to the extended component
|
|
673
730
|
child.with_collection_parameter(__vc_provided_collection_parameter)
|
|
@@ -684,7 +741,7 @@ module ViewComponent
|
|
|
684
741
|
|
|
685
742
|
# @private
|
|
686
743
|
def __vc_compiled?
|
|
687
|
-
|
|
744
|
+
CompileCache.compiled?(self)
|
|
688
745
|
end
|
|
689
746
|
|
|
690
747
|
# Hook called by the compiler after a component is compiled.
|
|
@@ -707,6 +764,17 @@ module ViewComponent
|
|
|
707
764
|
@__vc_compiler ||= Compiler.new(self)
|
|
708
765
|
end
|
|
709
766
|
|
|
767
|
+
# Synthetic digest source computed at boot. Not inherited, since each
|
|
768
|
+
# component's source differs.
|
|
769
|
+
#
|
|
770
|
+
# @private
|
|
771
|
+
attr_accessor :__vc_cache_digest_source
|
|
772
|
+
|
|
773
|
+
# @private
|
|
774
|
+
def __vc_template_formats
|
|
775
|
+
__vc_compiler.template_formats
|
|
776
|
+
end
|
|
777
|
+
|
|
710
778
|
# Set the parameter name used when rendering elements of a collection ([documentation](/guide/collections)):
|
|
711
779
|
#
|
|
712
780
|
# ```ruby
|
|
@@ -854,9 +922,16 @@ module ViewComponent
|
|
|
854
922
|
signatures
|
|
855
923
|
end
|
|
856
924
|
|
|
925
|
+
def __vc_files_ending_with(extensions, pattern)
|
|
926
|
+
paths = CompileCache.sidecar_files_matching(pattern)
|
|
927
|
+
extensions.flat_map { |extension| paths.select { |path| path.end_with?(extension) } }
|
|
928
|
+
end
|
|
929
|
+
|
|
857
930
|
attr_reader :__vc_provided_collection_parameter
|
|
858
931
|
end
|
|
859
932
|
|
|
933
|
+
include ViewComponent::Cacheable
|
|
934
|
+
|
|
860
935
|
ActiveSupport.run_load_hooks(:view_component, self)
|
|
861
936
|
end
|
|
862
937
|
end
|
|
@@ -14,19 +14,51 @@ module ViewComponent
|
|
|
14
14
|
#
|
|
15
15
|
# @private
|
|
16
16
|
module DependencyTracking
|
|
17
|
+
# Templates are memoized during a digest, so a template reached along
|
|
18
|
+
# many edges of the tree is scanned once.
|
|
17
19
|
def find_dependencies(name, template, view_paths = nil)
|
|
18
|
-
dependencies
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
20
|
+
CacheDigest.memoize([:dependencies, name, template, view_paths]) do
|
|
21
|
+
dependencies = super
|
|
22
|
+
source = template.source
|
|
23
|
+
|
|
24
|
+
# Synthetic templates already declare every dependency found in their
|
|
25
|
+
# sources, as paths.
|
|
26
|
+
next dependencies if name.start_with?("#{CacheDigest::VIRTUAL_PATH_PREFIX}/")
|
|
27
|
+
|
|
28
|
+
# `# Template Dependency: SomeComponent` names a class, which Rails
|
|
29
|
+
# would resolve as a template path and never find. Swap it for the path
|
|
30
|
+
# the component is digested under, so the declaration resolves instead
|
|
31
|
+
# of becoming a missing node.
|
|
32
|
+
CacheDigest.explicit_component_dependencies(source).each do |declared, virtual_path|
|
|
33
|
+
dependencies = dependencies - [declared] + [virtual_path]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
next dependencies unless CacheDigest.template_dependencies?(source, template.handler)
|
|
28
37
|
|
|
29
|
-
|
|
38
|
+
# ERB's regex tracker guesses different paths than the Ruby parser,
|
|
39
|
+
# especially for namespaced components. Ask the registered tracker
|
|
40
|
+
# about isolated render calls without reentering this extension.
|
|
41
|
+
if template.handler.is_a?(ActionView::Template::Handlers::ERB)
|
|
42
|
+
tracker = lambda do |calls|
|
|
43
|
+
isolated = ActionView::Template.new(
|
|
44
|
+
calls.map { |call| "<% #{call} %>" }.join("\n"),
|
|
45
|
+
template.identifier,
|
|
46
|
+
template.handler,
|
|
47
|
+
locals: [],
|
|
48
|
+
virtual_path: name
|
|
49
|
+
)
|
|
50
|
+
super(name, isolated, view_paths)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
component_paths, inferred_partials =
|
|
55
|
+
CacheDigest.template_renders_in(CacheDigest.compiled_source(template), name, view_paths: view_paths, &tracker)
|
|
56
|
+
# Older ERB trackers omit the underscore on unqualified partial paths.
|
|
57
|
+
inferred_partials |= inferred_partials.map { |path| path.delete_prefix("_") }
|
|
58
|
+
declared = source.scan(CacheDigest::EXPLICIT_DEPENDENCY).flatten
|
|
59
|
+
|
|
60
|
+
(dependencies - (inferred_partials - declared) + component_paths).uniq
|
|
61
|
+
end
|
|
30
62
|
end
|
|
31
63
|
|
|
32
64
|
# @private
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "action_view/digestor"
|
|
4
|
+
|
|
5
|
+
module ViewComponent
|
|
6
|
+
module CacheDigest
|
|
7
|
+
# Lets `ActionView::Digestor` find the synthetic templates `Resolver` builds.
|
|
8
|
+
#
|
|
9
|
+
# Prepended to the Digestor's singleton class rather than appending
|
|
10
|
+
# `Resolver` to the application's view paths, so the resolver is only ever
|
|
11
|
+
# consulted while digesting. Code that walks `view_paths` and expects every
|
|
12
|
+
# entry to be a file system resolver is unaffected.
|
|
13
|
+
#
|
|
14
|
+
# @private
|
|
15
|
+
module DigestorLookup
|
|
16
|
+
# @private
|
|
17
|
+
def self.install!
|
|
18
|
+
digestor = ActionView::Digestor.singleton_class
|
|
19
|
+
return if digestor.include?(self)
|
|
20
|
+
|
|
21
|
+
digestor.prepend(self)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def digest(finder:, **)
|
|
25
|
+
CacheDigest.memoizing do
|
|
26
|
+
previous = CacheDigest.memo[:pruned]
|
|
27
|
+
loop do
|
|
28
|
+
pruned = CacheDigest.memo[:pruned] = {}
|
|
29
|
+
digest = super
|
|
30
|
+
|
|
31
|
+
# A component that registered while the tree was built expired the
|
|
32
|
+
# digests the pruned nodes stood in for, so this one, and the
|
|
33
|
+
# digests computed for them since, are missing their dependencies.
|
|
34
|
+
break digest if pruned.all? { |name, cached| finder.digest_cache[name].equal?(cached) }
|
|
35
|
+
|
|
36
|
+
finder.digest_cache.clear
|
|
37
|
+
end
|
|
38
|
+
ensure
|
|
39
|
+
CacheDigest.memo[:pruned] = previous
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# A dependency whose digest is cached already becomes a leaf:
|
|
44
|
+
# `Node#dependency_digest` reads the cache and never walks its children.
|
|
45
|
+
# Without this, every component digested at boot rebuilds the tree of
|
|
46
|
+
# everything it renders, which for components rendering a shared helper
|
|
47
|
+
# module is most of the application.
|
|
48
|
+
def tree(name, finder, partial = false, seen = {})
|
|
49
|
+
CompileCache.with_sidecar_files_cache do
|
|
50
|
+
pruned = CacheDigest.memo&.fetch(:pruned, nil)
|
|
51
|
+
cached = finder.digest_cache[name] if pruned && partial
|
|
52
|
+
next super unless cached
|
|
53
|
+
|
|
54
|
+
path = ActionView::TemplatePath.parse(name)
|
|
55
|
+
template = find_template(finder, path.name, [path.prefix], partial, [])
|
|
56
|
+
next super unless template
|
|
57
|
+
|
|
58
|
+
pruned[name] = cached
|
|
59
|
+
seen[template.identifier] ||= ActionView::Digestor::Node.create(name, name.gsub(%r{/_}, "/"), template, partial)
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
private
|
|
64
|
+
|
|
65
|
+
def find_template(finder, name, prefixes, partial, keys)
|
|
66
|
+
key = [:template, name, prefixes.first, partial, finder.formats, finder.view_paths]
|
|
67
|
+
template = CacheDigest.memoize(key) do
|
|
68
|
+
Resolver.instance.find_templates(
|
|
69
|
+
name, prefixes.first, partial, {formats: finder.formats}, view_paths: finder.view_paths
|
|
70
|
+
).first
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
template || super
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|