How to split CanCanCan ability into smaller files

in rails •  7 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
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hey bro, nice article.
you from korea ?

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!