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

Cache policies and policy scopes, closes #134

This commit is contained in:
Jonas Nicklas 2014-08-22 13:20:32 +02:00
parent 7ecdde65b3
commit 698c9b5cb8
2 changed files with 11 additions and 7 deletions

View file

@ -79,16 +79,20 @@ module Pundit
def policy_scope(scope)
@_policy_scoped = true
@policy_scope or Pundit.policy_scope!(pundit_user, scope)
policy_scopes[scope] ||= Pundit.policy_scope!(pundit_user, scope)
end
attr_writer :policy_scope
def policy(record)
@_policy or Pundit.policy!(pundit_user, record)
policies[record] ||= Pundit.policy!(pundit_user, record)
end
def policy=(policy)
@_policy = policy
def policies
@_policies ||= {}
end
def policy_scopes
@_policy_scopes ||= {}
end
def pundit_user

View file

@ -212,7 +212,7 @@ describe Pundit do
it "allows policy to be injected" do
new_policy = OpenStruct.new
controller.policy = new_policy
controller.policies[post] = new_policy
expect(controller.policy(post)).to eq new_policy
end
@ -229,9 +229,9 @@ describe Pundit do
it "allows policy_scope to be injected" do
new_scope = OpenStruct.new
controller.policy_scope = new_scope
controller.policy_scopes[Post] = new_scope
expect(controller.policy_scope(post)).to eq new_scope
expect(controller.policy_scope(Post)).to eq new_scope
end
end
end