Skip to content

Commit 756a322

Browse files
authored
Merge pull request #2362 from ViewComponent/2359-virtual-path-clobber
Fix bug where virtual path was not reset, breaking translations outside of components
2 parents 3f7153b + e1d7a26 commit 756a322

File tree

9 files changed

+36
-2
lines changed

9 files changed

+36
-2
lines changed

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ namespace :docs do
131131
end
132132

133133
task :all_tests do
134+
ENV["RAILS_ENV"] = "test"
135+
134136
if ENV["MEASURE_COVERAGE"]
135137
SimpleCov.start do
136138
command_name "rails#{Rails::VERSION::STRING}-ruby#{RUBY_VERSION}"

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 wasn't 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: 4 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
@@ -140,6 +141,8 @@ def render_in(view_context, &block)
140141
value = nil
141142

142143
@output_buffer.with_buffer do
144+
@view_context.instance_variable_set(:@virtual_path, virtual_path)
145+
143146
rendered_template = render_template_for(@__vc_requested_details).to_s
144147

145148
# Avoid allocating new string when output_preamble and output_postamble are blank
@@ -160,6 +163,7 @@ def render_in(view_context, &block)
160163
""
161164
end
162165
ensure
166+
view_context.instance_variable_set(:@virtual_path, old_virtual_path)
163167
@current_template = old_current_template
164168
end
165169

lib/view_component/template.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ def compile_to_component
9898
@component.silence_redefinition_of_method(call_method_name)
9999

100100
# rubocop:disable Style/EvalWithLocation
101-
@component.class_eval <<~RUBY, @path, @lineno - 1
101+
@component.class_eval <<~RUBY, @path, @lineno
102102
def #{call_method_name}
103-
@view_context.instance_variable_set(:@virtual_path, virtual_path)
104103
#{compiled_source}
105104
end
106105
RUBY
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)