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:
parent
2214acf661
commit
9711b4896b
3 changed files with 20 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue