Skip to content

Instantly share code, notes, and snippets.

@andreapavoni
Created April 29, 2010 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreapavoni/383546 to your computer and use it in GitHub Desktop.
Save andreapavoni/383546 to your computer and use it in GitHub Desktop.
simple user roles management with ActiveRecord
module RolePlay
module PluginMethods
def has_roleplay(roles = {})
@@roles = roles
@@roles_ids = roles.invert
def roles
@@roles
end
def find_by_role(role_name, *args)
find(:all, :conditions => { :role_id => @@roles[role_name]}, *args)
end
define_method 'role?' do |r|
r == @@roles_ids[role_id]
end
define_method :role do
@@roles_ids[role_id]
end
end
end
end
ActiveRecord::Base.extend RolePlay::PluginMethods
class User < ActiveRecord::Base
#...
has_roleplay(:admin => 0, :student => 1)
#...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment