Use Cancancan with Multiple Ability files for different models
Hi its possible to have more that one ability file or extra arg in the initialize. I need to use the cancancan on a Admin model , User Model and a Company Model example:
class PolicyAbility
include CanCan::Ability
def initialize(company) #current_company
if company.policy.each do |p|
...
end
end
end
class Ability
include CanCan::Ability
def initialize(user) #current_user
if user.role == "manager"
can [:manage], User
else
can [:index,:show] User
end
end
end
class AdminAbility
include CanCan::Ability
def initialize(admin) #current_admin
can [:manage], Company
end
end
This the solution that I use for my problem. Love to know if there's a better solución for this.
class ApplicationController < ActionController::Base
def current_company
...
end
private
def current_ability
@current_ability ||= Ability.new(current_user, current_company)
end
end
class Ability
include CanCan::Ability
def initialize(user, company)
user stuff
and company stuff
end
end
class ApplicationController < ActionController::Base
def current_company
...
end
private
def current_ability
@current_ability ||=
if current_user
Ability.new(current_user, current_company)
else current_super_admin
SuperAdminAbility.new
end
end
end
Does this still work for you @Frank004 ?
@kaizenx yes I'm still using it on rails 5.0
Hi can yo show the final code in Ability, controlles and models.
@Frank004 would be awesome if you can share some more code
From cancan :
- https://github.com/ryanb/cancan/issues/311#issuecomment-904149
- https://github.com/ryanb/cancan/wiki/Changing-Defaults
# routes.rb
devise_for :users # current_user
devise_for :admins # current_admin
# in application_controller.rb
def current_ability
@current_ability ||= current_admin ? AdminAbility.new(current_admin) : UserAbility.new(current_user)
end
@mustela and @lalovargas
ApplicationController
def current_ability
if current_user
@current_ability ||= Ability.new(current_user, current_company)
elsif current_admin
@current_ability ||= AdminAbility.new(current_admin)
end
end
Ability for user and Ability for Admin
nothing more
Thanks @Frank004, I did a few more things, and the performance improvement was incredible.
We used to have a single ability file per role, with a lot of rules. So it was actually killing each request. What we did was to use the controller name to actually load only the abilities you need for that particular endpoint.
So we ended up with something like
-- abilities
---- user_ability < base_ability
---- resource_ability < base_ability
---- another_resource_ability < base_ability
Then we have a factory that we placed in the application_controller that loads the correct class, something like:
def current_ability
@current_ability ||= Abilities::BaseAbility.build(current_user, controller_name)
end
Hope it helps :)
@mustela WOW that's better than what I'm using the request go faster like this.
https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities:-Best-Practices#split-your-abilityrb-file