Skip to content

Instantly share code, notes, and snippets.

@robyurkowski
Created June 27, 2012 15:44
Show Gist options
  • Save robyurkowski/3004935 to your computer and use it in GitHub Desktop.
Save robyurkowski/3004935 to your computer and use it in GitHub Desktop.
Customizing Menus in RefineryCMS 2.0
class MenuCell < Cell::Rails
helper MenuHelper
def utility(args)
@roots = args[:menu].roots.select {|p| p[:menu_location] == 'utility' }
@roots_count = @roots.count
render
end
def main(args)
@roots = args[:menu].roots.select {|p| p[:menu_location] == 'main' }
@roots_count = @roots.count
render
end
def footer(args)
@roots = args[:menu].roots.select {|p| p[:menu_location] == 'footer' }
@roots_count = @roots.count
render
end
def about(args)
# Get all menu pages, not just roots
@roots = args[:menu].select {|p| p[:menu_location] == 'about' }
@roots_count = @roots.count
render
end
end
Refinery::MenuItem.class_eval do
class << self
def attributes
[:title, :parent_id, :lft, :rgt, :depth, :url, :menu, :menu_match, :menu_location]
end
end
end
::Refinery::Page.class_eval do
MENU_LOCATIONS = %w(utility main footer about)
validates :menu_location, :inclusion => {:in => MENU_LOCATIONS}, :allow_blank => true, :allow_nil => true
attr_accessible :menu_location
def to_refinery_menu_item
{
:id => id,
:lft => lft,
:menu_match => menu_match,
:parent_id => parent_id,
:rgt => rgt,
:title => refinery_menu_title,
:type => self.class.name,
:url => url,
:menu_location => menu_location
}
end
class << self
MENU_LOCATIONS.each do |location|
define_method("in_#{location}_menu".to_sym) do
where(:menu_location => location)
end
end
end
end
module ApplicationHelper
def render_menu(template)
render_cell :menu, template.to_sym, :menu => refinery_menu_pages
end
end
module MenuHelper
def css_for_item(index, has_children = false)
css = []
css << Refinery::Core.menu_css[:first] if index == 0 unless Refinery::Core.menu_css[:first].nil?
css << Refinery::Core.menu_css[:last] if (index + 1) == @roots_count unless Refinery::Core.menu_css[:last].nil?
css << 'submenu' if has_children
css
end
end
<% # ... %>
<div class='field'>
<%= f.label :menu_location %>
<%= f.select :menu_location, ::Refinery::Page::MENU_LOCATIONS.collect {|m| [m.capitalize, m]}, :include_blank => true %>
</div>
</div>
<%= render 'form_advanced_options_seo', :f => f %>
<% # ... %>
# Run rake db:migrate.
class AddMenuLocationToRefineryPages < ActiveRecord::Migration
def change
add_column :refinery_pages, :menu_location, :string
end
end
# Add this:
gem 'cells'
- ####################
- # app/views/refinery/_footer.html.haml
%footer#footer
= render_menu 'footer'
- ####################
- # app/cells/menu/main.html.haml
%nav#navigation
.container
%span.logo.ir Guru<span></span>
%ul
- @roots.each_with_index do |page, index|
%li{:class => css_for_item(index, page.children.present?).join(" ")}
= link_to refinery.url_for(page.url) do
= page.title
- if page.children.present?
<span></span>
- if page.children.present?
%div
%nav
- page.children.each do |child|
%span.title= child.title
- if child.children.present?
%ul
- child.children.each do |grandchild|
%li
= link_to grandchild.title, refinery.url_for(grandchild.url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment