Custom objects in abilities #8

ryanb opened this Issue Nov 20, 2009 · 1 comment


None yet
1 participant

ryanb commented Nov 20, 2009

There are times one needs to set permission on something which doesn't have to do with a model or ruby class. Perhaps there's a page which displays general statistics about the site. One should be able to use a symbol or any other object when defining abilities, and then it will only match that same object.

can :read, :stats
can? :read, :stats # => true

If one passes a block to this, then no additional objects should be passed since we aren't dealign with a class here.

can :read, :stats do
  # ...

So in this case the block is a little pointless since you can define conditions in an "if" condition outside of this. The only real difference is the block gets executed on each check.

The :stats symbol could be any object, so in theory this could be used to match a specific record in the database. However, I recommend using the block for performance reasons.

can :manage, user.profile # don't do this
can(:manage, Profile) { |profile| user.profile == profile } # do this

This will lead to better performance since you are only loading the record object when performing the check and not on every request.

This should be mentioned in the documentation.


This comment has been minimized.

Show comment Hide comment

ryanb Nov 26, 2009


support custom objects (usually symbols) in can definition - closed by e603655


ryanb commented Nov 26, 2009

support custom objects (usually symbols) in can definition - closed by e603655

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment