Skip to content

Instantly share code, notes, and snippets.

@knoxknox
Created May 6, 2015 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knoxknox/f4e5e7836db73e51a898 to your computer and use it in GitHub Desktop.
Save knoxknox/f4e5e7836db73e51a898 to your computer and use it in GitHub Desktop.
# controllers/application_controller.rb
class ApplicationController
# @override
def current_ability
@current_ability ||= Ability::Factory.build(user)
end
end
# ability/factory.rb
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
# ability/editor.rb
class Ability::Editor < Ability
def initialize(user)
can :view, Report, last_modifier_id: user.id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment