Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Separating Abilities

Alex edited this page May 6, 2015 · 2 revisions

Below is a simple example how to separate abilities for each role.
Use a factory pattern and move responsibilities to appropriate ability files.

class ApplicationController
  # @override
  def current_ability
    @current_ability ||= Ability::Factory.build(user)
  end
end
class Ability::Factory
  def self.build(user)
    case user.role
      when :user then Ability::User.new(user)
      when :admin then Ability::Admin.new(user)
      when :editor then Ability::Editor.new(user)
    end
  end
end
class Ability::Editor < Ability
  def initialize(user)
    can :view, Report, last_modifier_id: user.id
  end
end