How to split CanCanCan ability into smaller files

in #rails6 years ago

Assume that the following roles exist in the system: developer hacker designer startup

# app/models/ability.rb
class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new
    if user.id.present?
      self.merge Abilities::LoginedUserAbility.new(user)
      self.merge Abilities.const_get("#{user.role}_ability".classify).new(user) if user.role.present?
    else
      self.merge Abilities::GuestAbility.new
    end
  end

end
# app/models/abilities/logined_user_ability.rb
module Abilities
  class LoginedUserAbility
    include CanCan::Ability
    def initialize(user)
     # ...
    end
  end
end
# app/models/abilities/developer_ability.rb
module Abilities
  class DeveloperAbility
    include CanCan::Ability
    def initialize(user)
     # ...
    end
  end
end
Sort:  

Congratulations @borisding1994! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.029
BTC 59139.79
ETH 2511.97
USDT 1.00
SBD 2.41