diff options
author | Dov Murik <dov.murik@gmail.com> | 2015-07-04 22:34:00 -0400 |
---|---|---|
committer | Dov Murik <dov.murik@gmail.com> | 2015-07-07 16:57:09 -0400 |
commit | 5a41d00455f67a03e3cb77cd89c3b710a7c389d0 (patch) | |
tree | 04581f1a19441de919eacfd5ce3a4b0481c03658 /actionview/lib/action_view/template.rb | |
parent | 062cbd18ddbf6ef48c9a9a2a69081737aebbee89 (diff) | |
download | rails-5a41d00455f67a03e3cb77cd89c3b710a7c389d0.tar.gz rails-5a41d00455f67a03e3cb77cd89c3b710a7c389d0.tar.bz2 rails-5a41d00455f67a03e3cb77cd89c3b710a7c389d0.zip |
Support explicit defintion of resouce name for collection caching.
If a template includes `# Template Collection: ...` anywhere in its
source, that name will be used as the cache name for the partial that is
rendered for the collection.
This allows users to enable collection caching even if the template
doesn't start with `<% cache ... do %>`.
Moreover, the `# Template Collection: ...` notation is recognized in all
template types (and template types other than ERB can define a
resource_cache_call_pattern method to allow the `cache ... do` pattern
to be recognized too).
Diffstat (limited to 'actionview/lib/action_view/template.rb')
-rw-r--r-- | actionview/lib/action_view/template.rb | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/actionview/lib/action_view/template.rb b/actionview/lib/action_view/template.rb index 377ceb534a..d8585514d5 100644 --- a/actionview/lib/action_view/template.rb +++ b/actionview/lib/action_view/template.rb @@ -130,7 +130,7 @@ module ActionView @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 @@ module ActionView 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 |