Add cache_if and cache_unless two methods to skip fragment cache. #7868

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
5 participants

jjyr commented Oct 7, 2012

Sometimes you don't want cache to all user.
you can

<%cache_if session[:user_id].blank?,'xxx' do%>
  something..
<%end%>

or

<%cache_unless current_user.admin?, 'xxx' do%>
  something..
<%end%>
actionpack/lib/action_view/helpers/cache_helper.rb
+ perform_caching = options[:if]
+ elsif options.include?(:unless)
+ perform_caching = !options[:unless]
+ end
@mhuggins

mhuggins Oct 7, 2012

Looks like you used a tab instead of spaces for the leading whitespace on lines 107-112. The indentation appears off.

Contributor

acapilleri commented Oct 8, 2012

Something like the following does respect much the rails standard?

<%cache_if current_user.admin?, xxx %>

or

<%cache_unless current_user.admin?, xxx %>

jjyr commented Oct 8, 2012

@acapilleri good idea

Owner

rafaelfranca commented Oct 15, 2012

This is a dup of #5396. Closing in favor of #5396

Contributor

joevandyk commented Nov 21, 2012

This is much simpler than the other solution.

+ #
+ # See cache method for details.
+ def cache_unless(condition, name = {}, options = nil, &block)
+ unless condition
@joevandyk

joevandyk Nov 21, 2012

Contributor

Should this method be

cache_if !condition, name, options, &block

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment