Skip to content

Instantly share code, notes, and snippets.

@kfatehi
Created April 20, 2012 09:01
Show Gist options
  • Save kfatehi/2427227 to your computer and use it in GitHub Desktop.
Save kfatehi/2427227 to your computer and use it in GitHub Desktop.
module UserAbilities
def can?(verb, target)
Ability[verb].call(self, verb, target)
end
Ability = {
:use=>lambda { |user, verb, target|
# This depends primarily on what the target is.
Target[target.class.to_s].call(user, verb, target)
}
}
Target = {
"Folder"=>lambda {|user, verb, target_folder|
user.folders.exists? target_folder
},
"User"=>lambda {|user, verb, target_user|
# "Power users" could set advanced permissions
# through a domain-specific language we come up with
# that gets injected in this space, so to speak
}
}
end
class User
include UserAbilities
...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment