Created
September 4, 2012 18:11
-
-
Save andrerpbts/3624361 to your computer and use it in GitHub Desktop.
Menus SGO
This file contains 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
#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 |
This file contains 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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment