Joined October 2013
·
Posted to
Rails, Devise, CanCan, Rolify and User Company Role
over 1 year
ago
You're correct with this - hopefully, it was just a typo.
Posted to
Rails, Devise, CanCan, Rolify and User Company Role
over 1 year
ago
You can get rid of the nested conditionals by refactoring it like this:
def initialize(user)
user ||= User.new
can :read, :all
if user.current_role == 'admin'
can :manage, :all
else
can :read, Products
can :update, Products
cannot :destroy, Products
cannot :create, Products
cannot :manage, Client
end
end
And in the case that you have multiple roles, you can do the following:
def initialize(user)
user ||= User.new
can :read, :all
case user.current_role
when 'admin'
can :manage, :all
when 'moderator'
can :read, Products
can :update, Products
cannot :destroy, Products
cannot :create, Products
cannot :manage, Client
# ... other roles and abilities ...
end
end
Achievements
27 Karma
0 Total ProTip Views
Forked
Have a project valued enough to be forked by someone else
Charity
Fork and commit to someone's open source project in need
Mongoose
Have at least one original repo where Ruby is the dominant language
You're correct with this - hopefully, it was just a typo.