_mask and Rails
- by Eric Koslow
So I am trying to get the cancan gem to work with my rails 3 app and I've hit a problem.
I tried to copy the code that Ryan Bates (the creator of the gem) used in his screen cast, but I get an error saying that roles_mask is not a method. I figure that the _mask method was removed from Ruby/Rails at some point, and I'm now wondering what is the replacement.
Here's the code in my user.rb model:
named_scope :with_role, lambda { |role| {:conditions => "roles_mask & #{2**ROLES.index(role.to_s)} > 0 "} }
ROLES = %w[admin student principal admissions]
def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
end
def roles
ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }
end
def role?
roles.include? role.to_s
end
def role_symbols
roles.map(&:to_sym)
end
I'm using Rails 3 and Ruby 1.9.2dev
Thank you