Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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