Skip to content

Instantly share code, notes, and snippets.

@tsechingho
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsechingho/714d6f50865f86d6ac55 to your computer and use it in GitHub Desktop.
Save tsechingho/714d6f50865f86d6ac55 to your computer and use it in GitHub Desktop.
cancancan abilities
# 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
# app/controllers/application_controller.rb or any controller
class ApplicationController < ActionController::Base
def current_ability
@current_ability ||= Ability.ability_for current_customer
end
end
# 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