Cancancanallows you to define permissions for a given context. This context may be a user role that is not part Cancancan, and therefore the roles must be defined by themselves.
There are various ways to define a user role, for example.
- as a model
Role, - Rails enum ,
- as proposed here ,
- as a string attribute of the model
User.
It all depends on the use case. An example of defining abilities can be found here . In your case, it will look like this:
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.reviewer?
can :manage, Comment, { owner_id: user.id }
elsif user.admin?
can :manage, :all
end
end
end
class User < ActiveRecord::Base
enum role: [ :reviewer, :admin ]
end
source
share