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

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]
This commit is contained in:
Sergey Stupachenko 2014-10-11 16:01:45 +03:00
parent 2214acf661
commit 9711b4896b
3 changed files with 20 additions and 0 deletions

View file

@ -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

View file

@ -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)

View file

@ -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