Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
22d9b3e8
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
22d9b3e8
编写于
2月 25, 2020
作者:
J
John Hawthorn
提交者:
GitHub
2月 25, 2020
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #38569 from jhawthorn/simplify_fixture_resolver
Simplify fixture resolver
上级
f0774fcb
c3c1c32f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
37 addition
and
45 deletion
+37
-45
actionview/lib/action_view/template/resolver.rb
actionview/lib/action_view/template/resolver.rb
+15
-3
actionview/lib/action_view/testing/resolvers.rb
actionview/lib/action_view/testing/resolvers.rb
+9
-26
actionview/test/template/lookup_context_test.rb
actionview/test/template/lookup_context_test.rb
+13
-16
未找到文件。
actionview/lib/action_view/template/resolver.rb
浏览文件 @
22d9b3e8
...
...
@@ -204,9 +204,13 @@ def query(path, details, formats, locals, cache:)
end
end
def
source_for_template
(
template
)
Template
::
Sources
::
File
.
new
(
template
)
end
def
build_unbound_template
(
template
,
virtual_path
)
handler
,
format
,
variant
=
extract_handler_and_format_and_variant
(
template
)
source
=
Template
::
Sources
::
File
.
new
(
template
)
source
=
source_for_template
(
template
)
UnboundTemplate
.
new
(
source
,
...
...
@@ -316,14 +320,22 @@ def initialize(path)
end
private
def
find_
template_paths_from_details
(
path
,
details
)
def
find_
candidate_template_paths
(
path
)
# Instead of checking for every possible path, as our other globs would
# do, scan the directory for files with the right prefix.
query
=
"
#{
escape_entry
(
File
.
join
(
@path
,
path
))
}
*"
Dir
[
query
].
reject
do
|
filename
|
File
.
directory?
(
filename
)
end
end
def
find_template_paths_from_details
(
path
,
details
)
candidates
=
find_candidate_template_paths
(
path
)
regex
=
build_regex
(
path
,
details
)
Dir
[
query
]
.
uniq
.
reject
do
|
filename
|
candidates
.
uniq
.
reject
do
|
filename
|
# This regex match does double duty of finding only files which match
# details (instead of just matching the prefix) and also filtering for
# case-insensitive file systems.
...
...
actionview/lib/action_view/testing/resolvers.rb
浏览文件 @
22d9b3e8
...
...
@@ -27,34 +27,17 @@ def to_s
end
private
def
query
(
path
,
exts
,
_
,
locals
,
cache
:)
regex
=
build_regex
(
path
,
exts
)
@hash
.
select
do
|
_path
,
_
|
(
"/"
+
_path
).
match?
(
regex
)
end
.
map
do
|
_path
,
source
|
handler
,
format
,
variant
=
extract_handler_and_format_and_variant
(
_path
)
Template
.
new
(
source
,
_path
,
handler
,
virtual_path:
path
.
virtual
,
format:
format
,
variant:
variant
,
locals:
locals
)
end
.
sort_by
do
|
t
|
match
=
(
"/"
+
t
.
identifier
).
match
(
regex
)
EXTENSIONS
.
keys
.
reverse
.
map
do
|
ext
|
if
ext
==
:variants
&&
exts
[
ext
]
==
:any
match
[
ext
].
nil?
?
0
:
1
elsif
match
[
ext
].
nil?
exts
[
ext
].
length
else
found
=
match
[
ext
].
to_sym
exts
[
ext
].
index
(
found
)
end
end
def
find_candidate_template_paths
(
path
)
@hash
.
keys
.
select
do
|
fixture
|
fixture
.
start_with?
(
path
.
virtual
)
end
.
map
do
|
fixture
|
"/
#{
fixture
}
"
end
end
def
source_for_template
(
template
)
@hash
[
template
[
1
..
template
.
size
]]
end
end
class
NullResolver
<
PathResolver
...
...
actionview/test/template/lookup_context_test.rb
浏览文件 @
22d9b3e8
...
...
@@ -190,33 +190,30 @@ def teardown
assert_equal
3
,
keys
.
uniq
.
size
end
test
"gives the key forward to the resolver, so it can be used as cache key"
do
@lookup_context
=
build_lookup_context
(
ActionView
::
FixtureResolver
.
new
(
"test/_foo.erb"
=>
"Foo"
),
{})
test
"uses details as part of cache key"
do
fixtures
=
{
"test/_foo.erb"
=>
"Foo"
,
"test/_foo.da.erb"
=>
"Bar"
,
}
@lookup_context
=
build_lookup_context
(
ActionView
::
FixtureResolver
.
new
(
fixtures
),
{})
template
=
@lookup_context
.
find
(
"foo"
,
%w(test)
,
true
)
original_template
=
template
assert_equal
"Foo"
,
template
.
source
# Now we are going to change the template, but it won't change the returned template
# since we will hit the cache.
@lookup_context
.
view_paths
.
first
.
data
[
"test/_foo.erb"
]
=
"Bar"
# We should get the same template
template
=
@lookup_context
.
find
(
"foo"
,
%w(test)
,
true
)
assert_
equal
"Foo"
,
template
.
sourc
e
assert_
same
original_template
,
templat
e
# This time we will change the locale. The updated template should be picked since
# lookup_context generated a new key after we changed the locale.
# Using a different locale we get a different view
@lookup_context
.
locale
=
:da
template
=
@lookup_context
.
find
(
"foo"
,
%w(test)
,
true
)
assert_equal
"Bar"
,
template
.
source
# Now we will change back the locale and it will still pick the old template.
# This is expected because lookup_context will reuse the previous key for :en locale.
# Using en we get the original view
@lookup_context
.
locale
=
:en
template
=
@lookup_context
.
find
(
"foo"
,
%w(test)
,
true
)
assert_equal
"Foo"
,
template
.
source
# Finally, we can expire the cache. And the expected template will be used.
@lookup_context
.
view_paths
.
first
.
clear_cache
template
=
@lookup_context
.
find
(
"foo"
,
%w(test)
,
true
)
assert_equal
"Bar"
,
template
.
source
assert_same
original_template
,
template
end
test
"can disable the cache on demand"
do
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录