1
0
Fork 0
mirror of https://github.com/varvet/pundit.git synced 2022-11-09 12:30:11 -05:00

Custom pundit user

This commit is contained in:
Philip Vieira 2013-07-13 05:42:34 +02:00
parent 60a249f4e6
commit d7ef22bb97
3 changed files with 24 additions and 2 deletions

View file

@ -303,6 +303,18 @@ Pundit.policy_scope(user, Post)
The bang methods will raise an exception if the policy does not exist, whereas The bang methods will raise an exception if the policy does not exist, whereas
those without the bang will return nil. those without the bang will return nil.
## Customize pundit user
In some cases your controller might not have access to `current_user`, or your
`current_user` is not the method one that should be invoked by pundit. Simply
define a method in your controller called `pundit_user`.
```ruby
def pundit_user
User.find_by_other_means
end
```
## Pundit and strong_parameters ## Pundit and strong_parameters
In Rails 3 using [strong_parameters](https://github.com/rails/strong_parameters) In Rails 3 using [strong_parameters](https://github.com/rails/strong_parameters)

View file

@ -61,10 +61,14 @@ module Pundit
def policy_scope(scope) def policy_scope(scope)
@_policy_scoped = true @_policy_scoped = true
Pundit.policy_scope!(current_user, scope) Pundit.policy_scope!(pundit_user, scope)
end end
def policy(record) def policy(record)
Pundit.policy!(current_user, record) Pundit.policy!(pundit_user, record)
end
def pundit_user
respond_to?(:current_user) ? current_user : nil
end end
end end

View file

@ -226,6 +226,12 @@ describe Pundit do
end end
end end
describe "#pundit_user" do
it 'returns the same thing as current_user' do
controller.pundit_user.should eq controller.current_user
end
end
describe ".policy" do describe ".policy" do
it "returns an instantiated policy" do it "returns an instantiated policy" do
policy = controller.policy(post) policy = controller.policy(post)