Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kzkn opened this issue Sep 1, 2024 · 0 comments · May be fixed by #7
Open

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

kzkn opened this issue Sep 1, 2024 · 0 comments · May be fixed by #7

Comments

@kzkn
Copy link
Owner

kzkn commented Sep 1, 2024

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

@kzkn kzkn linked a pull request Sep 1, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant