Last active
December 22, 2015 22:09
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ....... | |
| ....... | |
| <% cache menu_cache_key, expire_in: 1.day do %> | |
| <%= render(:partial => "/refinery/menu", :locals => { | |
| :css => 'submenu' | |
| }) %> | |
| <% end %> | |
| ....... | |
| ....... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added also a condition to check for non blank menu matches