Joined October 2013
·

Joshua Pinter

Canada
·
·

You're correct with this - hopefully, it was just a typo.

You're correct with this - hopefully, it was just a typo.

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
Interests & Skills