Proposal: faragment cache with :if / :unless conditions


I’m wondering if it would be a good idea to have fragment cache to allow options :if / :unless to determine whether or not to actually perform caching on the given block.

It would actually be a good idea in my opinion right now, since I’m working on a piece of code that would benefit by this feature.

The scenario is the following:

I have a partial, which is conditionally requiring other partials inside. The first line of this partial starts with a cache(key) do declaration, but some of the nested partial can’t basically be cached due to the nature of the content.

I could obviously wrap every nested partial call inside a cache for itself. This is technically not the same in terms of performance gain (not all computations inside the main partial would be cached) and it would probably be more clean to have something like this (trivialized):

<%= cache(@model, unless: content_cachable?(@model)) do %>

<% if @model.render_good? %>

<%= render ‘good_one’ %>

<% else %>

<%= render ‘bad_one’ %>

<% end %>

… other tens of similar conditions

<% end %>

This may reveal some bad partial design I’m facing, but I’m curious to know what you guys think about it.


There is an open pull request for this:

I searched issues for something similar but didn't find it. Thanks!

Just for the records, there’s a new pull request here: