Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Update Readme Only] Add namespace policy example #497

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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)
policies[record] ||= "#{controller_path.classify}Policy".constantize.new(pundit_user, record)
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
Expand Down