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.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/app/views/test_mailer/test_multipart_email.html.erb +1 -0
  3. data/app/views/test_mailer/test_multipart_email.text.erb +1 -0
  4. data/docs/CHANGELOG.md +180 -0
  5. data/lib/generators/view_component/abstract_generator.rb +24 -4
  6. data/lib/generators/view_component/component/component_generator.rb +8 -6
  7. data/lib/generators/view_component/component/templates/component.rb.tt +1 -1
  8. data/lib/generators/view_component/erb/erb_generator.rb +2 -2
  9. data/lib/generators/view_component/haml/haml_generator.rb +1 -1
  10. data/lib/generators/view_component/locale/locale_generator.rb +2 -6
  11. data/lib/generators/view_component/preview/preview_generator.rb +9 -6
  12. data/lib/generators/view_component/preview/templates/component_preview.rb.tt +2 -2
  13. data/lib/generators/view_component/rspec/rspec_generator.rb +1 -1
  14. data/lib/generators/view_component/rspec/templates/component_spec.rb.tt +1 -1
  15. data/lib/generators/view_component/slim/slim_generator.rb +1 -1
  16. data/lib/generators/view_component/stimulus/stimulus_generator.rb +3 -7
  17. data/lib/generators/view_component/test_unit/templates/component_test.rb.tt +2 -2
  18. data/lib/generators/view_component/test_unit/test_unit_generator.rb +8 -7
  19. data/lib/view_component/base.rb +127 -101
  20. data/lib/view_component/cache_digest/dependency_tracking.rb +27 -11
  21. data/lib/view_component/cache_digest/digestor_lookup.rb +47 -1
  22. data/lib/view_component/cache_digest/resolver.rb +146 -52
  23. data/lib/view_component/cache_digest.rb +346 -56
  24. data/lib/view_component/cacheable.rb +16 -18
  25. data/lib/view_component/collection.rb +4 -9
  26. data/lib/view_component/compile_cache.rb +62 -2
  27. data/lib/view_component/compiler.rb +76 -13
  28. data/lib/view_component/config.rb +114 -8
  29. data/lib/view_component/engine.rb +10 -6
  30. data/lib/view_component/errors.rb +16 -0
  31. data/lib/view_component/request_details.rb +5 -10
  32. data/lib/view_component/slot.rb +12 -0
  33. data/lib/view_component/slotable.rb +32 -8
  34. data/lib/view_component/template.rb +33 -15
  35. data/lib/view_component/test_helpers.rb +40 -16
  36. data/lib/view_component/translatable.rb +35 -1
  37. data/lib/view_component/version.rb +1 -1
  38. metadata +17 -1
@@ -18,18 +18,26 @@ module ViewComponent
18
18
  # Extensions whose contents are hashed into the synthetic source.
19
19
  SIDECAR_EXTENSIONS = %w[yml yaml].freeze
20
20
 
21
+ RELATIVE_PARTIAL_PREFIX = "view_component/relative_partial/"
22
+ RELATIVE_DEPENDENCY = /<%# Relative Template Dependency: (\S+) %>/
23
+ private_constant :RELATIVE_PARTIAL_PREFIX, :RELATIVE_DEPENDENCY
24
+
25
+ # Where ViewComponent's own modules, such as the ones it includes into
26
+ # every component, are defined.
27
+ VIEW_COMPONENT_LIB = "#{::File.expand_path("..", __dir__)}/".freeze
28
+
21
29
  class << self
22
30
  def instance
23
31
  INSTANCE
24
32
  end
25
33
  end
26
34
 
27
- def find_templates(name, prefix, partial, details, locals = [])
35
+ def find_templates(name, prefix, partial, details, locals = [], view_paths: ActionController::Base.view_paths)
28
36
  virtual_path = [prefix.presence, name].compact.join("/")
29
37
  component = CacheDigest.component_for(virtual_path)
30
38
  return [] unless component
31
39
 
32
- [build_template(component, virtual_path, details)]
40
+ [build_template(component, virtual_path, details, view_paths)]
33
41
  end
34
42
 
35
43
  def to_s
@@ -49,30 +57,37 @@ module ViewComponent
49
57
  # @return [String]
50
58
  def source_for(component)
51
59
  parts = []
60
+ templates = template_sources(component)
52
61
 
53
62
  # Safety net: this template should never be rendered, only digested.
54
63
  parts << "<% raise ViewComponent::CacheDigestTemplateError.new(#{component.name.inspect}) %>"
55
64
 
56
65
  # Content hashes of the Ruby files and sidecar files backing the
57
- # component and its component superclasses. Hashing rather than
58
- # inlining keeps the source small and avoids embedding Ruby that a
59
- # tracker might misread as a render call.
66
+ # component, its component superclasses, and the modules they include.
67
+ # Hashing rather than inlining keeps the source small and avoids
68
+ # embedding Ruby that a tracker might misread as a render call.
60
69
  source_files(component).each do |path|
61
70
  parts << "<%# Resolved Dependency: #{path} #{file_digest(path)} %>"
62
71
  end
63
72
 
64
- # Everything the component renders from Ruby rather than from a
65
- # template: `# Template Dependency:` declarations, components rendered
66
- # from `#call` methods, and partials referenced by string path.
67
- # Re-emitted so the Digestor resolves them as tree nodes.
68
- ruby_dependencies(component).each do |dependency|
69
- parts << "<%# Template Dependency: #{dependency} %>"
73
+ # Everything the component renders: `# Template Dependency:`
74
+ # declarations, the components its slots are declared with, and the
75
+ # components and partials referenced from its templates, in any
76
+ # language, and from its Ruby. Emitted as declarations so the Digestor
77
+ # resolves them as tree nodes without trackers having to find them in
78
+ # the template sources, where they'd also infer partials from component
79
+ # renders.
80
+ dependencies(component, templates).each do |dependency|
81
+ parts << if dependency.start_with?(RELATIVE_PARTIAL_PREFIX)
82
+ "<%# Relative Template Dependency: #{dependency.delete_prefix(RELATIVE_PARTIAL_PREFIX)} %>"
83
+ else
84
+ "<%# Template Dependency: #{dependency} %>"
85
+ end
70
86
  end
71
87
 
72
- # Template sources verbatim, so trackers can discover the partials and
73
- # components they render.
74
- template_sources(component).each do |source|
75
- parts << source
88
+ # Content hashes of the templates, including inline ones.
89
+ templates.each do |source, _extension|
90
+ parts << "<%# Template Digest: #{ActiveSupport::Digest.hexdigest(source)} %>"
76
91
  end
77
92
 
78
93
  parts.join("\n")
@@ -80,9 +95,10 @@ module ViewComponent
80
95
 
81
96
  private
82
97
 
83
- def build_template(component, virtual_path, details)
98
+ def build_template(component, virtual_path, details, view_paths)
99
+ source = component.__vc_cache_digest_source || source_for(component)
84
100
  ActionView::Template.new(
85
- component.__vc_cache_digest_source || source_for(component),
101
+ resolve_relative_dependencies(source, view_paths),
86
102
  "view_component cache digest for #{component.name}",
87
103
  ActionView::Template.handler_for_extension(:erb),
88
104
  locals: [],
@@ -91,45 +107,87 @@ module ViewComponent
91
107
  )
92
108
  end
93
109
 
110
+ # A relative render uses the calling controller's prefixes. Track all
111
+ # matching partials so precomputed sources work for every controller.
112
+ def resolve_relative_dependencies(source, view_paths)
113
+ return source unless source.include?("Relative Template Dependency:")
114
+
115
+ paths = view_paths.flat_map(&:all_template_paths).uniq
116
+ source.gsub(RELATIVE_DEPENDENCY) do
117
+ name = Regexp.last_match(1)
118
+ matches = paths.filter_map do |path|
119
+ path.to_s if path.partial? && ::File.fnmatch?(name.delete_prefix("_"), path.name)
120
+ end.sort
121
+ matches = [name] if matches.empty?
122
+ matches.map { |path| "<%# Template Dependency: #{path} %>" }.join("\n")
123
+ end
124
+ end
125
+
94
126
  # The component and any component superclasses, nearest first. Including
95
127
  # ancestors means editing `ApplicationComponent` invalidates every
96
128
  # component that inherits from it.
97
129
  def component_ancestors(component)
98
- component.ancestors.select do |ancestor|
99
- ancestor.is_a?(Class) &&
100
- ancestor <= ViewComponent::Base &&
101
- ancestor != ViewComponent::Base
130
+ ancestors = []
131
+ until component == ViewComponent::Base
132
+ ancestors << component
133
+ component = component.superclass
102
134
  end
135
+ ancestors
136
+ end
137
+
138
+ # Modules the component and its component superclasses include, such as
139
+ # a module of helpers that render other components. Modules included
140
+ # into `ViewComponent::Base` and above are left out, as superclasses
141
+ # are, and so are the modules ViewComponent adds to every component.
142
+ #
143
+ # @return [Array<String>] the files the modules are defined in
144
+ def module_files(component)
145
+ component.ancestors.take_while { |ancestor| ancestor != ViewComponent::Base }.filter_map { |ancestor|
146
+ next if ancestor.is_a?(Class) || ancestor.name.nil?
147
+
148
+ begin
149
+ path, _line = Object.const_source_location(ancestor.name)
150
+ rescue NameError
151
+ next
152
+ end
153
+ path unless path.nil? || path.start_with?(VIEW_COMPONENT_LIB)
154
+ }.uniq
103
155
  end
104
156
 
105
157
  def source_files(component)
106
- component_ancestors(component).flat_map { |ancestor|
107
- [ancestor.identifier, *ancestor.sidecar_files(SIDECAR_EXTENSIONS)]
108
- }.compact.uniq.select { |path| ::File.exist?(path) }
158
+ sidecars = component_ancestors(component).flat_map { |ancestor| ancestor.__vc_sidecar_files(SIDECAR_EXTENSIONS) }
159
+ (ruby_files(component) + sidecars).uniq.select { |path| ::File.exist?(path) }
109
160
  end
110
161
 
111
- def template_files(component)
112
- component_ancestors(component)
113
- .flat_map { |ancestor| ancestor.sidecar_files(ActionView::Template::Handlers.extensions) }
114
- .uniq
115
- .select { |path| ::File.exist?(path) }
162
+ def ruby_files(component)
163
+ CacheDigest.memoize([:ruby_files, component]) do
164
+ component_ancestors(component).map(&:identifier).concat(module_files(component))
165
+ .compact.uniq.select { |path| ::File.exist?(path) }
166
+ end
116
167
  end
117
168
 
118
169
  # Sidecar template files plus inline templates, which live in the Ruby
119
170
  # file and so are invisible to Action View's trackers.
171
+ #
172
+ # @return [Array<Array(String, String, Class)>] source, extension, and defining component
120
173
  def template_sources(component)
121
- sources = template_files(component).map { |path| ::File.read(path) }
122
-
123
- component_ancestors(component).each do |ancestor|
174
+ component_ancestors(component).flat_map do |ancestor|
175
+ sources = ancestor.__vc_sidecar_files(ActionView::Template::Handlers.extensions).filter_map do |path|
176
+ [CompileCache.read_file(path), ::File.extname(path).delete_prefix("."), ancestor] if ::File.exist?(path)
177
+ end
124
178
  inline_template = ancestor.__vc_inline_template
125
- sources << inline_template.source if inline_template
126
- end
179
+ sources << [inline_template.source, inline_template.language.to_s, ancestor] if inline_template
127
180
 
128
- sources.uniq
181
+ sources
182
+ end.uniq
129
183
  end
130
184
 
131
- def explicit_dependencies(component)
132
- ruby_sources(component).flat_map { |source|
185
+ def explicit_dependencies(sources)
186
+ sources.flat_map { |source|
187
+ # A cheap check first: most sources declare nothing, and the
188
+ # pattern is slow to scan for in sources with many comments.
189
+ next [] unless source.include?("Template Dependency:")
190
+
133
191
  declared = source.scan(CacheDigest::EXPLICIT_DEPENDENCY).flatten
134
192
 
135
193
  # Component class names are translated to the path they're digested
@@ -142,29 +200,65 @@ module ViewComponent
142
200
  }.uniq
143
201
  end
144
202
 
145
- # Everything a component renders from Ruby code rather than from a
146
- # template. Action View's trackers only read templates, so a `#call`
147
- # method that renders another component or a partial would otherwise go
148
- # unnoticed.
149
- def ruby_dependencies(component)
150
- virtual_path = CacheDigest.virtual_path_for(component)
203
+ # Everything but the component and its superclasses, which a component
204
+ # references in its own source but are digested as part of it already.
205
+ def dependencies(component, templates)
206
+ virtual_path = "#{RELATIVE_PARTIAL_PREFIX}template"
207
+ ruby = ruby_sources(component)
208
+ own_paths = component_ancestors(component).filter_map { |ancestor| CacheDigest.virtual_path_for(ancestor) }
209
+
210
+ dependencies = explicit_dependencies(ruby + templates.map(&:first)) |
211
+ ruby.flat_map { |source| ruby_dependencies(source, virtual_path) } |
212
+ CacheDigest.slot_renders_of(component) |
213
+ templates.flat_map { |source, extension, owner| template_dependencies(owner, source, extension, virtual_path) }
151
214
 
152
- explicit_dependencies(component) |
153
- ruby_sources(component).flat_map { |source|
154
- CacheDigest.component_paths_in(source) |
155
- CacheDigest.partial_paths_in(source, virtual_path)
156
- }.uniq
215
+ dependencies - own_paths
157
216
  end
158
217
 
218
+ # Components and partials rendered from Ruby, such as a `#call` method.
219
+ def ruby_dependencies(source, virtual_path)
220
+ CacheDigest.renders_in(source, virtual_path, view_paths: ActionController::Base.view_paths).flatten
221
+ end
222
+
223
+ def template_dependencies(component, source, extension, virtual_path)
224
+ handler = ActionView::Template.handler_for_extension(extension)
225
+ return [] unless CacheDigest.template_dependencies?(source, handler)
226
+
227
+ template = ActionView::Template.new(
228
+ source,
229
+ "view_component cache digest for #{component.name}",
230
+ handler,
231
+ locals: [],
232
+ format: nil,
233
+ virtual_path: virtual_path
234
+ )
235
+ CacheDigest.renders_in(
236
+ CacheDigest.compiled_source(template), virtual_path,
237
+ view_paths: ActionController::Base.view_paths, nesting: [component]
238
+ ).flatten
239
+ end
240
+
241
+ # Components loaded from encrypted files, as some commercial gems ship
242
+ # them, have binary identifiers that can't be scanned. They're still
243
+ # hashed by `file_digest`.
159
244
  def ruby_sources(component)
160
- component_ancestors(component).filter_map { |ancestor|
161
- path = ancestor.identifier
162
- ::File.read(path) if path && ::File.exist?(path)
245
+ ruby_files(component).filter_map { |path|
246
+ source = CompileCache.read_file(path)
247
+ source if source.valid_encoding?
163
248
  }
164
249
  end
165
250
 
166
251
  def file_digest(path)
167
- ActiveSupport::Digest.hexdigest(::File.read(path))
252
+ CacheDigest.memoize([:file_digest, path]) do
253
+ # Reuse source reads unless text mode can transcode or translate newlines.
254
+ source = if ::File::ALT_SEPARATOR.nil? &&
255
+ (Encoding.default_internal.nil? || Encoding.default_internal == Encoding.default_external)
256
+ CompileCache.read_file(path)
257
+ else
258
+ ::File.binread(path)
259
+ end
260
+ ActiveSupport::Digest.hexdigest(source)
261
+ end
168
262
  end
169
263
 
170
264
  # Built once at load time rather than memoized, so no class-level state