Skip to content

Instantly share code, notes, and snippets.

@sideshowbandana
Last active June 28, 2019 11:03
Show Gist options
  • Select an option

  • Save sideshowbandana/2659e1f4268d06b97bbf28aefe53e5f4 to your computer and use it in GitHub Desktop.

Select an option

Save sideshowbandana/2659e1f4268d06b97bbf28aefe53e5f4 to your computer and use it in GitHub Desktop.
# app/models/abilities/base.rb
module Abilities
class Base
include CanCan::Ability
def initialize(user)
end
end
end
# app/models/abilities/my_model.rb
module Abilities
class MyModel < Base
def initialize(user)
super(user)
return unless user.present?
can :manage, ::MyModel, user_id: user.id
end
end
end
# app/models/abilities/factory.rb
module Abilities
class Factory
def self.ability_for(controller_class, user)
{
MyModelsController => Abilities::MyModel
}.fetch(controller_class) do
# Use the base ability by default
Abilities::Base
end.new(user)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment