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:
parent
6c67e27bd2
commit
d4ca317993
3 changed files with 26 additions and 27 deletions
|
@ -122,21 +122,29 @@ module ActionView
|
||||||
attr_reader :view_paths, :html_fallback_for_js
|
attr_reader :view_paths, :html_fallback_for_js
|
||||||
|
|
||||||
def find(name, prefixes = [], partial = false, keys = [], options = {})
|
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
|
end
|
||||||
alias :find_template :find
|
alias :find_template :find
|
||||||
|
|
||||||
def find_all(name, prefixes = [], partial = false, keys = [], options = {})
|
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
|
end
|
||||||
|
|
||||||
def exists?(name, prefixes = [], partial = false, keys = [], **options)
|
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
|
end
|
||||||
alias :template_exists? :exists?
|
alias :template_exists? :exists?
|
||||||
|
|
||||||
def any?(name, prefixes = [], partial = false)
|
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
|
end
|
||||||
alias :any_templates? :any?
|
alias :any_templates? :any?
|
||||||
|
|
||||||
|
@ -147,12 +155,6 @@ module ActionView
|
||||||
ActionView::PathSet.new(Array(paths))
|
ActionView::PathSet.new(Array(paths))
|
||||||
end
|
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).
|
# Compute details hash and key according to user options (e.g. passed from #render).
|
||||||
def detail_args_for(options) # :doc:
|
def detail_args_for(options) # :doc:
|
||||||
return @details, details_key if options.empty? # most common path.
|
return @details, details_key if options.empty? # most common path.
|
||||||
|
@ -167,12 +169,6 @@ module ActionView
|
||||||
[user_details, details_key]
|
[user_details, details_key]
|
||||||
end
|
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
|
def detail_args_for_any
|
||||||
@detail_args_for_any ||= begin
|
@detail_args_for_any ||= begin
|
||||||
details = {}
|
details = {}
|
||||||
|
|
|
@ -44,28 +44,31 @@ module ActionView #:nodoc:
|
||||||
METHOD
|
METHOD
|
||||||
end
|
end
|
||||||
|
|
||||||
def find(*args)
|
def find(path, prefixes, partial, details, details_key, locals)
|
||||||
find_all(*args).first || raise(MissingTemplate.new(self, *args))
|
find_all(path, prefixes, partial, details, details_key, locals).first ||
|
||||||
|
raise(MissingTemplate.new(self, path, prefixes, partial, details, details_key, locals))
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_all(path, prefixes = [], *args)
|
def find_all(path, prefixes, partial, details, details_key, locals)
|
||||||
_find_all path, prefixes, args
|
search_combinations(prefixes) do |resolver, prefix|
|
||||||
|
templates = resolver.find_all(path, prefix, partial, details, details_key, locals)
|
||||||
|
return templates unless templates.empty?
|
||||||
|
end
|
||||||
|
[]
|
||||||
end
|
end
|
||||||
|
|
||||||
def exists?(path, prefixes, *args)
|
def exists?(path, prefixes, partial, details, details_key, locals)
|
||||||
find_all(path, prefixes, *args).any?
|
find_all(path, prefixes, partial, details, details_key, locals).any?
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def _find_all(path, prefixes, args)
|
def search_combinations(prefixes)
|
||||||
prefixes = Array(prefixes)
|
prefixes = Array(prefixes)
|
||||||
prefixes.each do |prefix|
|
prefixes.each do |prefix|
|
||||||
paths.each do |resolver|
|
paths.each do |resolver|
|
||||||
templates = resolver.find_all(path, prefix, *args)
|
yield resolver, prefix
|
||||||
return templates unless templates.empty?
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
[]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def typecast(paths)
|
def typecast(paths)
|
||||||
|
|
|
@ -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
|
test "if a single prefix is passed as a string and the lookup fails, MissingTemplate accepts it" do
|
||||||
e = assert_raise ActionView::MissingTemplate do
|
e = assert_raise ActionView::MissingTemplate do
|
||||||
details = { handlers: [], formats: [], variants: [], locale: [] }
|
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
|
end
|
||||||
assert_match %r{Missing partial parent/_foo with .*\n\nSearched in:\n \* "/Path/to/views"\n}, e.message
|
assert_match %r{Missing partial parent/_foo with .*\n\nSearched in:\n \* "/Path/to/views"\n}, e.message
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue