Skip to content

Instantly share code, notes, and snippets.

@johncox00
Last active December 29, 2016 12:23
Show Gist options
  • Save johncox00/f7fb3b549367f0444f803803c9f0d02f to your computer and use it in GitHub Desktop.
Save johncox00/f7fb3b549367f0444f803803c9f0d02f to your computer and use it in GitHub Desktop.
What if we could load only the abilities that we want for Cancan? (This is a rough sketch of the concept.)
class FilteredAbility
def initialize(user)
@user = user
end
def ability_for(obj)
class_abilities = organized_abilities[obj.class]
considers = class_abilities.keys.select{|a| @user.send(a)}
considers.each{|c| create_ability(class_abilities[c])
end
def create_ability(ability, obj)
send ability.key, obj.class, ability.value # this isn't it precisely, but something along these lines
end
def organized_abilities
{
# Class: {
# :boolean_user_method => {
# can: {
# [:read, :list] => condition_with_way_to_inject_dependency_on_obj
# },
# cannot: {
# }
# }
# }
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment