Skip to content

Instantly share code, notes, and snippets.

@guich-wo
Forked from ryanb/abilities.rb
Last active March 6, 2017 16:30
Show Gist options
  • Save guich-wo/48dbc2c37d0d192de10f9a05be285a9c to your computer and use it in GitHub Desktop.
Save guich-wo/48dbc2c37d0d192de10f9a05be285a9c to your computer and use it in GitHub Desktop.
How you can break up large Ability class in CanCan
module Abilities
def self.ability_for(user)
AdminAbility.new(user) if user.admin?
MemberAbility.new(user) if user.member?
# etc...
end
class AdminAbility
include CanCan::Ability
def initialize(user)
# ...
end
end
class MemberAbility
include CanCan::Ability
def initialize(user)
# ...
end
end
class GuestAbility
include CanCan::Ability
def initialize(user)
# ...
end
end
# move classes into separate files as needed if they get too long
end
def current_ability
@current_ability ||= Abilities.ability_for(current_user)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment