Skip to content

Commit f6dc5be

Browse files
allan-piresjasonkimjoelhawksley
authored
Update filter in source_location to use base_label (#2009)
* Update filter in source_location to use base_label Co-authored-by: jasonkim <[email protected]> * Add more context to changelog and code comment * Update docs/CHANGELOG.md * Add allan-pires and jasonkim to contributors list --------- Co-authored-by: jasonkim <[email protected]> Co-authored-by: Joel Hawksley <[email protected]>
1 parent c33a422 commit f6dc5be

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

docs/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ nav_order: 5
1010

1111
## main
1212

13+
* Fix templates not being correctly populated when caller location label has a prefix.
14+
15+
On the upstream version of Ruby, method owners are now included in backtraces as prefixes. This caused the call stack filtering to not work as intended and thus `source_location` to be incorrect for child ViewComponents, consequently not populating templates correctly.
16+
17+
*Allan Pires, Jason Kim*
18+
1319
* Use component path for generating RSpec files.
1420

1521
When generating new RSpec files for components, the generator will use the `view_component_path` value in the config to decide where to put the new spec file. For instance, if the `view_component_path` option has been changed to `app/views/components`, the generator will put the spec file in `spec/views/components`. **If the `view_component_path` doesn't start with `app/`, then the generator will fall back to `spec/components/`.**

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ ViewComponent is built by over a hundred members of the community, including:
218218
<img src="https://avatars.githubusercontent.com/aduth?s=64" alt="aduth" width="32" />
219219
<img src="https://avatars.githubusercontent.com/htcarr3?s=64" alt="htcarr3" width="32" />
220220
<img src="https://avatars.githubusercontent.com/neanias?s=64" alt="neanias" width="32" />
221+
<img src="https://avatars.githubusercontent.com/allan-pires?s=64" alt="allan-pires" width="32" />
222+
<img src="https://avatars.githubusercontent.com/jasonkim?s=64" alt="jasonkim" width="32" />
221223

222224
## Who uses ViewComponent?
223225

lib/view_component/base.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,9 @@ def render_template_for(variant = nil)
537537
# Derive the source location of the component Ruby file from the call stack.
538538
# We need to ignore `inherited` frames here as they indicate that `inherited`
539539
# has been re-defined by the consuming application, likely in ApplicationComponent.
540-
child.source_location = caller_locations(1, 10).reject { |l| l.label == "inherited" }[0].path
540+
# We use `base_label` method here instead of `label` to avoid cases where the method
541+
# owner is included in a prefix like `ApplicationComponent.inherited`.
542+
child.source_location = caller_locations(1, 10).reject { |l| l.base_label == "inherited" }[0].path
541543

542544
# If Rails application is loaded, removes the first part of the path and the extension.
543545
if defined?(Rails) && Rails.application

0 commit comments

Comments
 (0)