From 31683a12dffa1f3aadf5df442a32fed18c4508dd Mon Sep 17 00:00:00 2001 From: Wayne Date: Mon, 20 Nov 2017 21:32:49 +0800 Subject: [PATCH] Add namespace policy example reference: https://github.com/elabs/pundit/issues/12#issuecomment-345693675 --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index 2d446594..5fcab7f7 100644 --- a/README.md +++ b/README.md @@ -351,6 +351,8 @@ end ## Manually specifying policy classes +### From a given class + Sometimes you might want to explicitly declare which policy to use for a given class, instead of letting Pundit infer it. This can be done like so: @@ -362,6 +364,38 @@ class Post end ``` +### From the place you call `authorize` + +Sometimes you might want to apply namespace for your policy, especially when you +have namespace for your controller, you can simply override `policy(record)` method +from you controller. + +Here is an example for rails controllers: + +```ruby +# app/controllers/api/v1/base_controller.rb +class Api::V1::BaseController < ApplicationController + include Pundit + + private + + def policy(record) + "#{controller_path.classify}Policy".constantize + end +end +``` + +And add your policy + +```ruby +# app/policies/api/v1/auth/offer_policy.rb +class Api::V1::Auth::OfferPolicy < Api::V1::BasePolicy + def index? + true + end +end +``` + ## Just plain old Ruby As you can see, Pundit doesn't do anything you couldn't have easily done