Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
0f62e3ec
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,发现更多精彩内容 >>
提交
0f62e3ec
编写于
7月 08, 2015
作者:
K
Kasper Timm Hansen
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #20781 from dubek/collection-cache-explicit
Support explicit defintion of resource name for collection caching.
上级
a41698bc
5a41d004
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
47 addition
and
9 deletion
+47
-9
actionview/CHANGELOG.md
actionview/CHANGELOG.md
+5
-0
actionview/lib/action_view/helpers/cache_helper.rb
actionview/lib/action_view/helpers/cache_helper.rb
+15
-0
actionview/lib/action_view/template.rb
actionview/lib/action_view/template.rb
+13
-4
actionview/lib/action_view/template/handlers/erb.rb
actionview/lib/action_view/template/handlers/erb.rb
+9
-2
actionview/test/template/template_test.rb
actionview/test/template/template_test.rb
+5
-3
未找到文件。
actionview/CHANGELOG.md
浏览文件 @
0f62e3ec
*
Allow defining explicit collection caching using a
`# Template Collection: ...`
directive inside templates.
*Dov Murik*
*
Asset helpers raise
`ArgumentError`
when
`nil`
is passed as a source.
*Anton Kolomiychuk*
...
...
actionview/lib/action_view/helpers/cache_helper.rb
浏览文件 @
0f62e3ec
...
...
@@ -137,6 +137,21 @@ module CacheHelper
# The automatic cache multi read can be turned off like so:
#
# <%= render @notifications, cache: false %>
#
# === Explicit Collection Caching
#
# If the partial template doesn't start with a clean cache call as
# mentioned above, you can still benefit from collection caching by
# adding a special comment format anywhere in the template, like:
#
# <%# Template Collection: notification %>
# <% my_helper_that_calls_cache(some_arg, notification) do %>
# <%= notification.name %>
# <% end %>
#
# The pattern used to match these is <tt>/# Template Collection: (\S+)/</tt>,
# so it's important that you type it out just so.
# You can only declare one collection in a partial template file.
def
cache
(
name
=
{},
options
=
{},
&
block
)
if
controller
.
respond_to?
(
:perform_caching
)
&&
controller
.
perform_caching
safe_concat
(
fragment_for
(
cache_fragment_name
(
name
,
options
),
options
,
&
block
))
...
...
actionview/lib/action_view/template.rb
浏览文件 @
0f62e3ec
...
...
@@ -130,7 +130,7 @@ def initialize(source, identifier, handler, details)
@source
=
source
@identifier
=
identifier
@handler
=
handler
@cache_name
=
extract_resource_cache_
call_
name
@cache_name
=
extract_resource_cache_name
@compiled
=
false
@original_encoding
=
nil
@locals
=
details
[
:locals
]
||
[]
...
...
@@ -351,9 +351,18 @@ def instrument(action, &block)
ActiveSupport
::
Notifications
.
instrument
(
"
#{
action
}
.action_view"
,
payload
,
&
block
)
end
def
extract_resource_cache_call_name
$1
if
@handler
.
respond_to?
(
:resource_cache_call_pattern
)
&&
@source
=~
@handler
.
resource_cache_call_pattern
EXPLICIT_COLLECTION
=
/# Template Collection: (?<resource_name>\w+)/
def
extract_resource_cache_name
if
match
=
@source
.
match
(
EXPLICIT_COLLECTION
)
||
resource_cache_call_match
match
[
:resource_name
]
end
end
def
resource_cache_call_match
if
@handler
.
respond_to?
(
:resource_cache_call_pattern
)
@source
.
match
(
@handler
.
resource_cache_call_pattern
)
end
end
def
inferred_cache_name
...
...
actionview/lib/action_view/template/handlers/erb.rb
浏览文件 @
0f62e3ec
...
...
@@ -125,7 +125,7 @@ def call(template)
# Returns Regexp to extract a cached resource's name from a cache call at the
# first line of a template.
# The extracted cache name is
expected in $1
.
# The extracted cache name is
captured as :resource_name
.
#
# <% cache notification do %> # => notification
#
...
...
@@ -138,7 +138,14 @@ def call(template)
#
# <% cache notification.event do %> # => nil
def
resource_cache_call_pattern
/\A(?:<%#.*%>)*\s*<%\s*cache\(?\s*(\w+)[\s\)]/m
/\A
(?:<%\#.*%>)* # optional initial comment
\s* # followed by optional spaces or newlines
<%\s*cache[\(\s] # followed by an ERB call to cache
\s* # followed by optional spaces or newlines
(?<resource_name>\w+) # capture the cache call argument as :resource_name
[\s\)] # followed by a space or close paren
/xm
end
private
...
...
actionview/test/template/template_test.rb
浏览文件 @
0f62e3ec
...
...
@@ -194,14 +194,15 @@ def test_not_eligible_for_collection_caching_without_cache_call
[
"<%= 'Hello' %>"
,
"<% cache_customer = 42 %>"
,
"<% cache customer.name do %><% end %>"
"<% cache customer.name do %><% end %>"
,
"<% my_cache customer do %><% end %>"
].
each
do
|
body
|
template
=
new_template
(
body
,
virtual_path:
"test/foo/_customer"
)
assert_not
template
.
eligible_for_collection_caching?
,
"Template
#{
body
.
inspect
}
should not be eligible for collection caching"
end
end
def
test_eligible_for_collection_caching_with_cache_call
def
test_eligible_for_collection_caching_with_cache_call
_or_explicit
[
"<% cache customer do %><% end %>"
,
"<% cache(customer) do %><% end %>"
,
...
...
@@ -213,7 +214,8 @@ def test_eligible_for_collection_caching_with_cache_call
"<%# comment %><% cache customer do %><% end %>"
,
"<%# comment %>
\n
<% cache customer do %><% end %>"
,
"<%# comment
\n
line 2
\n
line 3 %>
\n
<% cache customer do %><% end %>"
,
"<%# comment 1 %>
\n
<%# comment 2 %>
\n
<% cache customer do %><% end %>"
"<%# comment 1 %>
\n
<%# comment 2 %>
\n
<% cache customer do %><% end %>"
,
"<%# comment 1 %>
\n
<%# Template Collection: customer %>
\n
<% my_cache customer do %><% end %>"
].
each
do
|
body
|
template
=
new_template
(
body
,
virtual_path:
"test/foo/_customer"
)
assert
template
.
eligible_for_collection_caching?
,
"Template
#{
body
.
inspect
}
should be eligible for collection caching"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录