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

Merge pull request #475 from ryanhertz/fix-arrays

fix PolicyFinder when given an array with policy class override
This commit is contained in:
Linus Marton 2018-05-31 13:36:34 +02:00 committed by GitHub
commit a0124c523b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 15 deletions

View file

@ -36,7 +36,7 @@ module Pundit
# policy.update? #=> false
#
def policy
klass = find
klass = find(object)
klass = klass.constantize if klass.is_a?(String)
klass
rescue NameError
@ -48,7 +48,7 @@ module Pundit
#
def scope!
raise NotDefinedError, "unable to find policy scope of nil" if object.nil?
scope or raise NotDefinedError, "unable to find scope `#{find}::Scope` for `#{object.inspect}`"
scope or raise NotDefinedError, "unable to find scope `#{find(object)}::Scope` for `#{object.inspect}`"
end
# @return [Class] policy class with query methods
@ -56,7 +56,7 @@ module Pundit
#
def policy!
raise NotDefinedError, "unable to find policy of nil" if object.nil?
policy or raise NotDefinedError, "unable to find policy `#{find}` for `#{object.inspect}`"
policy or raise NotDefinedError, "unable to find policy `#{find(object)}` for `#{object.inspect}`"
end
# @return [String] the name of the key this object would have in a params hash
@ -73,19 +73,20 @@ module Pundit
private
def find
if object.nil?
def find(subject)
if subject.nil?
nil
elsif object.respond_to?(:policy_class)
object.policy_class
elsif object.class.respond_to?(:policy_class)
object.class.policy_class
elsif subject.is_a?(Array)
modules = subject.dup
last = modules.pop
context = modules.map { |x| find_class_name(x) }.join("::")
[context, find(last)].join("::")
elsif subject.respond_to?(:policy_class)
subject.policy_class
elsif subject.class.respond_to?(:policy_class)
subject.class.policy_class
else
klass = if object.is_a?(Array)
object.map { |x| find_class_name(x) }.join("::")
else
find_class_name(object)
end
klass = find_class_name(subject)
"#{klass}#{SUFFIX}"
end
end

View file

@ -154,6 +154,13 @@ describe Pundit do
expect(policy.post).to eq [:project, 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]
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
@ -181,6 +188,13 @@ describe Pundit do
expect(policy.post).to eq [:project, 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]
end
it "returns correct policy class for an array of a multi-word symbols" do
policy = Pundit.policy(user, %i[project_one_two_three criteria_four_five_six])
expect(policy.class).to eq ProjectOneTwoThree::CriteriaFourFiveSixPolicy

View file

@ -77,9 +77,13 @@ module Customer
OpenStruct.new(param_key: "customer_post")
end
def policy_class
def self.policy_class
PostPolicy
end
def policy_class
self.class.policy_class
end
end
end