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

View file

@ -154,6 +154,13 @@ describe Pundit do
expect(policy.post).to eq [:project, post] expect(policy.post).to eq [:project, post]
end 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 it "returns an instantiated policy given an array of a symbol and an active model instance" do
policy = Pundit.policy(user, [:project, comment]) policy = Pundit.policy(user, [:project, comment])
expect(policy.class).to eq Project::CommentPolicy expect(policy.class).to eq Project::CommentPolicy
@ -181,6 +188,13 @@ describe Pundit do
expect(policy.post).to eq [:project, Comment] expect(policy.post).to eq [:project, Comment]
end 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 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]) policy = Pundit.policy(user, %i[project_one_two_three criteria_four_five_six])
expect(policy.class).to eq ProjectOneTwoThree::CriteriaFourFiveSixPolicy expect(policy.class).to eq ProjectOneTwoThree::CriteriaFourFiveSixPolicy

View file

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