1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Avoid splats in LookupContext and PathSet

Though this ends up with a bit more repetition, I believe it's a little
clearer and is also a decent amount faster.
This commit is contained in:
John Hawthorn 2021-07-13 14:09:36 -07:00
parent 6c67e27bd2
commit d4ca317993
3 changed files with 26 additions and 27 deletions

View file

@ -122,21 +122,29 @@ module ActionView
attr_reader :view_paths, :html_fallback_for_js
def find(name, prefixes = [], partial = false, keys = [], options = {})
@view_paths.find(*args_for_lookup(name, prefixes, partial, keys, options))
name, prefixes = normalize_name(name, prefixes)
details, details_key = detail_args_for(options)
@view_paths.find(name, prefixes, partial, details, details_key, keys)
end
alias :find_template :find
def find_all(name, prefixes = [], partial = false, keys = [], options = {})
@view_paths.find_all(*args_for_lookup(name, prefixes, partial, keys, options))
name, prefixes = normalize_name(name, prefixes)
details, details_key = detail_args_for(options)
@view_paths.find_all(name, prefixes, partial, details, details_key, keys)
end
def exists?(name, prefixes = [], partial = false, keys = [], **options)
@view_paths.exists?(*args_for_lookup(name, prefixes, partial, keys, options))
name, prefixes = normalize_name(name, prefixes)
details, details_key = detail_args_for(options)
@view_paths.exists?(name, prefixes, partial, details, details_key, keys)
end
alias :template_exists? :exists?
def any?(name, prefixes = [], partial = false)
@view_paths.exists?(*args_for_any(name, prefixes, partial))
name, prefixes = normalize_name(name, prefixes)
details, details_key = detail_args_for_any
@view_paths.exists?(name, prefixes, partial, details, details_key, [])
end
alias :any_templates? :any?
@ -147,12 +155,6 @@ module ActionView
ActionView::PathSet.new(Array(paths))
end
def args_for_lookup(name, prefixes, partial, keys, details_options)
name, prefixes = normalize_name(name, prefixes)
details, details_key = detail_args_for(details_options)
[name, prefixes, partial || false, details, details_key, keys]
end
# Compute details hash and key according to user options (e.g. passed from #render).
def detail_args_for(options) # :doc:
return @details, details_key if options.empty? # most common path.
@ -167,12 +169,6 @@ module ActionView
[user_details, details_key]
end
def args_for_any(name, prefixes, partial)
name, prefixes = normalize_name(name, prefixes)
details, details_key = detail_args_for_any
[name, prefixes, partial || false, details, details_key]
end
def detail_args_for_any
@detail_args_for_any ||= begin
details = {}

View file

@ -44,28 +44,31 @@ module ActionView #:nodoc:
METHOD
end
def find(*args)
find_all(*args).first || raise(MissingTemplate.new(self, *args))
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))
end
def find_all(path, prefixes = [], *args)
_find_all path, prefixes, args
def find_all(path, prefixes, partial, details, details_key, locals)
search_combinations(prefixes) do |resolver, prefix|
templates = resolver.find_all(path, prefix, partial, details, details_key, locals)
return templates unless templates.empty?
end
[]
end
def exists?(path, prefixes, *args)
find_all(path, prefixes, *args).any?
def exists?(path, prefixes, partial, details, details_key, locals)
find_all(path, prefixes, partial, details, details_key, locals).any?
end
private
def _find_all(path, prefixes, args)
def search_combinations(prefixes)
prefixes = Array(prefixes)
prefixes.each do |prefix|
paths.each do |resolver|
templates = resolver.find_all(path, prefix, *args)
return templates unless templates.empty?
yield resolver, prefix
end
end
[]
end
def typecast(paths)

View file

@ -221,7 +221,7 @@ class TestMissingTemplate < ActiveSupport::TestCase
test "if a single prefix is passed as a string and the lookup fails, MissingTemplate accepts it" do
e = assert_raise ActionView::MissingTemplate do
details = { handlers: [], formats: [], variants: [], locale: [] }
@lookup_context.view_paths.find("foo", "parent", true, details)
@lookup_context.view_paths.find("foo", "parent", true, details, nil, [])
end
assert_match %r{Missing partial parent/_foo with .*\n\nSearched in:\n \* "/Path/to/views"\n}, e.message
end