view_component 4.15.0 → 4.20.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 +124 -0
- data/lib/view_component/base.rb +136 -25
- data/lib/view_component/cache_digest/dependency_tracking.rb +9 -6
- data/lib/view_component/cache_digest/digestor_lookup.rb +31 -0
- data/lib/view_component/cache_digest/resolver.rb +111 -54
- data/lib/view_component/cache_digest.rb +233 -76
- data/lib/view_component/{experimentally_cacheable.rb → cacheable.rb} +40 -87
- data/lib/view_component/collection.rb +19 -16
- data/lib/view_component/compile_cache.rb +27 -0
- data/lib/view_component/compiler.rb +39 -14
- data/lib/view_component/engine.rb +30 -5
- data/lib/view_component/instrumentation.rb +5 -1
- data/lib/view_component/request_details.rb +10 -2
- data/lib/view_component/slotable.rb +4 -4
- data/lib/view_component/template.rb +46 -31
- data/lib/view_component/translatable.rb +8 -13
- data/lib/view_component/version.rb +1 -1
- data/lib/view_component.rb +1 -1
- metadata +11 -8
|
@@ -3,25 +3,13 @@
|
|
|
3
3
|
require "view_component/cache_digest"
|
|
4
4
|
|
|
5
5
|
module ViewComponent
|
|
6
|
-
#
|
|
6
|
+
# Caching support for ViewComponents.
|
|
7
7
|
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
# https://github.com/ViewComponent/view_component/issues/234.
|
|
11
|
-
#
|
|
12
|
-
# Including this module does two things:
|
|
13
|
-
#
|
|
14
|
-
# 1. Registers the component with Rails' template digest tree, so a
|
|
15
|
-
# `<% cache %>` block wrapping the component in a view is invalidated when
|
|
16
|
-
# the component's template, Ruby class, sidecar files, or child components
|
|
17
|
-
# change.
|
|
18
|
-
# 2. Enables the `cache_on` macro, which caches the component's own rendered
|
|
19
|
-
# output.
|
|
8
|
+
# Provides the `cache_on` macro, which caches the component's own rendered
|
|
9
|
+
# output. Components don't cache their output until they call `cache_on`.
|
|
20
10
|
#
|
|
21
11
|
# ```ruby
|
|
22
12
|
# class MessageComponent < ViewComponent::Base
|
|
23
|
-
# include ViewComponent::ExperimentallyCacheable
|
|
24
|
-
#
|
|
25
13
|
# cache_on :message
|
|
26
14
|
#
|
|
27
15
|
# def initialize(message:)
|
|
@@ -29,7 +17,7 @@ module ViewComponent
|
|
|
29
17
|
# end
|
|
30
18
|
# end
|
|
31
19
|
# ```
|
|
32
|
-
module
|
|
20
|
+
module Cacheable
|
|
33
21
|
extend ActiveSupport::Concern
|
|
34
22
|
|
|
35
23
|
# Stands in for `nil` in the cache key. Without it, `expand_cache_key`
|
|
@@ -37,11 +25,6 @@ module ViewComponent
|
|
|
37
25
|
# that respect would share an entry.
|
|
38
26
|
NIL_CACHE_VALUE = :__vc_nil
|
|
39
27
|
|
|
40
|
-
included do
|
|
41
|
-
ViewComponent::CacheDigest.install!
|
|
42
|
-
ViewComponent::CacheDigest.register(self)
|
|
43
|
-
end
|
|
44
|
-
|
|
45
28
|
class_methods do
|
|
46
29
|
# Declare the values that identify a rendering of this component.
|
|
47
30
|
#
|
|
@@ -54,8 +37,7 @@ module ViewComponent
|
|
|
54
37
|
# ```
|
|
55
38
|
#
|
|
56
39
|
# Calling `cache_on` opts the component into caching its own output.
|
|
57
|
-
#
|
|
58
|
-
# Rails' digest tree.
|
|
40
|
+
# Components that don't call it aren't cached.
|
|
59
41
|
#
|
|
60
42
|
# Pass `if:` or `unless:` to cache only some renders. Both accept a method
|
|
61
43
|
# name or a proc evaluated on the component:
|
|
@@ -118,11 +100,6 @@ module ViewComponent
|
|
|
118
100
|
superclass.__vc_cache_unless if superclass.respond_to?(:__vc_cache_unless)
|
|
119
101
|
end
|
|
120
102
|
|
|
121
|
-
# @private
|
|
122
|
-
def __vc_cacheable?
|
|
123
|
-
true
|
|
124
|
-
end
|
|
125
|
-
|
|
126
103
|
# Whether this component caches its own rendered output.
|
|
127
104
|
#
|
|
128
105
|
# @return [Boolean]
|
|
@@ -130,67 +107,11 @@ module ViewComponent
|
|
|
130
107
|
__vc_cache_on.any?
|
|
131
108
|
end
|
|
132
109
|
|
|
133
|
-
#
|
|
134
|
-
#
|
|
135
|
-
# partials it renders.
|
|
110
|
+
# `__vc_caches_output?`, stored when the component compiles so rendering
|
|
111
|
+
# reads a flag instead of computing it.
|
|
136
112
|
#
|
|
137
|
-
# Computed with Rails' own `ActionView::Digestor`, so it's the same digest
|
|
138
|
-
# used to invalidate `<% cache %>` blocks.
|
|
139
|
-
#
|
|
140
|
-
# Usable outside a request, where no view context exists:
|
|
141
|
-
#
|
|
142
|
-
# ```ruby
|
|
143
|
-
# MessageComponent.cache_digest
|
|
144
|
-
# ```
|
|
145
|
-
#
|
|
146
|
-
# @param finder [ActionView::LookupContext] Defaults to a lookup context
|
|
147
|
-
# built from `ActionController::Base.view_paths`.
|
|
148
|
-
# @param format [Symbol]
|
|
149
|
-
# @return [String]
|
|
150
|
-
def cache_digest(finder: nil, format: :html)
|
|
151
|
-
ViewComponent::CacheDigest.digest(
|
|
152
|
-
self,
|
|
153
|
-
finder: finder || ViewComponent::CacheDigest.default_finder,
|
|
154
|
-
format: format
|
|
155
|
-
)
|
|
156
|
-
end
|
|
157
|
-
|
|
158
113
|
# @private
|
|
159
|
-
|
|
160
|
-
super
|
|
161
|
-
ViewComponent::CacheDigest.register(child)
|
|
162
|
-
end
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
# Renders the component, reading from and writing to the Rails cache when
|
|
166
|
-
# `cache_on` has been declared.
|
|
167
|
-
#
|
|
168
|
-
# @private
|
|
169
|
-
def render_in(view_context, **, &block)
|
|
170
|
-
return super unless self.class.__vc_caches_output?
|
|
171
|
-
|
|
172
|
-
# Content provided by the caller isn't part of the cache key, so caching
|
|
173
|
-
# it would serve one caller's content to another. Raised whether or not
|
|
174
|
-
# caching is currently enabled, so the conflict surfaces in development
|
|
175
|
-
# and test rather than only in production.
|
|
176
|
-
if block || __vc_content_set_by_with_content_defined? || __vc_slots_set_by_caller?
|
|
177
|
-
raise ContentPassedToCachedComponentError.new(self.class.name)
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
return super unless __vc_cache_enabled?(view_context)
|
|
181
|
-
|
|
182
|
-
store = Rails.cache
|
|
183
|
-
key = cache_key(view_context)
|
|
184
|
-
|
|
185
|
-
if (cached = store.read(key))
|
|
186
|
-
# Safe to mark as HTML-safe: the cached string was produced by this same
|
|
187
|
-
# rendering pipeline, which escapes output before it's written.
|
|
188
|
-
return cached.html_safe # rubocop:disable Rails/OutputSafety
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
super.tap do |output|
|
|
192
|
-
store.write(key, output.to_s)
|
|
193
|
-
end
|
|
114
|
+
attr_accessor :__vc_cache_output
|
|
194
115
|
end
|
|
195
116
|
|
|
196
117
|
# The cache key for this rendering of the component.
|
|
@@ -227,6 +148,38 @@ module ViewComponent
|
|
|
227
148
|
|
|
228
149
|
private
|
|
229
150
|
|
|
151
|
+
# Renders through the Rails cache. Called by `Base#render_in` for
|
|
152
|
+
# components that declare `cache_on`.
|
|
153
|
+
def __vc_render_cached(view_context, content_block)
|
|
154
|
+
# Content provided by the caller isn't part of the cache key, so caching
|
|
155
|
+
# it would serve one caller's content to another. Raised whether or not
|
|
156
|
+
# caching is currently enabled, so the conflict surfaces in development
|
|
157
|
+
# and test rather than only in production.
|
|
158
|
+
if content_block || __vc_content_set_by_with_content_defined? || __vc_slots_set_by_caller?
|
|
159
|
+
raise ContentPassedToCachedComponentError.new(self.class.name)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
return __vc_render_uncached(view_context) unless __vc_cache_enabled?(view_context)
|
|
163
|
+
|
|
164
|
+
store = Rails.cache
|
|
165
|
+
key = cache_key(view_context)
|
|
166
|
+
|
|
167
|
+
if (cached = store.read(key))
|
|
168
|
+
# Safe to mark as HTML-safe: the cached string was produced by this same
|
|
169
|
+
# rendering pipeline, which escapes output before it's written.
|
|
170
|
+
return cached.html_safe
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
__vc_render_uncached(view_context).tap do |output|
|
|
174
|
+
store.write(key, output.to_s)
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def __vc_render_uncached(view_context)
|
|
179
|
+
@__vc_cache_bypassed = true
|
|
180
|
+
render_in(view_context)
|
|
181
|
+
end
|
|
182
|
+
|
|
230
183
|
# Slots set by the caller via `with_*`. Checked before rendering, so slots
|
|
231
184
|
# a component fills in for itself with a `default_*` method — which resolve
|
|
232
185
|
# lazily during the render — aren't counted.
|
|
@@ -12,6 +12,9 @@ module ViewComponent
|
|
|
12
12
|
|
|
13
13
|
delegate :size, to: :@collection
|
|
14
14
|
|
|
15
|
+
EMPTY_SPACER = "".html_safe.freeze
|
|
16
|
+
private_constant :EMPTY_SPACER
|
|
17
|
+
|
|
15
18
|
def render_in(view_context, **_, &block)
|
|
16
19
|
rendered = components.map! do |component|
|
|
17
20
|
component.render_in(view_context, &block)
|
|
@@ -36,18 +39,27 @@ module ViewComponent
|
|
|
36
39
|
# Always rebuild child component instances per render to avoid leaking
|
|
37
40
|
# request-scoped state from a previous render into a later one (GHSA).
|
|
38
41
|
def components
|
|
39
|
-
iterator = ActionView::PartialIteration.new(@collection.size)
|
|
40
|
-
|
|
41
42
|
component.__vc_validate_collection_parameter!(validate_default: true)
|
|
42
43
|
|
|
44
|
+
iterator = ActionView::PartialIteration.new(@collection.size)
|
|
45
|
+
collection_param = component.__vc_collection_parameter
|
|
46
|
+
counter_present = component.__vc_counter_argument_present?
|
|
47
|
+
counter_param = component.__vc_collection_counter_parameter if counter_present
|
|
48
|
+
iteration_present = component.__vc_iteration_argument_present?
|
|
49
|
+
iteration_param = component.__vc_collection_iteration_parameter if iteration_present
|
|
50
|
+
item_options = @options.dup
|
|
51
|
+
|
|
43
52
|
@collection.map do |item|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
53
|
+
item_options[collection_param] = item
|
|
54
|
+
item_options[counter_param] = iterator.index if counter_present
|
|
55
|
+
item_options[iteration_param] = iterator.dup if iteration_present
|
|
56
|
+
instance = component.new(**item_options)
|
|
57
|
+
iterator.iterate!
|
|
58
|
+
instance
|
|
47
59
|
end
|
|
48
60
|
end
|
|
49
61
|
|
|
50
|
-
def initialize(component, object, spacer_component,
|
|
62
|
+
def initialize(component, object, spacer_component, options = {})
|
|
51
63
|
@component = component
|
|
52
64
|
@collection = collection_variable(object || [])
|
|
53
65
|
@spacer_component = spacer_component
|
|
@@ -62,20 +74,11 @@ module ViewComponent
|
|
|
62
74
|
end
|
|
63
75
|
end
|
|
64
76
|
|
|
65
|
-
def component_options(item, iterator)
|
|
66
|
-
item_options = @options.dup
|
|
67
|
-
item_options[component.__vc_collection_parameter] = item
|
|
68
|
-
item_options[component.__vc_collection_counter_parameter] = iterator.index if component.__vc_counter_argument_present?
|
|
69
|
-
item_options[component.__vc_collection_iteration_parameter] = iterator.dup if component.__vc_iteration_argument_present?
|
|
70
|
-
|
|
71
|
-
item_options
|
|
72
|
-
end
|
|
73
|
-
|
|
74
77
|
# Render the spacer through a fresh `dup` so a collection rendered multiple
|
|
75
78
|
# times does not reuse (and trip the single-render guard on) the spacer
|
|
76
79
|
# instance passed by the caller.
|
|
77
80
|
def rendered_spacer(view_context)
|
|
78
|
-
return
|
|
81
|
+
return EMPTY_SPACER unless @spacer_component
|
|
79
82
|
|
|
80
83
|
spacer = @spacer_component.dup
|
|
81
84
|
if spacer.instance_variable_defined?(:@__vc_rendered)
|
|
@@ -25,5 +25,32 @@ module ViewComponent
|
|
|
25
25
|
def invalidate!
|
|
26
26
|
cache.clear
|
|
27
27
|
end
|
|
28
|
+
|
|
29
|
+
# Sidecar files can't change while the application eager loads: within the
|
|
30
|
+
# block, each component's are looked up once. Fiber storage rather than
|
|
31
|
+
# module state, so the cache is gone once boot is done.
|
|
32
|
+
def with_sidecar_files_cache
|
|
33
|
+
Fiber[:__vc_sidecar_files] = {}
|
|
34
|
+
yield
|
|
35
|
+
ensure
|
|
36
|
+
Fiber[:__vc_sidecar_files] = nil
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def sidecar_files_for(klass, extensions)
|
|
40
|
+
sidecar_files_by_component = Fiber[:__vc_sidecar_files]
|
|
41
|
+
return yield unless sidecar_files_by_component
|
|
42
|
+
|
|
43
|
+
sidecar_files = sidecar_files_by_component[klass] ||= {}
|
|
44
|
+
sidecar_files[extensions] ||= yield.freeze
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Invalidates the cache when a template or a translation file is added,
|
|
48
|
+
# changed, or removed. Changes to Ruby files reload the application instead.
|
|
49
|
+
def template_watcher(file_watcher)
|
|
50
|
+
extensions = ActionView::Template::Handlers.extensions.map(&:to_s) + %w[yml yaml]
|
|
51
|
+
directories = ActiveSupport::Dependencies.autoload_paths.map(&:to_s).index_with(extensions)
|
|
52
|
+
|
|
53
|
+
file_watcher.new([], directories) { invalidate! }
|
|
54
|
+
end
|
|
28
55
|
end
|
|
29
56
|
end
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "concurrent-ruby"
|
|
4
|
-
|
|
5
3
|
module ViewComponent
|
|
6
4
|
class Compiler
|
|
7
5
|
# Compiler development mode. Can be either:
|
|
@@ -41,6 +39,8 @@ module ViewComponent
|
|
|
41
39
|
return false
|
|
42
40
|
end
|
|
43
41
|
|
|
42
|
+
@component.__vc_build_collection_arguments
|
|
43
|
+
|
|
44
44
|
if raise_errors
|
|
45
45
|
@component.__vc_validate_initialization_parameters!
|
|
46
46
|
@component.__vc_validate_collection_parameter!
|
|
@@ -48,6 +48,8 @@ module ViewComponent
|
|
|
48
48
|
|
|
49
49
|
define_render_template_for
|
|
50
50
|
|
|
51
|
+
@component.__vc_cache_output = @component.__vc_caches_output?
|
|
52
|
+
|
|
51
53
|
@component.__vc_register_default_slots
|
|
52
54
|
@component.__vc_build_i18n_backend
|
|
53
55
|
|
|
@@ -74,6 +76,14 @@ module ViewComponent
|
|
|
74
76
|
filtered_templates
|
|
75
77
|
end
|
|
76
78
|
|
|
79
|
+
# Formats the component has templates for. Inline templates and `#call`
|
|
80
|
+
# methods have no format of their own and render as HTML.
|
|
81
|
+
#
|
|
82
|
+
# @return [Array<Symbol>]
|
|
83
|
+
def template_formats
|
|
84
|
+
gather_templates.map { |template| template.format || :html }.uniq
|
|
85
|
+
end
|
|
86
|
+
|
|
77
87
|
private
|
|
78
88
|
|
|
79
89
|
attr_reader :templates
|
|
@@ -85,23 +95,38 @@ module ViewComponent
|
|
|
85
95
|
|
|
86
96
|
@component.silence_redefinition_of_method(:render_template_for)
|
|
87
97
|
|
|
98
|
+
# Keep template references local to this method's owner, including when
|
|
99
|
+
# render_parent invokes it on an instance of a descendant component.
|
|
100
|
+
methods = Module.new
|
|
101
|
+
@templates.each_with_index { |template, index| methods.const_set(:"TEMPLATE_#{index}", template) }
|
|
102
|
+
|
|
88
103
|
if @templates.one?
|
|
89
104
|
template = @templates.first
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
end
|
|
105
|
+
body = <<~RUBY
|
|
106
|
+
@current_template = TEMPLATE_0
|
|
107
|
+
#{template.safe_method_call}
|
|
108
|
+
RUBY
|
|
95
109
|
else
|
|
96
|
-
|
|
97
|
-
@
|
|
98
|
-
|
|
99
|
-
|
|
110
|
+
methods.const_set(:COMPILER, self)
|
|
111
|
+
calls = @templates.each_with_index.map do |template, index|
|
|
112
|
+
"when TEMPLATE_#{index}\n#{template.safe_method_call}"
|
|
113
|
+
end.join("\n")
|
|
114
|
+
body = <<~RUBY
|
|
115
|
+
@current_template = COMPILER.find_templates_for(details).first
|
|
116
|
+
case @current_template
|
|
117
|
+
#{calls}
|
|
100
118
|
else
|
|
101
|
-
raise MissingTemplateError.new(self.class.name, details)
|
|
119
|
+
raise ViewComponent::MissingTemplateError.new(self.class.name, details)
|
|
102
120
|
end
|
|
103
|
-
|
|
121
|
+
RUBY
|
|
104
122
|
end
|
|
123
|
+
|
|
124
|
+
methods.module_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
125
|
+
def render_template_for(details)
|
|
126
|
+
#{body}
|
|
127
|
+
end
|
|
128
|
+
RUBY
|
|
129
|
+
@component.define_method(:render_template_for, methods.instance_method(:render_template_for))
|
|
105
130
|
end
|
|
106
131
|
|
|
107
132
|
def template_errors
|
|
@@ -176,7 +201,7 @@ module ViewComponent
|
|
|
176
201
|
)]
|
|
177
202
|
else
|
|
178
203
|
path_parser = ActionView::Resolver::PathParser.new
|
|
179
|
-
templates = @component.
|
|
204
|
+
templates = @component.__vc_sidecar_files(
|
|
180
205
|
ActionView::Template::Handlers.extensions
|
|
181
206
|
).map do |path|
|
|
182
207
|
details = path_parser.parse(path).details
|
|
@@ -3,11 +3,16 @@
|
|
|
3
3
|
require "rails"
|
|
4
4
|
require "view_component/config"
|
|
5
5
|
require "view_component/deprecation"
|
|
6
|
+
require "view_component/cache_digest"
|
|
6
7
|
|
|
7
8
|
module ViewComponent
|
|
8
9
|
class Engine < Rails::Engine # :nodoc:
|
|
9
10
|
config.view_component = ViewComponent::Config.current
|
|
10
11
|
|
|
12
|
+
initializer "view_component.cache_digest" do
|
|
13
|
+
ViewComponent::CacheDigest.install!
|
|
14
|
+
end
|
|
15
|
+
|
|
11
16
|
initializer "view_component.set_configs" do |app|
|
|
12
17
|
options = app.config.view_component
|
|
13
18
|
|
|
@@ -28,6 +33,7 @@ module ViewComponent
|
|
|
28
33
|
initializer "view_component.enable_instrumentation" do |app|
|
|
29
34
|
ActiveSupport.on_load(:view_component) do
|
|
30
35
|
if app.config.view_component.instrumentation_enabled.present?
|
|
36
|
+
ViewComponent::Instrumentation.enabled = true
|
|
31
37
|
ViewComponent::Base.prepend(ViewComponent::Instrumentation)
|
|
32
38
|
end
|
|
33
39
|
end
|
|
@@ -88,10 +94,16 @@ module ViewComponent
|
|
|
88
94
|
ActiveSupport.on_load(:after_initialize) do
|
|
89
95
|
if Rails.application.config.eager_load
|
|
90
96
|
ActiveSupport::Notifications.instrument("compile.view_component") do
|
|
91
|
-
|
|
92
|
-
|
|
97
|
+
CompileCache.with_sidecar_files_cache do
|
|
98
|
+
components = ViewComponent::Base.descendants.reject(&:anonymous?)
|
|
93
99
|
|
|
94
|
-
|
|
100
|
+
ViewComponent::CacheDigest.retaining_compiled_sources do
|
|
101
|
+
components.each(&:__vc_compile)
|
|
102
|
+
|
|
103
|
+
if ActionView::Base.cache_template_loading
|
|
104
|
+
ViewComponent::CacheDigest.precompute!(components.select(&:__vc_compiled?))
|
|
105
|
+
end
|
|
106
|
+
end
|
|
95
107
|
end
|
|
96
108
|
end
|
|
97
109
|
end
|
|
@@ -131,9 +143,22 @@ module ViewComponent
|
|
|
131
143
|
end
|
|
132
144
|
end
|
|
133
145
|
|
|
134
|
-
|
|
135
|
-
|
|
146
|
+
# Like Rails' own template cache expiry, only watch templates when they
|
|
147
|
+
# aren't cached at boot. With template caching on, as in production, no
|
|
148
|
+
# hook runs per request, so requests never read ActionView's caching
|
|
149
|
+
# class variable, which non-main Ractors can't access.
|
|
150
|
+
ActiveSupport.on_load(:action_view) do
|
|
151
|
+
next if ActionView::Base.cache_template_loading
|
|
152
|
+
|
|
153
|
+
template_watcher = nil
|
|
154
|
+
|
|
155
|
+
app.executor.to_run :before do
|
|
156
|
+
template_watcher ||= CompileCache.template_watcher(app.config.file_watcher)
|
|
157
|
+
template_watcher.execute_if_updated
|
|
158
|
+
end
|
|
136
159
|
end
|
|
160
|
+
|
|
161
|
+
app.reloader.before_class_unload { CompileCache.invalidate! }
|
|
137
162
|
end
|
|
138
163
|
end
|
|
139
164
|
end
|
|
@@ -8,8 +8,12 @@ module ViewComponent # :nodoc:
|
|
|
8
8
|
mod.prepend(self) unless self <= ViewComponent::Instrumentation
|
|
9
9
|
end
|
|
10
10
|
|
|
11
|
+
class << self
|
|
12
|
+
attr_accessor :enabled
|
|
13
|
+
end
|
|
14
|
+
|
|
11
15
|
def render_in(...)
|
|
12
|
-
return super
|
|
16
|
+
return super unless Instrumentation.enabled
|
|
13
17
|
|
|
14
18
|
payload = {
|
|
15
19
|
name: self.class.name,
|
|
@@ -24,8 +24,16 @@ module ViewComponent
|
|
|
24
24
|
# The hash `user_details` would normally be the standard arguments that
|
|
25
25
|
# `render` accepts, but there's currently no mechanism for users to
|
|
26
26
|
# provide these when calling render on a ViewComponent.
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
if user_details.equal?(EMPTY_DETAILS)
|
|
28
|
+
# Fast path: with template caching on, `details_key` is the
|
|
29
|
+
# Requested for the current details, memoized by ActionView and
|
|
30
|
+
# reset whenever a detail such as `formats` changes. Using it
|
|
31
|
+
# directly skips the tuple `detail_args_for` allocates.
|
|
32
|
+
details_key || ActionView::TemplateDetails::Requested.new(**@details)
|
|
33
|
+
else
|
|
34
|
+
details, from_cache = detail_args_for(user_details)
|
|
35
|
+
from_cache || ActionView::TemplateDetails::Requested.new(**details)
|
|
36
|
+
end
|
|
29
37
|
end
|
|
30
38
|
end
|
|
31
39
|
end
|
|
@@ -408,19 +408,19 @@ module ViewComponent
|
|
|
408
408
|
self.class.const_get(slot_definition[:renderable_class_name]).new(*args, **kwargs)
|
|
409
409
|
# If passed a lambda
|
|
410
410
|
elsif slot_definition[:renderable_function]
|
|
411
|
-
# Use `
|
|
411
|
+
# Use `bind_call(self)` to ensure lambda is executed in the context of the
|
|
412
412
|
# current component. This is necessary to allow the lambda to access helper
|
|
413
413
|
# methods like `content_tag` as well as parent component state.
|
|
414
|
-
renderable_function = slot_definition[:renderable_function]
|
|
414
|
+
renderable_function = slot_definition[:renderable_function]
|
|
415
415
|
renderable_value =
|
|
416
416
|
if block
|
|
417
|
-
renderable_function.
|
|
417
|
+
renderable_function.bind_call(self, *args, **kwargs) do |*rargs|
|
|
418
418
|
with_captured_virtual_path(captured_block_virtual_path) do
|
|
419
419
|
view_context.capture(*rargs, &block)
|
|
420
420
|
end
|
|
421
421
|
end
|
|
422
422
|
else
|
|
423
|
-
renderable_function.
|
|
423
|
+
renderable_function.bind_call(self, *args, **kwargs)
|
|
424
424
|
end
|
|
425
425
|
|
|
426
426
|
# Function calls can return components, so if it's a component handle it specially
|
|
@@ -60,13 +60,7 @@ module ViewComponent
|
|
|
60
60
|
# Strip the annotation line to maintain correct line numbers when coverage
|
|
61
61
|
# is running (avoids segfault from negative lineno)
|
|
62
62
|
def strip_annotation_line?
|
|
63
|
-
erb_newline_compensation_needed? &&
|
|
64
|
-
coverage_running? &&
|
|
65
|
-
ActionView::Base.annotate_rendered_view_with_filenames
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def erb_newline_compensation_needed?
|
|
69
|
-
Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 && details.handler == :erb
|
|
63
|
+
erb_newline_compensation_needed? && coverage_running?
|
|
70
64
|
end
|
|
71
65
|
|
|
72
66
|
def compiled_source
|
|
@@ -82,22 +76,11 @@ module ViewComponent
|
|
|
82
76
|
def initialize(component:, inline_template:)
|
|
83
77
|
details = ActionView::TemplateDetails.new(nil, inline_template.language.to_sym, nil, nil)
|
|
84
78
|
|
|
85
|
-
# Rails 8.1 added a newline to compiled ERB output (rails/rails#53731).
|
|
86
|
-
# Subtract 1 to compensate for correct line numbers in stack traces.
|
|
87
|
-
# Inline templates start at line 2+ (defined inside a class), so this
|
|
88
|
-
# won't result in negative line numbers that cause segfaults with coverage.
|
|
89
|
-
lineno =
|
|
90
|
-
if Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 && details.handler == :erb
|
|
91
|
-
inline_template.lineno - 1
|
|
92
|
-
else
|
|
93
|
-
inline_template.lineno
|
|
94
|
-
end
|
|
95
|
-
|
|
96
79
|
super(
|
|
97
80
|
component: component,
|
|
98
81
|
details: details,
|
|
99
82
|
path: inline_template.path,
|
|
100
|
-
lineno: lineno,
|
|
83
|
+
lineno: inline_template.lineno,
|
|
101
84
|
)
|
|
102
85
|
|
|
103
86
|
@source = inline_template.source.dup
|
|
@@ -106,6 +89,19 @@ module ViewComponent
|
|
|
106
89
|
def type
|
|
107
90
|
:inline
|
|
108
91
|
end
|
|
92
|
+
|
|
93
|
+
private
|
|
94
|
+
|
|
95
|
+
# Rails 8.1 added a newline to compiled ERB output (rails/rails#53731).
|
|
96
|
+
# Subtract 1 to compensate for correct line numbers in stack traces.
|
|
97
|
+
# Inline templates start at line 2+ (defined inside a class), so this
|
|
98
|
+
# won't result in negative line numbers that cause segfaults with coverage.
|
|
99
|
+
#
|
|
100
|
+
# Computed at compile time rather than at initialization because the
|
|
101
|
+
# annotation setting can change between the two.
|
|
102
|
+
def lineno
|
|
103
|
+
erb_newline_compensation_needed? ? super - 1 : super
|
|
104
|
+
end
|
|
109
105
|
end
|
|
110
106
|
|
|
111
107
|
class InlineCall < Template
|
|
@@ -127,14 +123,13 @@ module ViewComponent
|
|
|
127
123
|
@component.define_method(safe_method_name, @component.instance_method(@call_method_name))
|
|
128
124
|
end
|
|
129
125
|
|
|
130
|
-
def
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
Kernel.warn("WARNING: The #{self.class} component rendered HTML-unsafe output. " \
|
|
126
|
+
def safe_method_call
|
|
127
|
+
<<~RUBY
|
|
128
|
+
__vc_maybe_escape_html(#{super}) do
|
|
129
|
+
Kernel.warn("WARNING: The \#{self.class} component rendered HTML-unsafe output. " \
|
|
135
130
|
"The output will be automatically escaped, but you may want to investigate.")
|
|
136
131
|
end
|
|
137
|
-
|
|
132
|
+
RUBY
|
|
138
133
|
end
|
|
139
134
|
|
|
140
135
|
def defined_on_self?
|
|
@@ -160,9 +155,13 @@ module ViewComponent
|
|
|
160
155
|
defined?(Coverage) && Coverage.running?
|
|
161
156
|
end
|
|
162
157
|
|
|
163
|
-
def
|
|
164
|
-
|
|
165
|
-
|
|
158
|
+
def safe_method_call
|
|
159
|
+
method_name = safe_method_name
|
|
160
|
+
if /\A[a-zA-Z_]\w*[!?]?\z/.match?(method_name)
|
|
161
|
+
method_name
|
|
162
|
+
else
|
|
163
|
+
"send(#{method_name.to_sym.inspect})"
|
|
164
|
+
end
|
|
166
165
|
end
|
|
167
166
|
|
|
168
167
|
def requires_compiled_superclass?
|
|
@@ -196,16 +195,32 @@ module ViewComponent
|
|
|
196
195
|
|
|
197
196
|
attr_reader :lineno
|
|
198
197
|
|
|
198
|
+
def erb_newline_compensation_needed?
|
|
199
|
+
Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 &&
|
|
200
|
+
details.handler == :erb &&
|
|
201
|
+
annotated?
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# The newline compensated for lives inside the `<!-- BEGIN ... -->`
|
|
205
|
+
# annotation, which Rails only prepends to HTML templates, and only when
|
|
206
|
+
# annotations are enabled. Compensating outside those conditions shifts
|
|
207
|
+
# backtraces by a line and, under coverage, strips real template source.
|
|
208
|
+
def annotated?
|
|
209
|
+
ActionView::Base.annotate_rendered_view_with_filenames && html?
|
|
210
|
+
end
|
|
211
|
+
|
|
199
212
|
def compiled_source
|
|
200
213
|
handler = details.handler_class
|
|
201
|
-
|
|
202
|
-
this_source
|
|
214
|
+
original_source = source
|
|
215
|
+
this_source = @component.strip_trailing_whitespace? ? original_source.rstrip : original_source
|
|
203
216
|
|
|
204
217
|
short_identifier = defined?(Rails.root) ? @path.sub("#{Rails.root}/", "") : @path
|
|
205
218
|
format = self.format || DEFAULT_FORMAT
|
|
206
219
|
type = ActionView::Template::Types[format]
|
|
207
220
|
|
|
208
|
-
handler.call(DataWithSource.new(format:, identifier: @path, short_identifier:, type:), this_source)
|
|
221
|
+
handler.call(DataWithSource.new(format:, identifier: @path, short_identifier:, type:), this_source).tap do |compiled|
|
|
222
|
+
CacheDigest.compiled(handler, original_source, compiled)
|
|
223
|
+
end
|
|
209
224
|
end
|
|
210
225
|
end
|
|
211
226
|
end
|
|
@@ -30,7 +30,7 @@ module ViewComponent
|
|
|
30
30
|
# can inherit translations from its parent and is able to overwrite them.
|
|
31
31
|
translation_files = ancestors.reverse_each.with_object([]) do |ancestor, files|
|
|
32
32
|
if ancestor.is_a?(Class) && ancestor < ViewComponent::Base
|
|
33
|
-
files.concat(ancestor.
|
|
33
|
+
files.concat(ancestor.__vc_sidecar_files(TRANSLATION_EXTENSIONS))
|
|
34
34
|
end
|
|
35
35
|
end
|
|
36
36
|
|
|
@@ -59,7 +59,11 @@ module ViewComponent
|
|
|
59
59
|
locale = options.delete(:locale) || ::I18n.locale
|
|
60
60
|
key = __vc_i18n_key(key, options.delete(:scope))
|
|
61
61
|
|
|
62
|
-
__vc_i18n_backend
|
|
62
|
+
if __vc_i18n_backend&.exists?(locale, key)
|
|
63
|
+
__vc_i18n_backend.translate(locale, key, options)
|
|
64
|
+
else
|
|
65
|
+
::I18n.translate(key, locale: locale, **options)
|
|
66
|
+
end
|
|
63
67
|
end
|
|
64
68
|
|
|
65
69
|
alias_method :t, :translate
|
|
@@ -102,17 +106,8 @@ module ViewComponent
|
|
|
102
106
|
|
|
103
107
|
__vc_html_escape_translation_options!(options) if as_html
|
|
104
108
|
|
|
105
|
-
if
|
|
106
|
-
translated =
|
|
107
|
-
catch(:exception) do
|
|
108
|
-
__vc_i18n_backend.translate(locale, key, options)
|
|
109
|
-
end
|
|
110
|
-
|
|
111
|
-
# Fallback to the global translations
|
|
112
|
-
if translated.is_a? ::I18n::MissingTranslation
|
|
113
|
-
return @view_context.translate(key, locale: locale, **options)
|
|
114
|
-
end
|
|
115
|
-
|
|
109
|
+
if __vc_i18n_backend.exists?(locale, key)
|
|
110
|
+
translated = __vc_i18n_backend.translate(locale, key, options)
|
|
116
111
|
translated = __vc_html_safe_translation(translated) if as_html
|
|
117
112
|
translated
|
|
118
113
|
else
|