Created
April 29, 2010 12:54
-
-
Save andreapavoni/383546 to your computer and use it in GitHub Desktop.
simple user roles management with ActiveRecord
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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