Skip to content

Instantly share code, notes, and snippets.

@gavingmiller
Last active December 23, 2015 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gavingmiller/6627231 to your computer and use it in GitHub Desktop.
Save gavingmiller/6627231 to your computer and use it in GitHub Desktop.
A snapshot of PetroFeed's authorization code
class Ability
include CanCan::Ability
def initialize(user)
@user = user || User.new # guest user (not logged in)
authorize_payment_limits
# Other access code
end
private
def authorize_payment_limits
if follow_limit_reached?
cannot :follow, Well
else
can :follow, Well
end
end
def follow_limit_reached?
!@user.pro? && @user.follow_count >= FOLLOW_LIMIT
end
end
@joshuapinter
Copy link

Really clean and well thought out. I love it.

One thing, can you simply do this instead:

can :follow, Well unless follow_limit_reached?

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