Skip to content

Instantly share code, notes, and snippets.

@brenes
Last active December 22, 2015 22:09
Show Gist options
  • Select an option

  • Save brenes/6538531 to your computer and use it in GitHub Desktop.

Select an option

Save brenes/6538531 to your computer and use it in GitHub Desktop.
helper method to obtain a fragment cache key depending on the currently selected page on the refinery menu
.......
.......
<% cache menu_cache_key, expire_in: 1.day do %>
<%= render(:partial => "/refinery/menu", :locals => {
:css => 'submenu'
}) %>
<% end %>
.......
.......
def menu_cache_key
# We get the current page: @page or the first page which is
current_cache_page = @page
if @page.nil?
path = request.path.sub("//", "/")
path = path.force_encoding('utf-8') if path.respond_to?(:force_encoding)
# Ensure we match the path without the locale, if present.
if ::Refinery.i18n_enabled? and path =~ %r{^/#{::I18n.locale}/}
path = path.split(%r{^/#{::I18n.locale}}).last
path = "/" if path.blank?
end
current_cache_page = Refinery::Page.with_translations(::I18n::locale).where("menu_match IS NOT NULL AND menu_match != ''").detect{ |p| path =~ Regexp.new(p.menu_match) }
end
cache_key = "-#{current_cache_page.id}" unless current_cache_page.nil?
cache_key = "refinery-menu-#{I18n.locale}#{cache_key}"
end
@brenes
Copy link
Copy Markdown
Author

brenes commented Sep 24, 2013

Added also a condition to check for non blank menu matches

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment