Skip to content

Instantly share code, notes, and snippets.

@philsmy
Created August 23, 2011 09:30
Show Gist options
  • Save philsmy/1164741 to your computer and use it in GitHub Desktop.
Save philsmy/1164741 to your computer and use it in GitHub Desktop.
our ability class
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.role? :super_admin
can :manage, :all
else
if user.role? :live_trader
can :admin, LiveEvent
end
if user.role? :horse_trader
can :horse_admin, Event
end
if user.role? :channel_admin
can :manage, Channel
end
if user.role? :device_admin
can :manage, Device
end
if user.role? :ost
can :read, Customer.first
end
can do |action, subject_class, subject|
# Rails.logger.debug "action: #{action}"
user.roles.find_all_by_action(aliases_for_action(action)).any? do |role|
role.authorizable_type == subject_class.to_s &&
(subject.nil? || role.authorizable_id.nil? || role.authorizable_id == subject.id)
end
end
# can always manage ourselves
can :manage, user
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment