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
|
@@ -3,26 +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 is invalidated when the component's template, Ruby
|
|
16
|
-
# class, sidecar files, or child components change. This covers blocks
|
|
17
|
-
# wrapping the component in a view and blocks inside the component's own
|
|
18
|
-
# template.
|
|
19
|
-
# 2. Enables the `cache_on` macro, which caches the component's own rendered
|
|
20
|
-
# 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`.
|
|
21
10
|
#
|
|
22
11
|
# ```ruby
|
|
23
12
|
# class MessageComponent < ViewComponent::Base
|
|
24
|
-
# include ViewComponent::ExperimentallyCacheable
|
|
25
|
-
#
|
|
26
13
|
# cache_on :message
|
|
27
14
|
#
|
|
28
15
|
# def initialize(message:)
|
|
@@ -30,7 +17,7 @@ module ViewComponent
|
|
|
30
17
|
# end
|
|
31
18
|
# end
|
|
32
19
|
# ```
|
|
33
|
-
module
|
|
20
|
+
module Cacheable
|
|
34
21
|
extend ActiveSupport::Concern
|
|
35
22
|
|
|
36
23
|
# Stands in for `nil` in the cache key. Without it, `expand_cache_key`
|
|
@@ -38,11 +25,6 @@ module ViewComponent
|
|
|
38
25
|
# that respect would share an entry.
|
|
39
26
|
NIL_CACHE_VALUE = :__vc_nil
|
|
40
27
|
|
|
41
|
-
included do
|
|
42
|
-
ViewComponent::CacheDigest.install!
|
|
43
|
-
ViewComponent::CacheDigest.register(self)
|
|
44
|
-
end
|
|
45
|
-
|
|
46
28
|
class_methods do
|
|
47
29
|
# Declare the values that identify a rendering of this component.
|
|
48
30
|
#
|
|
@@ -55,8 +37,7 @@ module ViewComponent
|
|
|
55
37
|
# ```
|
|
56
38
|
#
|
|
57
39
|
# Calling `cache_on` opts the component into caching its own output.
|
|
58
|
-
#
|
|
59
|
-
# Rails' digest tree.
|
|
40
|
+
# Components that don't call it aren't cached.
|
|
60
41
|
#
|
|
61
42
|
# Pass `if:` or `unless:` to cache only some renders. Both accept a method
|
|
62
43
|
# name or a proc evaluated on the component:
|
|
@@ -119,11 +100,6 @@ module ViewComponent
|
|
|
119
100
|
superclass.__vc_cache_unless if superclass.respond_to?(:__vc_cache_unless)
|
|
120
101
|
end
|
|
121
102
|
|
|
122
|
-
# @private
|
|
123
|
-
def __vc_cacheable?
|
|
124
|
-
true
|
|
125
|
-
end
|
|
126
|
-
|
|
127
103
|
# Whether this component caches its own rendered output.
|
|
128
104
|
#
|
|
129
105
|
# @return [Boolean]
|
|
@@ -131,91 +107,42 @@ module ViewComponent
|
|
|
131
107
|
__vc_cache_on.any?
|
|
132
108
|
end
|
|
133
109
|
|
|
134
|
-
#
|
|
135
|
-
#
|
|
136
|
-
# partials it renders.
|
|
137
|
-
#
|
|
138
|
-
# Computed with Rails' own `ActionView::Digestor`, so it's the same digest
|
|
139
|
-
# used to invalidate `<% cache %>` blocks.
|
|
110
|
+
# `__vc_caches_output?`, stored when the component compiles so rendering
|
|
111
|
+
# reads a flag instead of computing it.
|
|
140
112
|
#
|
|
141
|
-
# Usable outside a request, where no view context exists:
|
|
142
|
-
#
|
|
143
|
-
# ```ruby
|
|
144
|
-
# MessageComponent.cache_digest
|
|
145
|
-
# ```
|
|
146
|
-
#
|
|
147
|
-
# @param finder [ActionView::LookupContext] Defaults to a lookup context
|
|
148
|
-
# built from `ActionController::Base.view_paths`.
|
|
149
|
-
# @param format [Symbol]
|
|
150
|
-
# @return [String]
|
|
151
|
-
def cache_digest(finder: nil, format: :html)
|
|
152
|
-
ViewComponent::CacheDigest.digest(
|
|
153
|
-
self,
|
|
154
|
-
finder: finder || ViewComponent::CacheDigest.default_finder,
|
|
155
|
-
format: format
|
|
156
|
-
)
|
|
157
|
-
end
|
|
158
|
-
|
|
159
113
|
# @private
|
|
160
|
-
|
|
161
|
-
super
|
|
162
|
-
ViewComponent::CacheDigest.register(child)
|
|
163
|
-
end
|
|
164
|
-
end
|
|
165
|
-
|
|
166
|
-
# Renders the component, reading from and writing to the Rails cache when
|
|
167
|
-
# `cache_on` has been declared.
|
|
168
|
-
#
|
|
169
|
-
# @private
|
|
170
|
-
def render_in(view_context, **, &block)
|
|
171
|
-
return super unless self.class.__vc_caches_output?
|
|
172
|
-
|
|
173
|
-
# Content provided by the caller isn't part of the cache key, so caching
|
|
174
|
-
# it would serve one caller's content to another. Raised whether or not
|
|
175
|
-
# caching is currently enabled, so the conflict surfaces in development
|
|
176
|
-
# and test rather than only in production.
|
|
177
|
-
if block || __vc_content_set_by_with_content_defined? || __vc_slots_set_by_caller?
|
|
178
|
-
raise ContentPassedToCachedComponentError.new(self.class.name)
|
|
179
|
-
end
|
|
180
|
-
|
|
181
|
-
return super unless __vc_cache_enabled?(view_context)
|
|
182
|
-
|
|
183
|
-
store = Rails.cache
|
|
184
|
-
key = cache_key(view_context)
|
|
185
|
-
|
|
186
|
-
if (cached = store.read(key))
|
|
187
|
-
# Safe to mark as HTML-safe: the cached string was produced by this same
|
|
188
|
-
# rendering pipeline, which escapes output before it's written.
|
|
189
|
-
return cached.html_safe
|
|
190
|
-
end
|
|
191
|
-
|
|
192
|
-
super.tap do |output|
|
|
193
|
-
store.write(key, output.to_s)
|
|
194
|
-
end
|
|
114
|
+
attr_accessor :__vc_cache_output
|
|
195
115
|
end
|
|
196
116
|
|
|
197
117
|
# The cache key for this rendering of the component.
|
|
198
118
|
#
|
|
199
119
|
# Combines the component's identity, its source digest, the requested
|
|
200
|
-
#
|
|
201
|
-
# `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.
|
|
202
122
|
#
|
|
203
123
|
# @param view_context [ActionView::Base]
|
|
204
124
|
# @return [String]
|
|
205
125
|
def cache_key(view_context = nil)
|
|
206
126
|
lookup_context = view_context&.lookup_context
|
|
207
|
-
|
|
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
|
|
208
131
|
|
|
209
132
|
parts = [
|
|
210
133
|
"view_component",
|
|
211
134
|
self.class.virtual_path,
|
|
212
|
-
self.class.cache_digest(finder: lookup_context, format:
|
|
135
|
+
self.class.cache_digest(finder: lookup_context, format: formats.first),
|
|
213
136
|
# Included in its own right, not just as a digest input: components that
|
|
214
137
|
# render every format from one template have the same digest for each.
|
|
215
|
-
format
|
|
216
|
-
|
|
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,
|
|
217
142
|
I18n.locale,
|
|
218
|
-
|
|
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
|
|
219
146
|
]
|
|
220
147
|
|
|
221
148
|
# Positions are significant, so nils are substituted rather than removed.
|
|
@@ -226,34 +153,34 @@ module ViewComponent
|
|
|
226
153
|
)
|
|
227
154
|
end
|
|
228
155
|
|
|
229
|
-
|
|
230
|
-
#
|
|
231
|
-
# `ActionView::Helpers::CacheHelper` digests the virtual path of whichever
|
|
232
|
-
# template is rendering. Inside a component that path is the component's
|
|
233
|
-
# own, which resolves to nothing: component templates aren't in the view
|
|
234
|
-
# paths. The Digestor returns an empty digest, `CacheHelper` falls back to
|
|
235
|
-
# the bare virtual path, and the fragment never invalidates.
|
|
236
|
-
#
|
|
237
|
-
# Substituting the digest the component already computes makes a `cache`
|
|
238
|
-
# block in a component template behave like one in a view. Everything else
|
|
239
|
-
# the component renders — a partial, say — keeps Rails' behavior.
|
|
240
|
-
#
|
|
241
|
-
# @private
|
|
242
|
-
def digest_path_from_template(template)
|
|
243
|
-
component_path = self.class.virtual_path
|
|
244
|
-
return super unless component_path && template.virtual_path == component_path
|
|
245
|
-
|
|
246
|
-
digest = self.class.cache_digest(
|
|
247
|
-
finder: lookup_context,
|
|
248
|
-
format: template.format || __vc_cache_format(lookup_context)
|
|
249
|
-
)
|
|
156
|
+
private
|
|
250
157
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
158
|
+
# Renders through the Rails cache. Called by `Base#render_in` for
|
|
159
|
+
# components that declare `cache_on`.
|
|
160
|
+
def __vc_render_cached(view_context, content_block)
|
|
161
|
+
# Content provided by the caller isn't part of the cache key, so caching
|
|
162
|
+
# it would serve one caller's content to another. Raised whether or not
|
|
163
|
+
# caching is currently enabled, so the conflict surfaces in development
|
|
164
|
+
# and test rather than only in production.
|
|
165
|
+
if content_block || __vc_content_set_by_with_content_defined? || __vc_slots_set_by_caller?
|
|
166
|
+
raise ContentPassedToCachedComponentError.new(self.class.name)
|
|
167
|
+
end
|
|
255
168
|
|
|
256
|
-
|
|
169
|
+
return __vc_render_uncached(view_context) unless __vc_cache_enabled?(view_context)
|
|
170
|
+
|
|
171
|
+
store = Rails.cache
|
|
172
|
+
key = cache_key(view_context)
|
|
173
|
+
|
|
174
|
+
if (cached = store.read(key))
|
|
175
|
+
# Safe to mark as HTML-safe: the cached string was produced by this same
|
|
176
|
+
# rendering pipeline, which escapes output before it's written.
|
|
177
|
+
return cached.html_safe
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
__vc_render_uncached(view_context).tap do |output|
|
|
181
|
+
store.write(key, output.to_s)
|
|
182
|
+
end
|
|
183
|
+
end
|
|
257
184
|
|
|
258
185
|
# Slots set by the caller via `with_*`. Checked before rendering, so slots
|
|
259
186
|
# a component fills in for itself with a `default_*` method — which resolve
|
|
@@ -302,12 +229,8 @@ module ViewComponent
|
|
|
302
229
|
end
|
|
303
230
|
end
|
|
304
231
|
|
|
305
|
-
def
|
|
306
|
-
Array(lookup_context&.
|
|
307
|
-
end
|
|
308
|
-
|
|
309
|
-
def __vc_cache_variant(lookup_context)
|
|
310
|
-
Array(lookup_context&.variants).first
|
|
232
|
+
def __vc_cache_variants(lookup_context)
|
|
233
|
+
Array(lookup_context&.variants)
|
|
311
234
|
end
|
|
312
235
|
end
|
|
313
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
|
|
@@ -15,7 +15,7 @@ module ViewComponent
|
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
def compiled?(klass)
|
|
18
|
-
cache.include? klass
|
|
18
|
+
@@cache.include? klass
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def invalidate_class!(klass)
|
|
@@ -26,13 +26,73 @@ module ViewComponent
|
|
|
26
26
|
cache.clear
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
+
# Share directory listings, sidecar lookups, and file contents within one
|
|
30
|
+
# eager load or digest computation, but never across reloads.
|
|
31
|
+
def with_sidecar_files_cache
|
|
32
|
+
return yield if Fiber[:__vc_sidecar_files]
|
|
33
|
+
|
|
34
|
+
begin
|
|
35
|
+
Fiber[:__vc_sidecar_files] = {}
|
|
36
|
+
Fiber[:__vc_sidecar_directories] = {}
|
|
37
|
+
Fiber[:__vc_source_files] = {}
|
|
38
|
+
Fiber[:__vc_path_parser] = ActionView::Resolver::PathParser.new
|
|
39
|
+
yield
|
|
40
|
+
ensure
|
|
41
|
+
Fiber[:__vc_sidecar_files] = nil
|
|
42
|
+
Fiber[:__vc_sidecar_directories] = nil
|
|
43
|
+
Fiber[:__vc_source_files] = nil
|
|
44
|
+
Fiber[:__vc_path_parser] = nil
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def path_parser
|
|
49
|
+
# Its regular expression depends on registered handlers, formats and locales.
|
|
50
|
+
# Reuse it during eager loading or a digest computation, but not across reloads.
|
|
51
|
+
Fiber[:__vc_path_parser] || ActionView::Resolver::PathParser.new
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def read_file(path)
|
|
55
|
+
files = Fiber[:__vc_source_files]
|
|
56
|
+
return File.read(path) unless files
|
|
57
|
+
|
|
58
|
+
(files[path] ||= File.read(path).freeze).dup
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def sidecar_files_for(klass, extensions)
|
|
62
|
+
sidecar_files_by_component = Fiber[:__vc_sidecar_files]
|
|
63
|
+
return yield unless sidecar_files_by_component
|
|
64
|
+
|
|
65
|
+
sidecar_files = sidecar_files_by_component[klass] ||= {}
|
|
66
|
+
sidecar_files[extensions] ||= yield.freeze
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def sidecar_files_matching(pattern)
|
|
70
|
+
directories = Fiber[:__vc_sidecar_directories]
|
|
71
|
+
return Dir[pattern] unless directories
|
|
72
|
+
|
|
73
|
+
filename = File.basename(pattern)
|
|
74
|
+
prefix = filename.delete_suffix(".*")
|
|
75
|
+
return Dir[pattern] if prefix.match?(/[*?{}\[\]\\]/)
|
|
76
|
+
|
|
77
|
+
directory = File.dirname(pattern)
|
|
78
|
+
# Index the shared directory by component name instead of scanning all
|
|
79
|
+
# its entries again for every component and extension.
|
|
80
|
+
entries = directories[directory] ||= Dir.glob("#{directory}/*", File::FNM_DOTMATCH)
|
|
81
|
+
.group_by { |path| File.basename(path).split(".", 2).first }
|
|
82
|
+
candidates = entries.fetch(prefix.split(".", 2).first, [])
|
|
83
|
+
candidates.select { |path| File.fnmatch?(filename, File.basename(path)) }
|
|
84
|
+
end
|
|
85
|
+
|
|
29
86
|
# Invalidates the cache when a template or a translation file is added,
|
|
30
87
|
# changed, or removed. Changes to Ruby files reload the application instead.
|
|
31
88
|
def template_watcher(file_watcher)
|
|
32
89
|
extensions = ActionView::Template::Handlers.extensions.map(&:to_s) + %w[yml yaml]
|
|
33
|
-
directories = ActiveSupport::Dependencies.autoload_paths.index_with(extensions)
|
|
90
|
+
directories = ActiveSupport::Dependencies.autoload_paths.map(&:to_s).index_with(extensions)
|
|
34
91
|
|
|
35
|
-
file_watcher.new([], directories)
|
|
92
|
+
file_watcher.new([], directories) do
|
|
93
|
+
invalidate!
|
|
94
|
+
ActionView::LookupContext::DetailsKey.clear
|
|
95
|
+
end
|
|
36
96
|
end
|
|
37
97
|
end
|
|
38
98
|
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:
|
|
@@ -28,10 +26,14 @@ module ViewComponent
|
|
|
28
26
|
# early exit
|
|
29
27
|
return if compiled? && !force
|
|
30
28
|
|
|
29
|
+
reset_templates
|
|
30
|
+
|
|
31
31
|
gather_templates
|
|
32
32
|
|
|
33
|
-
if self.class.__vc_development_mode && @templates.any?(&:requires_compiled_superclass?)
|
|
33
|
+
if (self.class.__vc_development_mode || !@component.superclass.__vc_compiled?) && @templates.any?(&:requires_compiled_superclass?)
|
|
34
34
|
@component.superclass.__vc_compile(raise_errors: raise_errors)
|
|
35
|
+
@templates = nil
|
|
36
|
+
gather_templates
|
|
35
37
|
end
|
|
36
38
|
|
|
37
39
|
if template_errors.present?
|
|
@@ -50,6 +52,8 @@ module ViewComponent
|
|
|
50
52
|
|
|
51
53
|
define_render_template_for
|
|
52
54
|
|
|
55
|
+
@component.__vc_cache_output = @component.__vc_caches_output?
|
|
56
|
+
|
|
53
57
|
@component.__vc_register_default_slots
|
|
54
58
|
@component.__vc_build_i18n_backend
|
|
55
59
|
|
|
@@ -76,10 +80,39 @@ module ViewComponent
|
|
|
76
80
|
filtered_templates
|
|
77
81
|
end
|
|
78
82
|
|
|
83
|
+
# Formats the component has templates for. Inline templates and `#call`
|
|
84
|
+
# methods have no format of their own and render as HTML.
|
|
85
|
+
#
|
|
86
|
+
# @return [Array<Symbol>]
|
|
87
|
+
def template_formats
|
|
88
|
+
gather_templates.map { |template| template.format || :html }.uniq
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# @private
|
|
92
|
+
def templates_for_call(method_name)
|
|
93
|
+
gather_templates.select { |template| template.call_method_name == method_name }
|
|
94
|
+
end
|
|
95
|
+
|
|
79
96
|
private
|
|
80
97
|
|
|
81
98
|
attr_reader :templates
|
|
82
99
|
|
|
100
|
+
def reset_templates
|
|
101
|
+
@templates&.each do |template|
|
|
102
|
+
next if template.inline_call?
|
|
103
|
+
|
|
104
|
+
call_method = template.call_method_name
|
|
105
|
+
safe_method = template.safe_method_name
|
|
106
|
+
next unless @component.method_defined?(call_method, false) && @component.method_defined?(safe_method, false)
|
|
107
|
+
next unless @component.instance_method(call_method) == @component.instance_method(safe_method)
|
|
108
|
+
|
|
109
|
+
@component.remove_method(call_method)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
@templates = nil
|
|
113
|
+
@_template_errors = nil
|
|
114
|
+
end
|
|
115
|
+
|
|
83
116
|
def define_render_template_for
|
|
84
117
|
@templates.each do |template|
|
|
85
118
|
template.compile_to_component
|
|
@@ -87,26 +120,50 @@ module ViewComponent
|
|
|
87
120
|
|
|
88
121
|
@component.silence_redefinition_of_method(:render_template_for)
|
|
89
122
|
|
|
123
|
+
# Keep template references local to this method's owner, including when
|
|
124
|
+
# render_parent invokes it on an instance of a descendant component.
|
|
125
|
+
methods = Module.new
|
|
126
|
+
@templates.each_with_index { |template, index| methods.const_set(:"TEMPLATE_#{index}", template) }
|
|
127
|
+
|
|
90
128
|
if @templates.one?
|
|
91
129
|
template = @templates.first
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
end
|
|
130
|
+
body = <<~RUBY
|
|
131
|
+
@current_template = TEMPLATE_0
|
|
132
|
+
#{template.safe_method_call}
|
|
133
|
+
RUBY
|
|
97
134
|
else
|
|
98
|
-
|
|
99
|
-
@
|
|
100
|
-
|
|
101
|
-
|
|
135
|
+
methods.const_set(:COMPILER, self)
|
|
136
|
+
calls = @templates.each_with_index.map do |template, index|
|
|
137
|
+
"when TEMPLATE_#{index}\n#{template.safe_method_call}"
|
|
138
|
+
end.join("\n")
|
|
139
|
+
body = <<~RUBY
|
|
140
|
+
@current_template = COMPILER.find_templates_for(details).first
|
|
141
|
+
case @current_template
|
|
142
|
+
#{calls}
|
|
102
143
|
else
|
|
103
|
-
raise MissingTemplateError.new(self.class.name,
|
|
144
|
+
raise ViewComponent::MissingTemplateError.new(self.class.name, @lookup_context)
|
|
104
145
|
end
|
|
146
|
+
RUBY
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
methods.module_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
150
|
+
def render_template_for(details)
|
|
151
|
+
#{body}
|
|
105
152
|
end
|
|
153
|
+
RUBY
|
|
154
|
+
@component.define_method(:render_template_for, methods.instance_method(:render_template_for))
|
|
155
|
+
|
|
156
|
+
# Descendants retain these unbound methods for render_parent.
|
|
157
|
+
render_method = @component.instance_method(:render_template_for)
|
|
158
|
+
@component.descendants.each do |descendant|
|
|
159
|
+
ancestor_calls = descendant.instance_variable_get(:@__vc_ancestor_calls)
|
|
160
|
+
ancestor_calls&.map! { |call| (call.owner == @component) ? render_method : call }
|
|
106
161
|
end
|
|
107
162
|
end
|
|
108
163
|
|
|
109
164
|
def template_errors
|
|
165
|
+
return [] if @templates.one?
|
|
166
|
+
|
|
110
167
|
@_template_errors ||= begin
|
|
111
168
|
errors = []
|
|
112
169
|
|
|
@@ -177,8 +234,8 @@ module ViewComponent
|
|
|
177
234
|
inline_template: @component.__vc_inline_template
|
|
178
235
|
)]
|
|
179
236
|
else
|
|
180
|
-
path_parser =
|
|
181
|
-
templates = @component.
|
|
237
|
+
path_parser = CompileCache.path_parser
|
|
238
|
+
templates = @component.__vc_sidecar_files(
|
|
182
239
|
ActionView::Template::Handlers.extensions
|
|
183
240
|
).map do |path|
|
|
184
241
|
details = path_parser.parse(path).details
|
|
@@ -192,6 +249,20 @@ module ViewComponent
|
|
|
192
249
|
).flat_map { |ancestor| ancestor.instance_methods(false).grep(/^call(_|$)/) }
|
|
193
250
|
.uniq
|
|
194
251
|
.each do |method_name|
|
|
252
|
+
unless component_instance_methods_on_self.include?(method_name)
|
|
253
|
+
owner = @component.instance_method(method_name).owner
|
|
254
|
+
inherited = owner.respond_to?(:__vc_compiler) ? owner.__vc_compiler.templates_for_call(method_name) : []
|
|
255
|
+
if inherited.any?
|
|
256
|
+
# A format and a variant can share a public call_* alias.
|
|
257
|
+
inherited.each do |template|
|
|
258
|
+
next if templates.any? { |own| own.variant == template.variant && own.format == template.format }
|
|
259
|
+
|
|
260
|
+
templates << Template::Inherited.new(component: @component, template: template)
|
|
261
|
+
end
|
|
262
|
+
next
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
|
|
195
266
|
templates << Template::InlineCall.new(
|
|
196
267
|
component: @component,
|
|
197
268
|
method_name: method_name,
|