Tuesday, November 8, 2011

Roles implemented with easy_roles plugin

 
Step 1 : Install Plugin
script/plugin install git://github.com/platform45/easy_roles.git


Step 2 : Generate migration to create column called roles in user's table

rails g easy_roles user roles
 

Step 3 : Run. rake db:migrate to run the migration
 
Step 4 : Add this line to the user's model
 
class User < ActiveRecord::Base
  easy_roles :roles
end


Add users role, remove, check role as follows.


@user = User.first 

@user.add_role 'admin' 

@user.is_admin? 
=> true 
   
@user.has_role? 'admin' 
=> true 
 
@user.is_awesome? 
=> false 
 
@user.add_role 'awesome' 
 
@user.is_awesome? 
=> true 
 
@user.remove_role 'admin' 
 
@user.is_admin? 
=> false 

No comments: