Add pundit_kit to your gemfile:
gem 'pundit_kit'
Example of initializer routes:
class ClientNotAllowedError < StandardError; end
class UserNotAllowedError < StandardError; end
PunditKit.routes do
namespace :staff, if: -> (user) { user.staff? }, presence: false do
namespace :admin, if: -> (user) { user.admin? }
namespace :user, if: -> (user) { user.user? }, error: UserNotAllowedError
end
namespace :client, if: -> (user) { user.client? }, error: ClientNotAllowedError do
namespace :superclient,
if: -> (user) { user.superclient? },
error: ClientNotAllowedError,
presence: false
end
end
Each namespace has these options:
options | default | description |
---|---|---|
if: | -> { true } | lamda(or any callable object) evaluation of which determines should be used this namespace or not |
presence: | true | if true then will raise error if policy in this namespace can't be found |
error: | Pundit::NotAuthorizedError | error which would be raised if authorize call will return false |
For example yours application logic looks like this: Include PunditKit to ApplicationController
class ApplicationController < ActionController::Base
include PunditKit
end
This'll add helpers to yours controllers:
authorize_all
- this method will call authorize on every namespaceall_policies
- this method will return all namespaces matches topundit_namespace_matcher
- scope
- fallbacks
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/wilddima/pundit_kit.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the PunditKit project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.