aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib
diff options
context:
space:
mode:
authorAngelo capilleri <capilleri@yahoo.com>2012-12-12 18:26:38 +0100
committerAngelo capilleri <capilleri@yahoo.com>2012-12-14 17:51:52 +0100
commit83223424b33e332673fc3e4bdcae7477e601377f (patch)
tree437a668dc56c84b7defc5c0e4913386f86c9b7fd /actionpack/lib
parentd4333eded218346eb8b8313f7c69851782f594ef (diff)
downloadrails-83223424b33e332673fc3e4bdcae7477e601377f.tar.gz
rails-83223424b33e332673fc3e4bdcae7477e601377f.tar.bz2
rails-83223424b33e332673fc3e4bdcae7477e601377f.zip
Removed :if and :unless from fragment cache option in favour of
cache_if(condition, option, &block) and cache_unless(condition, option, &block). In the PR #8371 was introduced conditional options :if and :unless in the cache method. Example: <%= cache @model, if: some_condition(@model) do %> ... <%end%> This is a good feature but *cache_if* and and *cache_unless* are more concise and close to the standard of rails view helpers (ex: link_to_if and link_to_unless). Example: <%= cache_if condition, @model do %> ... <%end%>
Diffstat (limited to 'actionpack/lib')
-rw-r--r--actionpack/lib/action_view/helpers/cache_helper.rb36
1 files changed, 24 insertions, 12 deletions
diff --git a/actionpack/lib/action_view/helpers/cache_helper.rb b/actionpack/lib/action_view/helpers/cache_helper.rb
index 8693f4f0e4..932a5fa65a 100644
--- a/actionpack/lib/action_view/helpers/cache_helper.rb
+++ b/actionpack/lib/action_view/helpers/cache_helper.rb
@@ -110,15 +110,8 @@ 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 && conditions_match?(options)
+ if controller.perform_caching
safe_concat(fragment_for(cache_fragment_name(name, options), options, &block))
else
yield
@@ -127,6 +120,29 @@ module ActionView
nil
end
+ #Chache fragments of a view if +condition+ is true
+ #
+ #=== Example
+ # <%= cache_if admin?, project do %>
+ # <b>All the topics on this project</b>
+ # <%= render project.topics %>
+ # <% end %>
+ def cache_if(condition, name = {}, options = nil, &block)
+ if condition
+ cache(name, options, &block)
+ else
+ yield
+ end
+
+ nil
+ end
+
+ #Chache fragments of a view unless +condition+ is true
+ #(see +cache_if+ for a example)
+ def cache_unless(condition, name = {}, options = nil, &block)
+ cache_if !condition, name, options, &block
+ end
+
# This helper returns the name of a cache key for a given fragment cache
# call. By supplying skip_digest: true to cache, the digestion of cache
# fragments can be manually bypassed. This is useful when cache fragments
@@ -144,10 +160,6 @@ module ActionView
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
[