Created
March 4, 2014 05:14
-
-
Save geoom/9340643 to your computer and use it in GitHub Desktop.
models/user.rb
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 | |
# Include default devise modules. Others available are: | |
# :confirmable, :lockable, :timeoutable and :omniauthable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable | |
ROLES = %w[admin moderator author banned] | |
def roles=(roles) | |
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.inject(0, :+) | |
end | |
def roles | |
ROLES.reject do |r| | |
((roles_mask.to_i || 0) & 2**ROLES.index(r)).zero? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment