开发者

cancan ability definition syntax - is there a shorter version?

I currently have something like this in ability.rb:

if user.role? == "staff"
    can :update, Interp do |item|
        item.try(:user) == user
    end
    can :update, Invitation do |item|
        item.try(:user) == user
    end
end

Actually, there are a bunch of statements like this for "update开发者_运维技巧" for each role. Would this next version work just as well?

if user.role? == "staff"
    can :update, [Interp, Invitation] do |item|
        item.try(:user) == user
    end
end

Is there a shorter way?

UPDATE: This method definitely works and is the least amount of code to do this.


That should work. If it doesn't, you can always do it like this:

if user.role? == "staff"
    [Interp, Invitation].each do |resource|
        can :update, resource do |item|
            item.try(:user) == user
        end
    end
end

However, I think cancan is smart enough to understand what you're trying to do in this case. Never tried it, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜