Allow fragment cache to accept :if and :unless options #8371

Merged
merged 1 commit into from Dec 5, 2012

Projects

None yet

4 participants

@freegenie
Contributor

No description provided.

@freegenie
Contributor

This issue was discussed here #5396

@rafaelfranca
Member

I think the implementation on #5396 is better.

@freegenie
Contributor

@rafaelfranca please tell me more of your point of view so that I can learn what's wrong

@rafaelfranca
Member

I think this is simpler.

def options_pass_conditions?(options) #:nodoc:
  !(options && (!options.fetch(:if, true) || options.fetch(:unless, false)))
end

Or this doesn't work with your implementation?

@freegenie
Contributor

well, you are right, it's much simpler, I fixed it and pushed again.

@rafaelfranca
Member

Why not use the same logic?

@freegenie
Contributor

yes you are right, I used fetch because it's much dryier and readable to me, pushed again. I'm using --force to keep it in one commit.

@freegenie
Contributor

@pixeltrix any opinion on this one too?

@pixeltrix
Member

I'm definitely 👍 on it - I've done something similar in my apps where for example I've cached the rendering of an empty shopping basket but not anything else as the cache hit rate would be low so anything which is user customisable but has a common default is definitely a good match for this.

I'll have a closer look later but it'll need a CHANGELOG entry and the commits squashing on first glance.

@rafaelfranca
Member

I didn't undertand why did you open a new pull request with the same code of #5396 without ask to the original author if it is still interested in the code.

Besides this, (both) pull requests look good and only need the CHANGELOG entry and the commits squashed.

@pixeltrix
Member

Does the test still fail in the way mentioned in #5396?

@freegenie
Contributor

@rafaelfranca sorry about that, I should have asked, you are right. I didn't mean to cheat anybody, I just thought it was easier for me to start from a white page on a branch of my own. And maybe I considered two months inactivity as enough time for anybody to be no longer interested in the code. Please go ahead with whatever you feel to be more correct to resolve this pull request.

@rafaelfranca
Member

@freegenie no problem. You did an awesome work and deserves recognition. Also, your tests are passing.

To be fair change you commit message and put this in the last line:

[Stephen Ausman + Fabrizio Regini]

And we can merge this one.

Thank you so much for the pull request.

@rafaelfranca
Member

Ah, if you could put the Stephen Ausman name in the CHANGELOG too

@freegenie
Contributor

@rafaelfranca done, thanks for teaching!

@rafaelfranca rafaelfranca merged commit 6ed4ad1 into rails:master Dec 5, 2012
@rafaelfranca
Member

Thank you so much for the pull request. ❤️

@pixeltrix
Member

@rafaelfranca hey, you stole my merge commit :trollface:

@bogdan bogdan commented on the diff Dec 5, 2012
actionpack/lib/action_view/helpers/cache_helper.rb
@@ -136,6 +143,11 @@ def cache_fragment_name(name = {}, options = nil)
end
private
+
+ def conditions_match?(options)
+ !(options && (!options.fetch(:if, true) || options.fetch(:unless, false)))
@bogdan
bogdan Dec 5, 2012 Contributor

I thought there is :force => true option supported by ActiveSupport::CacheStore to do this. I believe it is supported at this layer as well.

@pixeltrix
pixeltrix Dec 5, 2012 Member

@bogdan that's slightly different - it forces a cache miss so it will still write the newly rendered fragment. This will not cache the fragment at all.

@bogdan
bogdan Dec 5, 2012 Contributor

@pixeltrix clear enough

Than maybe we should support :if and :unless in CacheStore. wdyt?

@pixeltrix
pixeltrix Dec 5, 2012 Member

Possibly - can you think of a use case?

@bogdan
bogdan Dec 5, 2012 Contributor

Sure.

Test environment can create object with same id many times during test suite

Now I do:

  Rails.cache.fetch(object.id, :force => Rails.env.test?) do

  end

But using :if will be better.

@rafaelfranca
Member

@pixeltrix so sorry 🙇

@acapilleri acapilleri added a commit to acapilleri/rails that referenced this pull request Dec 14, 2012
@acapilleri acapilleri 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%>
4a3293e
@acapilleri acapilleri added a commit to acapilleri/rails that referenced this pull request Dec 14, 2012
@acapilleri acapilleri 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%>
8322342
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment