diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-12-15 15:22:33 -0200 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2015-12-15 15:22:33 -0200 |
commit | ca8e14862a9f6f3ec37fb2842d76ecefd4eb2b4e (patch) | |
tree | da4ac7e4f893b294d02f8564dfd675c0bc0046d6 /actionpack/lib/action_controller/caching/fragments.rb | |
parent | ff370ee61754b00f864f3ddd0af62be4dfa0de67 (diff) | |
parent | e73fe1dd8c2740ae29e7a7f48d71a62b46e6b49d (diff) | |
download | rails-ca8e14862a9f6f3ec37fb2842d76ecefd4eb2b4e.tar.gz rails-ca8e14862a9f6f3ec37fb2842d76ecefd4eb2b4e.tar.bz2 rails-ca8e14862a9f6f3ec37fb2842d76ecefd4eb2b4e.zip |
Merge remote-tracking branch 'origin/master' into merge-action-cable
Diffstat (limited to 'actionpack/lib/action_controller/caching/fragments.rb')
-rw-r--r-- | actionpack/lib/action_controller/caching/fragments.rb | 51 |
1 files changed, 48 insertions, 3 deletions
diff --git a/actionpack/lib/action_controller/caching/fragments.rb b/actionpack/lib/action_controller/caching/fragments.rb index 2694d4c12f..c42384b741 100644 --- a/actionpack/lib/action_controller/caching/fragments.rb +++ b/actionpack/lib/action_controller/caching/fragments.rb @@ -14,12 +14,57 @@ module ActionController # # expire_fragment('name_of_cache') module Fragments + extend ActiveSupport::Concern + + included do + if respond_to?(:class_attribute) + class_attribute :fragment_cache_keys + else + mattr_writer :fragment_cache_keys + end + + self.fragment_cache_keys = [] + + helper_method :fragment_cache_key if respond_to?(:helper_method) + end + + module ClassMethods + # Allows you to specify controller-wide key prefixes for + # cache fragments. Pass either a constant +value+, or a block + # which computes a value each time a cache key is generated. + # + # For example, you may want to prefix all fragment cache keys + # with a global version identifier, so you can easily + # invalidate all caches. + # + # class ApplicationController + # fragment_cache_key "v1" + # end + # + # When it's time to invalidate all fragments, simply change + # the string constant. Or, progressively roll out the cache + # invalidation using a computed value: + # + # class ApplicationController + # fragment_cache_key do + # @account.id.odd? ? "v1" : "v2" + # end + # end + def fragment_cache_key(value = nil, &key) + self.fragment_cache_keys += [key || ->{ value }] + end + end + # Given a key (as described in +expire_fragment+), returns # a key suitable for use in reading, writing, or expiring a - # cached fragment. All keys are prefixed with <tt>views/</tt> and uses - # ActiveSupport::Cache.expand_cache_key for the expansion. + # cached fragment. All keys begin with <tt>views/</tt>, + # followed by any controller-wide key prefix values, ending + # with the specified +key+ value. The key is expanded using + # ActiveSupport::Cache.expand_cache_key. def fragment_cache_key(key) - ActiveSupport::Cache.expand_cache_key(key.is_a?(Hash) ? url_for(key).split("://").last : key, :views) + head = self.class.fragment_cache_keys.map { |key| instance_exec(&key) } + tail = key.is_a?(Hash) ? url_for(key).split("://").last : key + ActiveSupport::Cache.expand_cache_key([*head, *tail], :views) end # Writes +content+ to the location signified by |