Skip to content

Instantly share code, notes, and snippets.

@parndt
Created September 26, 2010 20:14
Show Gist options
  • Save parndt/598282 to your computer and use it in GitHub Desktop.
Save parndt/598282 to your computer and use it in GitHub Desktop.
# How to re-use the menu partial for a submenu in Refinery CMS >= 0.9.8.5 and < 0.9.9.22
# header menu (main menu) which hides its children.
<%= render :partial => "/shared/menu",
:locals => {
:dom_id => 'menu',
:css => 'menu',
:collection => @menu_pages,
:hide_children => true
} -%>
# content menu (submenu) (extra_suffix cures double-up on cache)
<%= render :partial => '/shared/menu',
:locals => {
:dom_id => 'submenu',
:css => 'menu',
:roots => @menu_pages.reject{|p| p.parent_id != @page.root.id },
:collection => @menu_pages.reject{|p| !p.self_and_ancestors.map(&:id).include?(@page.root.id) },
:extra_suffix => 'submenu'
} %>
@parndt
Copy link
Author

parndt commented May 30, 2011

At/after 1.0.0 you'll need something like this for the submenu:

<%= render :partial => '/shared/menu',
         :locals => {
           :dom_id => 'submenu',
           :css => 'menu',
           :roots => @menu_pages.reject{|p| p.parent_id != @page.root.id },
           :collection => @menu_pages.select{|p| ([p.id] | p.ancestors.map(&:id)).include?(@page.root.id) }
         } %>

@mattmate
Copy link

Hey parndt,

Do you know why this code could be breaking a site on running with Passenger 3 ?

I am pretty sure I have narrowed it down to the @menu_pages.select{|p| ([p.id] | p.ancestors.map(&:id)).include?(@page.root.id)

Every now and then the site will throw an error: ActionView::Template::Error (undefined method `each' for nil:NilClass):

A quick refresh and it functions as normal. Any ideas?

@dpie
Copy link

dpie commented Oct 3, 2011

Thanks for sharing this code, parndt. Works great!

@aripoya
Copy link

aripoya commented Jan 30, 2012

awesome!
now the newbie question: how do i implement link_to_unless_current on this thing?

@parndt
Copy link
Author

parndt commented Jan 30, 2012

You would have to override the _menu_branch partial

@matadere
Copy link

sorry I was in a rush commenting as aripoya, my colleague. I didn't see my own page source after trying this out. To my surprise the unless_current thingie already done to the code by automatically appending 'selected' class on the li. I feel silly now hehe. Thank you for replying anyway:D

@igorrootsi
Copy link

Refinery 2.0.0
<%

Collect the root items.

::Refinery::Menu is smart enough to remember all of the items in the original collection.

if (roots = local_assigns[:roots] || (collection ||= refinery_menu_pages).roots).present?
dom_id ||= 'menu'
css = [(css || 'menu'), 'clearfix'].flatten.join(' ')
hide_children = Refinery::Core.menu_hide_children if hide_children.nil?

# generating collection for sub menu
cll = refinery_menu_pages.reject{|p| ([p.original_id] | p.ancestors.map(&:original_id)).include?(@page.root.id) }
cll = refinery_menu_pages.reject{|p| p.parent_id != @page.root.id }

-%>

<%= render :partial => '/refinery/menu_branch', :collection => cll,
           :locals => {
             :hide_children => hide_children,
             :sibling_count => (roots.length - 1),
             :menu_levels => local_assigns[:menu_levels],
             :apply_css => true #if you don't care about class='first' class='last' or class='selected' set apply_css to false for speed.
           } -%>

sorry, im a newbie... if i did something wrong...

@jess
Copy link

jess commented Dec 31, 2012

this works for me:

  <ul class="sidenav">
    <%= render :partial => '/refinery/menu_branch', 
      :collection => refinery_menu_pages.select{|p| p.parent_id == @page.root.id},
      :locals => {
        :hide_children => false,
        :apply_css => true 
      } -%>
  </ul>

It'll display all sub menu links for a root menu item while on a page for that root.

@gajeev
Copy link

gajeev commented Aug 30, 2013

Anyone got this working in 2.1?

@gajeev
Copy link

gajeev commented Aug 30, 2013

For 2.1's sexy new menu presenter:

  <%=
      presenter = Refinery::Pages::MenuPresenter.new(refinery_menu_pages, self)
      presenter.roots = refinery_menu_pages.select{|p| p.parent_id == @page.root.id}
      presenter.to_html
  %>

@philmill
Copy link

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