Skip to content

Commit 212ef25

Browse files
joelhawksleymyabc
andcommitted
Fix bug where virtual path was not reset, breaking translations outside of components.
Closes: #2359 Co-authored-by: Alex Coles <alex@alexbcoles.com>
1 parent 4098dc9 commit 212ef25

7 files changed

Lines changed: 31 additions & 0 deletions

File tree

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ nav_order: 6
1818

1919
*Meyric Rawlings*, *Joel Hawksley*
2020

21+
* Fix bug where virtual path was not reset, breaking translations outside of components.
22+
23+
*Alex Coles*, *Joel Hawksley*
24+
2125
## 4.0.0.alpha7
2226

2327
* BREAKING: Remove deprecated `use_helper(s)`. Use `include MyHelper` or `helpers.` proxy instead.

lib/view_component/base.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def render_in(view_context, &block)
108108
self.class.__vc_compile(raise_errors: true)
109109

110110
@view_context = view_context
111+
old_virtual_path = view_context.instance_variable_get(:@virtual_path)
111112
self.__vc_original_view_context ||= view_context
112113

113114
@output_buffer = view_context.output_buffer
@@ -160,6 +161,7 @@ def render_in(view_context, &block)
160161
""
161162
end
162163
ensure
164+
view_context.instance_variable_set(:@virtual_path, old_virtual_path)
163165
@current_template = old_current_template
164166
end
165167

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<p>Virtual path: <%= @virtual_path %></p>
2+
<p id="before"><%= t('.message') %></p>
3+
4+
<%= render MyComponent.new %>
5+
6+
<p>Virtual path: <%= @virtual_path %></p>
7+
<p id="after"><%= t('.message') %></p>

test/sandbox/config/locales/en.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ en:
1515

1616
editorb_component:
1717
title: Editorb!
18+
19+
integration_examples:
20+
virtual_path_reset:
21+
message: "Hello world!"
22+
23+
my_component:
24+
message: "OH NO! (you shouldn't see me)"

test/sandbox/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
get :unsafe_postamble_component, to: "integration_examples#unsafe_postamble_component"
3434
get :multiple_formats_component, to: "integration_examples#multiple_formats_component"
3535
get :slotable_default_override, to: "integration_examples#slotable_default_override"
36+
get :virtual_path_reset, to: "integration_examples#virtual_path_reset"
3637
post :create, to: "integration_examples#create"
3738
get :turbo_content_type, to: "integration_examples#turbo_content_type"
3839
post :submit, to: "integration_examples#submit"

test/sandbox/test/integration_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,4 +767,11 @@ def test_modifying_previews_reflected_on_reload
767767
get "/rails/view_components/preview_component/default"
768768
assert_select "h1", text: "Lorem Ipsum"
769769
end
770+
771+
def test_virtual_path_reset
772+
get "/virtual_path_reset"
773+
774+
assert_select "#before", text: "Hello world!"
775+
assert_select "#after", text: "Hello world!"
776+
end
770777
end

test/sandbox/test/translatable_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ def test_inheriting_and_overriding_translations
184184
def translate(key, **options)
185185
component = TranslatableComponent.new
186186
render_inline(component)
187+
component.
188+
instance_variable_get(:@view_context).
189+
instance_variable_set(:@virtual_path, component.virtual_path)
187190
component.translate(key, **options)
188191
end
189192
end

0 commit comments

Comments
 (0)