Skip to content

Instantly share code, notes, and snippets.

@andrerpbts
Created September 4, 2012 18:11
Show Gist options
  • Save andrerpbts/3624361 to your computer and use it in GitHub Desktop.
Save andrerpbts/3624361 to your computer and use it in GitHub Desktop.
Menus SGO
#encoding: utf-8
class Ability
include CanCan::Ability
def initialize(user)
user.perfis.each do |perfil|
perfil.permissoes_perfis.each do |permissao|
can permissao.acao.to_sym, Recurso.por_tipo(permissao.recurso, permissao.tipo)
end
end
end
end
# encoding: utf-8
module ApplicationHelper
def build_menus
return session[:menu] if session[:menu].present?
menu_hash = YAML::load(File.open('config/menu.yml')).recursive_symbolize_keys!
@total_menus ||= []
menu_hash.each_pair do |key, value|
menu = Menu.new
menu.hash = { key => value }
if menu.root?
sub_menus = build_sub_menus(menu.children)
@total_menus << content_tag(:h3, link_to(menu.nome, '#')) + content_tag(:div, tag(:hr) + content_tag(:ul, sub_menus, :class => "submenu")) unless sub_menus.blank?
end
end
session[:menu] = @total_menus.join('').html_safe
end
private
def build_sub_menus(sub_menus)
total_sub_menus = []
sub_menus.map do |menu|
if menu.children.any?
sub_menus = build_sub_menus(menu.children)
total_sub_menus << content_tag(:li, content_tag(:span, menu.nome) + content_tag(:ul, sub_menus), :class => "expandable") unless sub_menus.blank?
else
if (menu.acao && menu.recurso && menu.tipo &&
can?(menu.acao.to_sym, Recurso.por_tipo(menu.recurso, menu.tipo))) ||
menu.recurso.blank?
menu_path = menu.path.blank? ? '' : menu.path.to_sym
total_sub_menus << content_tag(:li, link_to(menu.nome, self.respond_to?(menu_path) ? send(menu_path) : '#'))
end
end
end
total_sub_menus.join('').html_safe
end
end
#encoding: utf-8
class Menu
attr_accessor :nome, :path, :recurso, :acao, :tipo, :children
def initialize
@children = []
@root = true
end
def root?
@root
end
def root=(value)
@root = value
end
def hash=(hash)
hash.each_pair do |key, value|
if value.is_a?(Hash)
value.each_pair do |child_key, child_value|
if child_value.is_a?(Hash)
menu = Menu.new
menu.root = false
menu.hash = { :menu => child_value }
@children << menu
else
send("#{child_key}=", child_value) if respond_to?("#{child_key}=")
end
end
else
send("#{key}=", value) if respond_to?("#{key}=")
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment