-
-
Save dhruvasagar/3736096 to your computer and use it in GitHub Desktop.
How you can break up large Ability class in CanCan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Abilities | |
def self.ability_for(user) | |
if user.admin? | |
AdminAbility.new(user) | |
else user | |
MemberAbility.new(user) | |
else | |
GuestAbility.new | |
end | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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