Your rules are optimized automatically at runtime. There are a set of "rules" to optimize your rules definition and they are implemented in the RulesCompressor
class. Here you can see how this works:
A rule without conditions is defined as catch_all
.
can :read, Book, author_id: user.id
cannot :read, Book, private: true
can :read, Book
can :read, Book, id: 1
cannot :read, Book, private: true
becomes
can :read, Book
cannot :read, Book, private: true
cannot :read, Book
can :read, Book, author_id: user.id
becomes
can :read, Book, author_id: user.id
cannot :read, Book, private: true
becomes
# nothing
These optimizations allow you to follow the strategy of "Give Permissions, don't take them" and automatically ignore previous rules when they are not needed.