Last active
August 29, 2015 14:13
-
-
Save tsechingho/714d6f50865f86d6ac55 to your computer and use it in GitHub Desktop.
cancancan abilities
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
# app/models/ability.rb | |
module Ability | |
class << self | |
def ability_for user, options = {} | |
abilities = AnonymousAbility.new | |
return abilities unless user | |
abilities.merge MemberAbility.new user, options | |
if user.has_role? 'administrator' | |
abilities.merge AdministratorAbility.new | |
end | |
abilities | |
end | |
end | |
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
# app/controllers/application_controller.rb or any controller | |
class ApplicationController < ActionController::Base | |
def current_ability | |
@current_ability ||= Ability.ability_for current_customer | |
end | |
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
# app/abilities/member_ability.rb | |
class MemberAbility | |
include CanCan::Ability | |
attr_accessor :current_user | |
def initialize current_user, options = {} | |
can :update, User do |user| | |
user.email == current_user.email | |
end | |
can :update, Profile do |profile| | |
profile.owners.include? current_user | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment