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

Pass the last thing in array to policy

When passing an array to Pundit to specify namespacing
we should only pass the last element of the array to the
policy.
This commit is contained in:
Linus Marton 2018-06-17 09:18:41 +02:00
parent aca4dcb965
commit 33424381c1
No known key found for this signature in database
GPG key ID: 3AE26AD3E41F5ED8
3 changed files with 17 additions and 11 deletions

View file

@ -109,7 +109,7 @@ module Pundit
# @return [Object, nil] instance of policy class with query methods
def policy(user, record)
policy = PolicyFinder.new(record).policy
policy.new(user, record) if policy
policy.new(user, pundit_model(record)) if policy
rescue ArgumentError
raise InvalidConstructorError, "Invalid #<#{policy}> constructor is called"
end
@ -124,10 +124,16 @@ module Pundit
# @return [Object] instance of policy class with query methods
def policy!(user, record)
policy = PolicyFinder.new(record).policy!
policy.new(user, record)
policy.new(user, pundit_model(record)) if policy
rescue ArgumentError
raise InvalidConstructorError, "Invalid #<#{policy}> constructor is called"
end
private
def pundit_model(record)
record.is_a?(Array) ? record.last : record
end
end
# @api private

View file

@ -144,35 +144,35 @@ describe Pundit do
policy = Pundit.policy(user, %i[project criteria])
expect(policy.class).to eq Project::CriteriaPolicy
expect(policy.user).to eq user
expect(policy.criteria).to eq %i[project criteria]
expect(policy.criteria).to eq :criteria
end
it "returns an instantiated policy given an array of a symbol and plain model instance" do
policy = Pundit.policy(user, [:project, post])
expect(policy.class).to eq Project::PostPolicy
expect(policy.user).to eq user
expect(policy.post).to eq [:project, post]
expect(policy.post).to eq post
end
it "returns an instantiated policy given an array of a symbol and a model instance with policy_class override" do
policy = Pundit.policy(user, [:project, customer_post])
expect(policy.class).to eq Project::PostPolicy
expect(policy.user).to eq user
expect(policy.post).to eq [:project, customer_post]
expect(policy.post).to eq customer_post
end
it "returns an instantiated policy given an array of a symbol and an active model instance" do
policy = Pundit.policy(user, [:project, comment])
expect(policy.class).to eq Project::CommentPolicy
expect(policy.user).to eq user
expect(policy.post).to eq [:project, comment]
expect(policy.comment).to eq comment
end
it "returns an instantiated policy given an array of a symbol and a plain model class" do
policy = Pundit.policy(user, [:project, Post])
expect(policy.class).to eq Project::PostPolicy
expect(policy.user).to eq user
expect(policy.post).to eq [:project, Post]
expect(policy.post).to eq Post
end
it "raises an error with a invalid policy constructor" do
@ -185,14 +185,14 @@ describe Pundit do
policy = Pundit.policy(user, [:project, Comment])
expect(policy.class).to eq Project::CommentPolicy
expect(policy.user).to eq user
expect(policy.post).to eq [:project, Comment]
expect(policy.comment).to eq Comment
end
it "returns an instantiated policy given an array of a symbol and a class with policy_class override" do
policy = Pundit.policy(user, [:project, Customer::Post])
expect(policy.class).to eq Project::PostPolicy
expect(policy.user).to eq user
expect(policy.post).to eq [:project, Customer::Post]
expect(policy.post).to eq Customer::Post
end
it "returns correct policy class for an array of a multi-word symbols" do
@ -312,7 +312,7 @@ describe Pundit do
policy = Pundit.policy!(user, %i[project criteria])
expect(policy.class).to eq Project::CriteriaPolicy
expect(policy.user).to eq user
expect(policy.criteria).to eq %i[project criteria]
expect(policy.criteria).to eq :criteria
end
it "throws an exception if the given policy can't be found" do

View file

@ -155,7 +155,7 @@ end
class CriteriaPolicy < Struct.new(:user, :criteria); end
module Project
class CommentPolicy < Struct.new(:user, :post); end
class CommentPolicy < Struct.new(:user, :comment); end
class CriteriaPolicy < Struct.new(:user, :criteria); end
class PostPolicy < Struct.new(:user, :post); end
end