Last active
June 28, 2019 11:03
-
-
Save sideshowbandana/2659e1f4268d06b97bbf28aefe53e5f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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