Menuizer
build menu items for admin page ( like AdminLTE )
Installation
Add this line to your application's Gemfile:
gem 'menuizer'
And then execute:
$ bundle
Or install it yourself as:
$ gem install
Usage
# config/initializers/menuizer.rb
Menuizer.configure do ||
.header "MAIN NAVIGATION"
.item "Dashboard", icon: "fa fa-dashboard" do
.item "Dashboard v1", icon: "fa fa-circle-o"
.item "Dashboard v2", icon: "fa fa-circle-o"
end
.item Widget, icon: "fa fa-th"
.item "Settings", icon: "fa fa-cog" do
.item Admin, icon: "fa fa-circle-o"
.item User, icon: "fa fa-circle-o", notices: [
->{ [:warning, User..count] },
->{ [:danger, User.wait.count] },
]
.item "nested" do
.item "nested item", path: :path_to_somewhere, icon: "fa fa-circle-o"
end
end
end
<%# app/views/admins/index.html.erb %>
<% @active_menu = Admin # first argument of menu.item %>
<% content_for :title do %><%= Menuizer.menu.active_item.title %><% end %>
...
<ol class="breadcrumb">
<% Menuizer.menu.active_items.each do |item| %>
<li><%= link_to item.path || "#" do %><i class="<%= item.icon %>"></i> <%= item.title %><% end %></li>
<% end %>
</ol>
<%# app/views/layouts/application.html.erb %>
<% Menuizer.menu.activate @active_menu %>
<title><%= yield :title %></title>
...
<ul class="sidebar-menu">
<% Menuizer.menu.items.each do |item| %>
<%= render "menu", item: item %>
<% end %>
</ul>
<%
item # Menuizer.menu.items's item
%>
<% case item.type %>
<% when :header %>
<li class="header"><%= item.title %></li>
<% when :item %>
<li class="<% if item.is_active %>active<% end %>">
<%= link_to item.path || "#" do %>
<i class="<%= item.icon %>"></i> <span><%= item.title %></span>
<% if item.notices.present? %>
<% item.notices.each do |notice| %>
<% type, text = notice.call %>
<span class="label label-<%= type %> pull-right"><%= text %></span>
<% end %>
<% end %>
<% end %>
</li>
<% else %>
<li class="<% if item.is_active %>active <% end %>treeview">
<a href="#">
<i class="<%= item.icon %>"></i> <span><%= item.title %></span>
<span class="pull-right-container">
<i class="fa fa-angle-left pull-right"></i>
</span>
</a>
<ul class="treeview-menu">
<% item.children.each do |item| %>
<%= render "layouts/manage/menu", item: item %>
<% end %>
</ul>
</li>
<% end %>
API
menu.item
.item(
title, # required : convert to item.title
path: nil, # optional : convert to item.path
# other keys pass-through to item.*
icon: "fa fa-icon",
notices: [
->{ [:info, count] },
],
)
title converting
- convert
title.model_name.humanif title respond tomodel_name - or, leave
title
path converting
- convert
:"#{namespace}#{title.model_name.plural}"if title respond tomodel_name - or, leave
nil
what namespace is?
↓↓↓
Multiple namespaces
if your rails application has multiple namespaces, and required multiple menues, pass :namespace to Menuizer methods.
# config/initializers/menuizer.rb
Menuizer.configure(:namespace) do ||
...
end
<%# app/views/admins/index.html.erb %>
<% @active_menu = Admin # first argument of menu.item %>
<% content_for :title do %><%= Menuizer.menu(:namespace).active_item.title %><% end %>
...
<ol class="breadcrumb">
<% Menuizer.menu(:namespace).active_items.each do |item| %>
<li><%= link_to item.path || "#" do %><i class="<%= item.icon %>"></i> <%= item.title %><% end %></li>
<% end %>
</ol>
<%# app/views/layouts/application.html.erb %>
<% Menuizer.menu(:namespace).activate @active_menu %>
<title><%= yield :title %></title>
...
<ul class="sidebar-menu">
<% Menuizer.menu(:namespace).items.each do |item| %>
<%= render "menu", item: item %>
<% end %>
</ul>
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/getto-systems/menuizer.