Skip to content

Commit

Permalink
Remove redundant @virtual_path variable
Browse files Browse the repository at this point in the history
Following the introduction of the @current_template variable in 1581cab,
the @virtual_path variable is now redundant, as the value of the virtual
path may be accessed via @current_template.virtual_path. This commit
removes @virtual_path and replaces any references to @virtual_path with
@current_template.virtual_path.
  • Loading branch information
alipman88 committed May 26, 2020
1 parent 4671fe2 commit dd7a673
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 13 deletions.
4 changes: 2 additions & 2 deletions actionview/lib/action_view/base.rb
Expand Up @@ -270,12 +270,12 @@ def initialize(lookup_context = nil, assigns = {}, controller = nil, formats = N
end

def _run(method, template, locals, buffer, add_to_stack: true, &block)
_old_output_buffer, _old_virtual_path, _old_template = @output_buffer, @virtual_path, @current_template
_old_output_buffer, _old_template = @output_buffer, @current_template
@current_template = template if add_to_stack
@output_buffer = buffer
send(method, locals, buffer, &block)
ensure
@output_buffer, @virtual_path, @current_template = _old_output_buffer, _old_virtual_path, _old_template
@output_buffer, @current_template = _old_output_buffer, _old_template
end

def compiled_method_container
Expand Down
1 change: 0 additions & 1 deletion actionview/lib/action_view/context.rb
Expand Up @@ -18,7 +18,6 @@ module Context
def _prepare_context
@view_flow = OutputFlow.new
@output_buffer = nil
@virtual_path = nil
end

# Encapsulates the interaction with the view flow so it
Expand Down
6 changes: 3 additions & 3 deletions actionview/lib/action_view/helpers/translation_helper.rb
Expand Up @@ -124,10 +124,10 @@ def localize(object, **options)
def scope_key_by_partial(key)
stringified_key = key.to_s
if stringified_key.start_with?(".")
if @virtual_path
if @current_template&.virtual_path
@_scope_key_by_partial_cache ||= {}
@_scope_key_by_partial_cache[@virtual_path] ||= @virtual_path.gsub(%r{/_?}, ".")
"#{@_scope_key_by_partial_cache[@virtual_path]}#{stringified_key}"
@_scope_key_by_partial_cache[@current_template.virtual_path] ||= @current_template.virtual_path.gsub(%r{/_?}, ".")
"#{@_scope_key_by_partial_cache[@current_template.virtual_path]}#{stringified_key}"
else
raise "Cannot use t(#{key.inspect}) shortcut because path is not available"
end
Expand Down
2 changes: 0 additions & 2 deletions actionview/test/abstract_unit.rb
Expand Up @@ -59,8 +59,6 @@ def view
end

def render_erb(string)
@virtual_path = nil

template = ActionView::Template.new(
string.strip,
"test template",
Expand Down
1 change: 0 additions & 1 deletion actionview/test/activerecord/relation_cache_test.rb
Expand Up @@ -10,7 +10,6 @@ def setup
view_paths = ActionController::Base.view_paths
lookup_context = ActionView::LookupContext.new(view_paths, {}, ["test"])
@view_renderer = ActionView::Renderer.new(lookup_context)
@virtual_path = "path"
@current_template = lookup_context.find "test/hello_world"

controller.cache_store = ActiveSupport::Cache::MemoryStore.new
Expand Down
7 changes: 3 additions & 4 deletions actionview/test/template/template_test.rb
Expand Up @@ -22,7 +22,6 @@ class Context < ActionView::Base
def initialize(*)
super
@output_buffer = "original"
@virtual_path = nil
end

def hello
Expand All @@ -35,7 +34,7 @@ def apostrophe

def partial
ActionView::Template.new(
"<%= @virtual_path %>",
"<%= @current_template.virtual_path %>",
"partial",
ERBHandler,
virtual_path: "partial",
Expand Down Expand Up @@ -115,9 +114,9 @@ def test_restores_buffer
end

def test_virtual_path
@template = new_template("<%= @virtual_path %>" \
@template = new_template("<%= @current_template.virtual_path %>" \
"<%= partial.render(self, {}) %>" \
"<%= @virtual_path %>")
"<%= @current_template.virtual_path %>")
assert_equal "hellopartialhello", render
end

Expand Down

0 comments on commit dd7a673

Please sign in to comment.