Are these two statements basically the same? If they aren't then what should the second version look like? And what's going on under the covers?
can :index, User, approved: true
can :index, User do |user|
user.approved?
end
I'm not able to get "block" versions of tests working. Everything else works fine.. but blocks don't work. I'm clearly doing something wrong, so I'm trying to understand. Thanks.
Rules with blocks are used when an instance is passed to the rule:
The
indexaction is special because there's no instance of User to pass to the rule, you are not loading a specific user, but multiple.So,
When the
UserController#indexaction is hit, assuming you haveload_and_authorize_resource(or similar in place), it will load in@usersall the users withapproved: true. If there are no conditions, all users will be loaded. It's straightforward.Now,
If a rule has a block and the instance is not passed to it (as I said above), the rule will always return
trueas in authorized, BUT no users will be loaded.More explanations, here.