kzkn / gretel

Flexible Ruby on Rails breadcrumbs plugin.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to get current breadcrumb, for use in fragment caching

MrShemek opened this issue · comments

In my views, I'm calling breadcrumb like so:

breadcrumb :categories

or sometimes

breadcrumb :category, @category

I also have a separate partial, where I render the breadcrumbs in a loop to be included in my layout.

Now, I want to cache the output of my this partial so it's not constantly hitting the DB when rendering the breadcrumbs. How to do that using fragment caching?

I'm new to caching in Rails, but my understanding is that I should get whatever it was I had passed to breadcrumb and create a cache_key based off that.

It would be nice if I could call breadcrumb without any arguments, to get whatever was previously set as the current breadcrumb.

So then I could do something like this in my breadcrumb partial:

<% cache(breadcrumb) do %>
<% breadcrumbs.tap do |links| %>
<% if links.any? %>
<div class="breadcrumbs breadcrumbs-fixed" id="layout-breadcrumbs">
  <div class="container">
    <ul class="breadcrumb">
      <% links.each do |link| %>
      <li<%= ' class="current"' if link.current? %>>
        <% if link.icon.present? %>
        <i class="ace-icon<%= " #{link.icon}" %>"></i>
        <% end %>
        <% if link.current? %>
        <%= link.text %>
        <% else %>
        <%= link_to link.text, link.url %>
        <% end %>
      </li>
      <% end %>
    </ul>
  </div>
</div>
<% end %>
<% end %>
<% end %>

Reading the doco though, it doesn't look like that is possible.

Is there any easy way to get the current breadcrumb (or really whatever object/symbol passed to breadcrumb), without looping through the actual breadcrumbs (and triggering a DB lookup)?


Original issue: #40