Skip to content

Commit

Permalink
Add namespace policy example
Browse files Browse the repository at this point in the history
  • Loading branch information
wayne5540 committed Nov 20, 2017
1 parent ac2a25d commit 81b9bca
Showing 1 changed file with 34 additions and 0 deletions.
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 a 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
Expand Down

0 comments on commit 81b9bca

Please sign in to comment.