From 9711b4896b175fbebec3d5d090821d9ce5fc6728 Mon Sep 17 00:00:00 2001 From: Sergey Stupachenko Date: Sat, 11 Oct 2014 16:01:45 +0300 Subject: [PATCH] Add array-based syntax for namespaced headless policies Before to namespace headless policy we needed to use following syntax: authorize :'project/dashboard' Now we can use array to specify each constant separately so it looks cleaner authorize [:project, :dashboard] --- lib/pundit/policy_finder.rb | 2 ++ spec/pundit_spec.rb | 14 ++++++++++++++ spec/spec_helper.rb | 4 ++++ 3 files changed, 20 insertions(+) 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