Make ActionView::PathSet immutable

Previously we would mutate the PathSet in order to implement
{append,prepend}_view_path.

This removes the mutation methods we had and instead constructs a new
PathSet whenever we are modifying it. This should allow flexibility in
the classes holding a view_path to know that their view paths isn't
modified (for example, to allow caching).
This commit is contained in:
John Hawthorn 2022-09-06 18:36:17 -07:00
parent e65d41a47a
commit 0371317e25
4 changed files with 16 additions and 16 deletions

View File

@ -148,6 +148,14 @@ module ActionView
end
alias :any_templates? :any?
def append_view_paths(paths)
@view_paths = build_view_paths(@view_paths.to_a + paths)
end
def prepend_view_paths(paths)
@view_paths = build_view_paths(paths + @view_paths.to_a)
end
private
# Whenever setting view paths, makes a copy so that we can manipulate them in
# instance objects as we wish.

View File

@ -13,14 +13,14 @@ module ActionView # :nodoc:
attr_reader :paths
delegate :[], :include?, :pop, :size, :each, to: :paths
delegate :[], :include?, :size, :each, to: :paths
def initialize(paths = [])
@paths = typecast paths
@paths = typecast(paths).freeze
end
def initialize_copy(other)
@paths = other.paths.dup
@paths = other.paths.dup.freeze
self
end
@ -36,14 +36,6 @@ module ActionView # :nodoc:
PathSet.new(paths + array)
end
%w(<< concat push insert unshift).each do |method|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{method}(*args)
paths.#{method}(*typecast(args))
end
METHOD
end
def find(path, prefixes, partial, details, details_key, locals)
find_all(path, prefixes, partial, details, details_key, locals).first ||
raise(MissingTemplate.new(self, path, prefixes, partial, details, details_key, locals))

View File

@ -35,7 +35,7 @@ module ActionView
# the default view path. You may also provide a custom view path
# (see ActionView::PathSet for more information)
def append_view_path(path)
self._view_paths = view_paths + Array(path)
self._view_paths = ActionView::PathSet.new(view_paths.to_a + Array(path))
end
# Prepend a path to the list of view paths for this controller.
@ -45,7 +45,7 @@ module ActionView
# the default view path. You may also provide a custom view path
# (see ActionView::PathSet for more information)
def prepend_view_path(path)
self._view_paths = ActionView::PathSet.new(Array(path) + view_paths)
self._view_paths = ActionView::PathSet.new(Array(path) + view_paths.to_a)
end
# A list of all of the default view paths for this controller.
@ -110,7 +110,7 @@ module ActionView
# the default view path. You may also provide a custom view path
# (see ActionView::PathSet for more information)
def append_view_path(path)
lookup_context.view_paths.push(*path)
lookup_context.append_view_paths(Array(path))
end
# Prepend a path to the list of view paths for the current LookupContext.
@ -120,7 +120,7 @@ module ActionView
# the default view path. You may also provide a custom view path
# (see ActionView::PathSet for more information)
def prepend_view_path(path)
lookup_context.view_paths.unshift(*path)
lookup_context.prepend_view_paths(Array(path))
end
end
end

View File

@ -155,7 +155,7 @@ class ViewLoadPathsTest < ActionController::TestCase
end
decorator = decorator_class.new(TestController.view_paths)
TestController.view_paths = ActionView::PathSet.new.push(decorator)
TestController.view_paths = ActionView::PathSet.new([decorator])
get :hello_world
assert_response :success