MonoForge

pellucidity / rank_fu

rank_fu

Public

Rank-Fu: A plugin for richer user classes

13 filesupdated Jun 18, 2026

README

RankFu

RankFu is a plugin that allows you to add expressive user roles to your Rails App.

Now it's easy to have separate moderators and admins, and define a role as a superset of another.

Disclaimer

This plugin is provided as is - therefore, the creators and contributors of this plugin are not responsible for any damages that may result from it's usage.

Use at your own risk; backup your data.

Another Disclaimer

This plugin meets our simple needs, and has been extracted because we've needed the functionality several times, but it is by no means complete.

If you encounter bugs or gaping holes in functionality , please contact me using my first name at my company, and I'll see what I can do.

RankFu uses Rails 2.1 style migrations. It shouldn't be too hard to create migrations manually if you are using <2.1.0.

Example

After installing this plugin, you'll want to start by performing some admin:

script/generate rank_fu user
script/generate rank_fu roles 		#create migration for roles
                                                     

After that, you'll probably want to add roles to your user model, so just add this line:

knows_rank_fu  

Then you need to create some roles. I suggest using the excellent db:populate rake task so you can do something like this:

Role.destroy_all
roles = [ {:id => 1, :key => "root",      :name=> "Superuser", :value => 2**22, :set => 1},
          {:id => 2, :key => "admin",     :name=> "Administrator", :value => 2**21, :set => 1},
          {:id => 3, :key => "moderator", :name=> "Editor", :value => 2**20, :set => 1},     
          {:id => 5, :key => "member",    :name=> "Member", :value => 2**10, :is_default => true}]

roles.each do |role|                          
   Role.create role
end    

NOTE: It is essential that the values be powers of two. RankFu uses bitwise math internally.

Now you can do the following:

User#has_role? 
User#has_role
User#"#{role}?"
User#"#{role}_exactly?"   #This forces an exact comparison, useful if you want to test for a role which has a superset (e.g. moderators and  administrators)
User#"make_#{role}          
User#"remove_#{role}" 

There is also some sugar for disabling users, to make your code more readable. These examples assume the existence of a role with :disabled as its key:

User#disable_user 
User#enable_user
User#enabled?    

TODO

Specs. Benchmarks.

Resources

Copyright (c) 2008 Simon Parsons of Intridea (http://www.intridea.com), released under the MIT license