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
|
@@ -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
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
|
|
@@ -26,10 +26,14 @@ module ViewComponent
|
|
|
26
26
|
# early exit
|
|
27
27
|
return if compiled? && !force
|
|
28
28
|
|
|
29
|
+
reset_templates
|
|
30
|
+
|
|
29
31
|
gather_templates
|
|
30
32
|
|
|
31
|
-
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?)
|
|
32
34
|
@component.superclass.__vc_compile(raise_errors: raise_errors)
|
|
35
|
+
@templates = nil
|
|
36
|
+
gather_templates
|
|
33
37
|
end
|
|
34
38
|
|
|
35
39
|
if template_errors.present?
|
|
@@ -84,10 +88,31 @@ module ViewComponent
|
|
|
84
88
|
gather_templates.map { |template| template.format || :html }.uniq
|
|
85
89
|
end
|
|
86
90
|
|
|
91
|
+
# @private
|
|
92
|
+
def templates_for_call(method_name)
|
|
93
|
+
gather_templates.select { |template| template.call_method_name == method_name }
|
|
94
|
+
end
|
|
95
|
+
|
|
87
96
|
private
|
|
88
97
|
|
|
89
98
|
attr_reader :templates
|
|
90
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
|
+
|
|
91
116
|
def define_render_template_for
|
|
92
117
|
@templates.each do |template|
|
|
93
118
|
template.compile_to_component
|
|
@@ -95,26 +120,50 @@ module ViewComponent
|
|
|
95
120
|
|
|
96
121
|
@component.silence_redefinition_of_method(:render_template_for)
|
|
97
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
|
+
|
|
98
128
|
if @templates.one?
|
|
99
129
|
template = @templates.first
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
end
|
|
130
|
+
body = <<~RUBY
|
|
131
|
+
@current_template = TEMPLATE_0
|
|
132
|
+
#{template.safe_method_call}
|
|
133
|
+
RUBY
|
|
105
134
|
else
|
|
106
|
-
|
|
107
|
-
@
|
|
108
|
-
|
|
109
|
-
|
|
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}
|
|
110
143
|
else
|
|
111
|
-
raise MissingTemplateError.new(self.class.name, details)
|
|
144
|
+
raise ViewComponent::MissingTemplateError.new(self.class.name, details)
|
|
112
145
|
end
|
|
146
|
+
RUBY
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
methods.module_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
150
|
+
def render_template_for(details)
|
|
151
|
+
#{body}
|
|
113
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 }
|
|
114
161
|
end
|
|
115
162
|
end
|
|
116
163
|
|
|
117
164
|
def template_errors
|
|
165
|
+
return [] if @templates.one?
|
|
166
|
+
|
|
118
167
|
@_template_errors ||= begin
|
|
119
168
|
errors = []
|
|
120
169
|
|
|
@@ -185,8 +234,8 @@ module ViewComponent
|
|
|
185
234
|
inline_template: @component.__vc_inline_template
|
|
186
235
|
)]
|
|
187
236
|
else
|
|
188
|
-
path_parser =
|
|
189
|
-
templates = @component.
|
|
237
|
+
path_parser = CompileCache.path_parser
|
|
238
|
+
templates = @component.__vc_sidecar_files(
|
|
190
239
|
ActionView::Template::Handlers.extensions
|
|
191
240
|
).map do |path|
|
|
192
241
|
details = path_parser.parse(path).details
|
|
@@ -200,6 +249,20 @@ module ViewComponent
|
|
|
200
249
|
).flat_map { |ancestor| ancestor.instance_methods(false).grep(/^call(_|$)/) }
|
|
201
250
|
.uniq
|
|
202
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
|
+
|
|
203
266
|
templates << Template::InlineCall.new(
|
|
204
267
|
component: @component,
|
|
205
268
|
method_name: method_name,
|
|
@@ -1,9 +1,102 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "view_component/
|
|
3
|
+
require "view_component/errors"
|
|
4
4
|
|
|
5
5
|
module ViewComponent
|
|
6
6
|
class Config
|
|
7
|
+
# An `ActiveSupport::OrderedOptions` that raises when an option it doesn't
|
|
8
|
+
# know about is set, rather than silently storing it.
|
|
9
|
+
class Options < ActiveSupport::OrderedOptions # :nodoc:
|
|
10
|
+
ROOT_NAMESPACE = "config.view_component"
|
|
11
|
+
|
|
12
|
+
def initialize(namespace, allowed_keys, default: nil, renamed: {}, removed: {})
|
|
13
|
+
super(default)
|
|
14
|
+
@namespace = namespace
|
|
15
|
+
@allowed_keys = allowed_keys
|
|
16
|
+
@renamed = renamed
|
|
17
|
+
@removed = removed
|
|
18
|
+
@nested_options = {}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def []=(key, value)
|
|
22
|
+
key = key.to_sym
|
|
23
|
+
|
|
24
|
+
if @allowed_keys.include?(key)
|
|
25
|
+
@nested_options[key] ||= value if value.is_a?(Options)
|
|
26
|
+
if (nested_options = @nested_options[key])
|
|
27
|
+
raise TypeError, "#{@namespace}.#{key} must be a Hash" unless value.is_a?(Hash)
|
|
28
|
+
|
|
29
|
+
value = nested_options.dup.clear.tap do |options|
|
|
30
|
+
value.each_pair { |nested_key, nested_value| options[nested_key] = nested_value }
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
super
|
|
35
|
+
else
|
|
36
|
+
raise UnknownConfigOptionError.new(
|
|
37
|
+
"#{@namespace}.#{key}",
|
|
38
|
+
renamed_to: @renamed[key] && "#{ROOT_NAMESPACE}.#{@renamed[key]}",
|
|
39
|
+
removed: @removed.key?(key),
|
|
40
|
+
hint: @removed[key],
|
|
41
|
+
valid_options: @allowed_keys
|
|
42
|
+
)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
alias_method :store, :[]=
|
|
47
|
+
|
|
48
|
+
def merge!(*others)
|
|
49
|
+
others.each do |other|
|
|
50
|
+
{}.merge!(other).each_pair do |key, value|
|
|
51
|
+
key = key.to_sym
|
|
52
|
+
value = yield(key, self[key], value) if block_given? && key?(key)
|
|
53
|
+
self[key] = value
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
self
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
alias_method :update, :merge!
|
|
60
|
+
|
|
61
|
+
def replace(other)
|
|
62
|
+
replacement = dup.clear
|
|
63
|
+
replacement.merge!(other)
|
|
64
|
+
super(replacement)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def transform_keys!(*args, &block)
|
|
68
|
+
return enum_for(:transform_keys!) if args.empty? && !block
|
|
69
|
+
|
|
70
|
+
replace(to_h.transform_keys!(*args, &block))
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def transform_values!(&block)
|
|
74
|
+
return enum_for(:transform_values!) unless block
|
|
75
|
+
|
|
76
|
+
replace(to_h.transform_values!(&block))
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Options renamed in 4.0, mapped to their new name.
|
|
81
|
+
RENAMED_OPTIONS = {
|
|
82
|
+
preview_controller: "previews.controller",
|
|
83
|
+
preview_route: "previews.route",
|
|
84
|
+
preview_paths: "previews.paths",
|
|
85
|
+
show_previews: "previews.enabled",
|
|
86
|
+
default_preview_layout: "previews.default_layout",
|
|
87
|
+
component_parent_class: "generate.parent_class",
|
|
88
|
+
view_component_path: "generate.path"
|
|
89
|
+
}.freeze
|
|
90
|
+
|
|
91
|
+
# Options removed in 4.0, mapped to a hint about what to do instead.
|
|
92
|
+
REMOVED_OPTIONS = {
|
|
93
|
+
test_controller: "Use the `vc_test_controller_class` test helper instead.",
|
|
94
|
+
render_monkey_patch_enabled: nil,
|
|
95
|
+
use_deprecated_instrumentation_name: "Events always use the `render.view_component` name.",
|
|
96
|
+
capture_compatibility_patch_enabled: nil,
|
|
97
|
+
show_previews_source: "Consider using Lookbook instead."
|
|
98
|
+
}.freeze
|
|
99
|
+
|
|
7
100
|
class << self
|
|
8
101
|
# `new` without any arguments initializes the default configuration, but
|
|
9
102
|
# it's important to differentiate in case that's no longer the case in
|
|
@@ -11,11 +104,16 @@ module ViewComponent
|
|
|
11
104
|
alias_method :default, :new
|
|
12
105
|
|
|
13
106
|
def defaults
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
previews
|
|
17
|
-
|
|
18
|
-
|
|
107
|
+
options = Options.new(
|
|
108
|
+
Options::ROOT_NAMESPACE,
|
|
109
|
+
%i[generate previews instrumentation_enabled],
|
|
110
|
+
renamed: RENAMED_OPTIONS,
|
|
111
|
+
removed: REMOVED_OPTIONS
|
|
112
|
+
)
|
|
113
|
+
options.generate = default_generate_options
|
|
114
|
+
options.previews = default_previews_options
|
|
115
|
+
options.instrumentation_enabled = false
|
|
116
|
+
options
|
|
19
117
|
end
|
|
20
118
|
|
|
21
119
|
# @!attribute generate
|
|
@@ -156,14 +254,22 @@ module ViewComponent
|
|
|
156
254
|
end
|
|
157
255
|
|
|
158
256
|
def default_generate_options
|
|
159
|
-
options =
|
|
257
|
+
options = Options.new(
|
|
258
|
+
"#{Options::ROOT_NAMESPACE}.generate",
|
|
259
|
+
%i[path preview_path parent_class sidecar stimulus_controller typescript locale distinct_locale_files preview use_component_path_for_rspec_tests],
|
|
260
|
+
default: false,
|
|
261
|
+
renamed: {component_parent_class: RENAMED_OPTIONS.fetch(:component_parent_class)}
|
|
262
|
+
)
|
|
160
263
|
options.preview_path = ""
|
|
161
264
|
options.path = "app/components"
|
|
162
265
|
options
|
|
163
266
|
end
|
|
164
267
|
|
|
165
268
|
def default_previews_options
|
|
166
|
-
options =
|
|
269
|
+
options = Options.new(
|
|
270
|
+
"#{Options::ROOT_NAMESPACE}.previews",
|
|
271
|
+
%i[controller route enabled default_layout paths]
|
|
272
|
+
)
|
|
167
273
|
options.controller = "ViewComponentsController"
|
|
168
274
|
options.route = "/rails/view_components"
|
|
169
275
|
options.enabled = defined?(Rails.env) && (Rails.env.development? || Rails.env.test?)
|
|
@@ -94,11 +94,16 @@ module ViewComponent
|
|
|
94
94
|
ActiveSupport.on_load(:after_initialize) do
|
|
95
95
|
if Rails.application.config.eager_load
|
|
96
96
|
ActiveSupport::Notifications.instrument("compile.view_component") do
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
CompileCache.with_sidecar_files_cache do
|
|
98
|
+
components = ViewComponent::Base.descendants.reject(&:anonymous?)
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
|
|
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
|
|
102
107
|
end
|
|
103
108
|
end
|
|
104
109
|
end
|
|
@@ -145,10 +150,9 @@ module ViewComponent
|
|
|
145
150
|
ActiveSupport.on_load(:action_view) do
|
|
146
151
|
next if ActionView::Base.cache_template_loading
|
|
147
152
|
|
|
148
|
-
template_watcher =
|
|
153
|
+
template_watcher = CompileCache.template_watcher(app.config.file_watcher)
|
|
149
154
|
|
|
150
155
|
app.executor.to_run :before do
|
|
151
|
-
template_watcher ||= CompileCache.template_watcher(app.config.file_watcher)
|
|
152
156
|
template_watcher.execute_if_updated
|
|
153
157
|
end
|
|
154
158
|
end
|
|
@@ -267,4 +267,20 @@ module ViewComponent
|
|
|
267
267
|
super(MESSAGE.gsub("COMPONENT", component_name.to_s))
|
|
268
268
|
end
|
|
269
269
|
end
|
|
270
|
+
|
|
271
|
+
class UnknownConfigOptionError < StandardError
|
|
272
|
+
def initialize(option, renamed_to: nil, removed: false, hint: nil, valid_options: [])
|
|
273
|
+
message =
|
|
274
|
+
if renamed_to
|
|
275
|
+
"`#{option}` was renamed to `#{renamed_to}` in ViewComponent 4.0."
|
|
276
|
+
elsif removed
|
|
277
|
+
"`#{option}` was removed in ViewComponent 4.0."
|
|
278
|
+
else
|
|
279
|
+
"`#{option}` isn't a ViewComponent configuration option. " \
|
|
280
|
+
"Valid options are: #{valid_options.join(", ")}."
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
super([message, hint].compact.join(" "))
|
|
284
|
+
end
|
|
285
|
+
end
|
|
270
286
|
end
|
|
@@ -25,16 +25,11 @@ module ViewComponent
|
|
|
25
25
|
# `render` accepts, but there's currently no mechanism for users to
|
|
26
26
|
# provide these when calling render on a ViewComponent.
|
|
27
27
|
if user_details.equal?(EMPTY_DETAILS)
|
|
28
|
-
# Fast path:
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return cached if cached
|
|
34
|
-
|
|
35
|
-
details, from_cache = detail_args_for(EMPTY_DETAILS)
|
|
36
|
-
@__vc_requested_details_cache =
|
|
37
|
-
from_cache || ActionView::TemplateDetails::Requested.new(**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)
|
|
38
33
|
else
|
|
39
34
|
details, from_cache = detail_args_for(user_details)
|
|
40
35
|
from_cache || ActionView::TemplateDetails::Requested.new(**details)
|
data/lib/view_component/slot.rb
CHANGED
|
@@ -12,6 +12,18 @@ module ViewComponent
|
|
|
12
12
|
@parent = parent
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
+
# @private
|
|
16
|
+
def __vc_dup(parent)
|
|
17
|
+
dup.tap do |copy|
|
|
18
|
+
copy.instance_variable_set(:@parent, parent)
|
|
19
|
+
copy.remove_instance_variable(:@content) if defined?(@content)
|
|
20
|
+
if __vc_component_instance?
|
|
21
|
+
component = @__vc_component_instance
|
|
22
|
+
copy.__vc_component_instance = component.is_a?(Base) ? component.__vc_dup_for_render : component.dup
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
15
27
|
def content?
|
|
16
28
|
return true if defined?(@__vc_content) && @__vc_content.present?
|
|
17
29
|
return true if defined?(@__vc_content_set_by_with_content) && @__vc_content_set_by_with_content.present?
|
|
@@ -216,9 +216,9 @@ module ViewComponent
|
|
|
216
216
|
def __vc_register_default_slots
|
|
217
217
|
registered_slots.each do |slot_name, config|
|
|
218
218
|
default_method_name = :"default_#{slot_name}"
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
219
|
+
registered_slots[slot_name] = config.merge(
|
|
220
|
+
default_method: method_defined?(default_method_name) ? default_method_name : nil
|
|
221
|
+
)
|
|
222
222
|
end
|
|
223
223
|
end
|
|
224
224
|
|
|
@@ -361,6 +361,9 @@ module ViewComponent
|
|
|
361
361
|
|
|
362
362
|
# If there is a default method for the slot, call it
|
|
363
363
|
if (default_method = registered_slots[slot_name][:default_method])
|
|
364
|
+
@__vc_default_slots ||= {}
|
|
365
|
+
return @__vc_default_slots[slot_name] if @__vc_default_slots.key?(slot_name)
|
|
366
|
+
|
|
364
367
|
renderable_value = send(default_method)
|
|
365
368
|
slot = Slot.new(self)
|
|
366
369
|
|
|
@@ -370,13 +373,34 @@ module ViewComponent
|
|
|
370
373
|
slot.__vc_content = renderable_value
|
|
371
374
|
end
|
|
372
375
|
|
|
373
|
-
slot
|
|
376
|
+
@__vc_default_slots[slot_name] = slot
|
|
374
377
|
elsif self.class.registered_slots[slot_name][:collection]
|
|
375
378
|
# If empty slot is a collection, return an empty array
|
|
376
379
|
[]
|
|
377
380
|
end
|
|
378
381
|
end
|
|
379
382
|
|
|
383
|
+
# @private
|
|
384
|
+
# Collection spacers need independent slots whose parent is the new copy.
|
|
385
|
+
def __vc_dup_for_render
|
|
386
|
+
__vc_check_reused_instance!
|
|
387
|
+
|
|
388
|
+
dup.tap do |copy|
|
|
389
|
+
%i[@__vc_set_slots @__vc_default_slots].each do |variable|
|
|
390
|
+
next unless instance_variable_defined?(variable)
|
|
391
|
+
|
|
392
|
+
slots = instance_variable_get(variable).transform_values do |slot|
|
|
393
|
+
if slot.is_a?(Array)
|
|
394
|
+
slot.map { |item| item.__vc_dup(copy) }
|
|
395
|
+
else
|
|
396
|
+
slot.__vc_dup(copy)
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
copy.instance_variable_set(variable, slots)
|
|
400
|
+
end
|
|
401
|
+
end
|
|
402
|
+
end
|
|
403
|
+
|
|
380
404
|
def __vc_set_slot(slot_name, slot_definition = nil, *args, **kwargs, &block)
|
|
381
405
|
slot_definition ||= self.class.registered_slots[slot_name]
|
|
382
406
|
slot = Slot.new(self)
|
|
@@ -408,19 +432,19 @@ module ViewComponent
|
|
|
408
432
|
self.class.const_get(slot_definition[:renderable_class_name]).new(*args, **kwargs)
|
|
409
433
|
# If passed a lambda
|
|
410
434
|
elsif slot_definition[:renderable_function]
|
|
411
|
-
# Use `
|
|
435
|
+
# Use `bind_call(self)` to ensure lambda is executed in the context of the
|
|
412
436
|
# current component. This is necessary to allow the lambda to access helper
|
|
413
437
|
# methods like `content_tag` as well as parent component state.
|
|
414
|
-
renderable_function = slot_definition[:renderable_function]
|
|
438
|
+
renderable_function = slot_definition[:renderable_function]
|
|
415
439
|
renderable_value =
|
|
416
440
|
if block
|
|
417
|
-
renderable_function.
|
|
441
|
+
renderable_function.bind_call(self, *args, **kwargs) do |*rargs|
|
|
418
442
|
with_captured_virtual_path(captured_block_virtual_path) do
|
|
419
443
|
view_context.capture(*rargs, &block)
|
|
420
444
|
end
|
|
421
445
|
end
|
|
422
446
|
else
|
|
423
|
-
renderable_function.
|
|
447
|
+
renderable_function.bind_call(self, *args, **kwargs)
|
|
424
448
|
end
|
|
425
449
|
|
|
426
450
|
# Function calls can return components, so if it's a component handle it specially
|
|
@@ -35,9 +35,9 @@ module ViewComponent
|
|
|
35
35
|
:file
|
|
36
36
|
end
|
|
37
37
|
|
|
38
|
-
#
|
|
38
|
+
# Only reuse file contents while eager loading; otherwise reflect edits.
|
|
39
39
|
def source
|
|
40
|
-
|
|
40
|
+
CompileCache.read_file(@path)
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
private
|
|
@@ -123,14 +123,13 @@ module ViewComponent
|
|
|
123
123
|
@component.define_method(safe_method_name, @component.instance_method(@call_method_name))
|
|
124
124
|
end
|
|
125
125
|
|
|
126
|
-
def
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
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. " \
|
|
131
130
|
"The output will be automatically escaped, but you may want to investigate.")
|
|
132
131
|
end
|
|
133
|
-
|
|
132
|
+
RUBY
|
|
134
133
|
end
|
|
135
134
|
|
|
136
135
|
def defined_on_self?
|
|
@@ -138,6 +137,19 @@ module ViewComponent
|
|
|
138
137
|
end
|
|
139
138
|
end
|
|
140
139
|
|
|
140
|
+
class Inherited < InlineCall
|
|
141
|
+
def initialize(component:, template:)
|
|
142
|
+
super(component: component, method_name: template.call_method_name, defined_on_self: false)
|
|
143
|
+
@details = template.details
|
|
144
|
+
@path = template.path
|
|
145
|
+
@template = template
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def compile_to_component
|
|
149
|
+
@component.define_method(safe_method_name, @component.instance_method(@template.safe_method_name))
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
141
153
|
def compile_to_component
|
|
142
154
|
@component.silence_redefinition_of_method(call_method_name)
|
|
143
155
|
|
|
@@ -156,9 +168,13 @@ module ViewComponent
|
|
|
156
168
|
defined?(Coverage) && Coverage.running?
|
|
157
169
|
end
|
|
158
170
|
|
|
159
|
-
def
|
|
160
|
-
|
|
161
|
-
|
|
171
|
+
def safe_method_call
|
|
172
|
+
method_name = safe_method_name
|
|
173
|
+
if /\A[a-zA-Z_]\w*[!?]?\z/.match?(method_name)
|
|
174
|
+
method_name
|
|
175
|
+
else
|
|
176
|
+
"send(#{method_name.to_sym.inspect})"
|
|
177
|
+
end
|
|
162
178
|
end
|
|
163
179
|
|
|
164
180
|
def requires_compiled_superclass?
|
|
@@ -181,7 +197,7 @@ module ViewComponent
|
|
|
181
197
|
end
|
|
182
198
|
|
|
183
199
|
def safe_method_name
|
|
184
|
-
"_#{call_method_name}_#{@component.name.underscore.gsub("/", "__")}"
|
|
200
|
+
"_#{call_method_name}_#{format}_#{@component.name.underscore.gsub("/", "__")}"
|
|
185
201
|
end
|
|
186
202
|
|
|
187
203
|
def normalized_variant_name
|
|
@@ -208,14 +224,16 @@ module ViewComponent
|
|
|
208
224
|
|
|
209
225
|
def compiled_source
|
|
210
226
|
handler = details.handler_class
|
|
211
|
-
|
|
212
|
-
this_source
|
|
227
|
+
original_source = source
|
|
228
|
+
this_source = @component.strip_trailing_whitespace? ? original_source.rstrip : original_source
|
|
213
229
|
|
|
214
230
|
short_identifier = defined?(Rails.root) ? @path.sub("#{Rails.root}/", "") : @path
|
|
215
231
|
format = self.format || DEFAULT_FORMAT
|
|
216
232
|
type = ActionView::Template::Types[format]
|
|
217
233
|
|
|
218
|
-
handler.call(DataWithSource.new(format:, identifier: @path, short_identifier:, type:), this_source)
|
|
234
|
+
handler.call(DataWithSource.new(format:, identifier: @path, short_identifier:, type:), this_source).tap do |compiled|
|
|
235
|
+
CacheDigest.compiled(handler, original_source, compiled)
|
|
236
|
+
end
|
|
219
237
|
end
|
|
220
238
|
end
|
|
221
239
|
end
|