ActsAsMenuRole is a rails plugin for simplify menu's creation for a particular role and controller, based on RoleRequirement and RestfulAuthentication (or acts_as_authenticated).
Features:
- The menu's actions can be separate by controller.
- Very easy to use.
- No need migrations.
Syntax:
The syntax for add menu is very simple:
Actions for a controller:
menu_role :role, {:action1 => "Screen Name1", :action2 => "Screen Name2"}
menu_role :other_role, {:action3 => "Screen Name3", :action4 => "Screen Name4"}Actions with reference a controller:
menu_role :role, {"/controller2/action1" => "Screen Name1", "/controller1/action2" => "Screen Name2"}And for a commons actions (all roles will can view)
menu_role :all, {:action5 => "Screen Name5"}Install:
1. Install restful_authentication and role_requirement
To install restful_authentication:
script/plugin install http://svn.techno-weenie.net/projects/plugins/restful_authentication/
Run generators and migrations:
script/generate authenticated user sessions rake db:migrate
To install role_requirement:
script/plugin install http://rolerequirement.googlecode.com/svn/tags/role_requirement/
Run generators:
if you want a user to have many roles:
script/generate roles Role User rake db:migrate
2. Install acts_as_menu_role:
script/plugin install http://actsasmenurole.googlecode.com/svn/tags/acts_as_menu_role
3. Copy rhtml template:
cp vendor/plugins/acts_as_menu_role/templates/_menu.rhtml app/views/users/_menu.rhtml
4. Edit applitacion Controller, MenuSystem must be included :
class ApplicationController < ActionController::Base # AuthenticatedSystem must be included for RoleRequirement, and is provided by installing acts_as_authenticates and running 'script/generate authenticated account user'. include AuthenticatedSystem # You can move this into a different controller, if you wish. This module gives you the require_role helpers, and others. include RoleRequirementSystem # MenuSystem must be included. include MenuSystem helper :all # include all helpers, all the time # See ActionController::RequestForgeryProtection for details # Uncomment the :secret if you're not using the cookie session store protect_from_forgery # :secret => '2b3f448f38126557af31fa82de12e217' end
5. Edit application's layout and add render partial.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Act as Menu</title>
<%= stylesheet_link_tag 'style' %>
<%= javascript_include_tag :defaults %>
</head>
<body>
<div id="title">My App</div>
<!-- ########## ADD PARTIAL FOR MENU ########### -->
<%= render_partial 'users/menu' %>
<% if flash[:error] %>
<div id="errors">
<%=flash[:error] %>
</div>
<% end %>
<% if flash[:notice] %>
<div id="notices">
<%=flash[:notice] %>
</div>
<% end %>
<div id="maincontent">
<%=@content_for_layout %>
</div>
</body>
</html>Set a menu for a users controller:
class UsersController < ApplicationController
# Menu for all roles
menu_role :default, { :login => "Login", :logout => "Exit" }
# Menu for :admin role
menu_role :admin, { :list => "View all accounts" }
def new
end
def create
cookies.delete :auth_token
# protects against session fixation attacks, wreaks havoc with
# request forgery protection.
# uncomment at your own risk
# reset_session
@user = User.new(params[:user])
@user.save!
self.current_user = @user
redirect_back_or_default('/')
flash[:notice] = "Thanks for signing up!"
rescue ActiveRecord::RecordInvalid
render :action => 'new'
end
endAnd that's it, enjoy!
Author:
Gastón Ramos - ramos.gaston <at> gmail <dot> com