aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-12-05 06:02:43 -0800
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-12-05 06:02:43 -0800
commit6ed4ad133486f235fcba136d3a660d89f769beb9 (patch)
tree44e98a261aa46203639a7f374f04eee989e37b83 /actionpack/lib
parent1b32c06b99c7df5715d0fb47afc7edc4a7abd769 (diff)
parent6b014a4580a8f17ebb73b441e029c0bb7d34a8c5 (diff)
downloadrails-6ed4ad133486f235fcba136d3a660d89f769beb9.tar.gz
rails-6ed4ad133486f235fcba136d3a660d89f769beb9.tar.bz2
rails-6ed4ad133486f235fcba136d3a660d89f769beb9.zip
Merge pull request #8371 from freegenie/5396-conditional-fragment-caching
Allow fragment cache to accept :if and :unless options. Closes #5396
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_view/helpers/cache_helper.rb14
1 files changed, 13 insertions, 1 deletions
diff --git a/actionpack/lib/action_view/helpers/cache_helper.rb b/actionpack/lib/action_view/helpers/cache_helper.rb
index db920ae7a4..8693f4f0e4 100644
--- a/actionpack/lib/action_view/helpers/cache_helper.rb
+++ b/actionpack/lib/action_view/helpers/cache_helper.rb
@@ -110,8 +110,15 @@ module ActionView
# <%= some_helper_method(person) %>
#
# Now all you'll have to do is change that timestamp when the helper method changes.
+ #
+ # ==== Conditional caching
+ #
+ # You can pass :if and :unless options, to conditionally perform or skip the cache.
+ #
+ # <%= cache @model, if: some_condition(@model) do %>
+ #
def cache(name = {}, options = nil, &block)
- if controller.perform_caching
+ if controller.perform_caching && conditions_match?(options)
safe_concat(fragment_for(cache_fragment_name(name, options), options, &block))
else
yield
@@ -136,6 +143,11 @@ module ActionView
end
private
+
+ def conditions_match?(options)
+ !(options && (!options.fetch(:if, true) || options.fetch(:unless, false)))
+ end
+
def fragment_name_with_digest(name) #:nodoc:
if @virtual_path
[