Skip to content

Instantly share code, notes, and snippets.

@stevegraham
Created March 2, 2010 16:45
Show Gist options
  • Save stevegraham/319658 to your computer and use it in GitHub Desktop.
Save stevegraham/319658 to your computer and use it in GitHub Desktop.
class Ability
include CanCan::Ability
def initialize(user)
return if user.nil?
# Collect rights from roles
authorizations = HashWithIndifferentAccess.new []
user.roles.each do |role|
role.resource_rights.each { |resource,actions| authorizations[resource] |= actions }
end
# Grant rights
authorizations.each { |resource,actions| can actions, resource.to_s.classify.constantize }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment