Skip to content

How to get current breadcrumb, for use in fragment caching #1

@kzkn

Description

@kzkn

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions