diff --git a/lib/pundit/policy_finder.rb b/lib/pundit/policy_finder.rb index ec2310a..a4114e6 100644 --- a/lib/pundit/policy_finder.rb +++ b/lib/pundit/policy_finder.rb @@ -44,6 +44,8 @@ module Pundit object elsif object.is_a?(Symbol) object.to_s.classify + elsif object.is_a?(Array) + object.map(&:to_s).join('/').to_s.classify else object.class end diff --git a/spec/pundit_spec.rb b/spec/pundit_spec.rb index 4dba32b..ccd248f 100644 --- a/spec/pundit_spec.rb +++ b/spec/pundit_spec.rb @@ -102,6 +102,13 @@ describe Pundit do expect(policy.user).to eq user expect(policy.dashboard).to eq :dashboard end + + it "returns an instantiated policy given an array" do + policy = Pundit.policy(user, [:project, :dashboard]) + expect(policy.class).to eq Project::DashboardPolicy + expect(policy.user).to eq user + expect(policy.dashboard).to eq [:project, :dashboard] + end end end @@ -137,6 +144,13 @@ describe Pundit do expect(policy.dashboard).to eq :dashboard end + it "returns an instantiated policy given an array" do + policy = Pundit.policy!(user, [:project, :dashboard]) + expect(policy.class).to eq Project::DashboardPolicy + expect(policy.user).to eq user + expect(policy.dashboard).to eq [:project, :dashboard] + end + it "throws an exception if the given policy can't be found" do expect { Pundit.policy!(user, article) }.to raise_error(Pundit::NotDefinedError) expect { Pundit.policy!(user, Article) }.to raise_error(Pundit::NotDefinedError) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e86278a..688e21f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -85,6 +85,10 @@ end class DashboardPolicy < Struct.new(:user, :dashboard); end +module Project + class DashboardPolicy < Struct.new(:user, :dashboard); end +end + class Controller include Pundit