Skip to content

Commit

Permalink
Add array-based syntax for namespaced headless policies
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
RaZer committed Oct 11, 2014
1 parent 2214acf commit 9711b48
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/pundit/policy_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def find
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
Expand Down
14 changes: 14 additions & 0 deletions spec/pundit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@
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

Expand Down Expand Up @@ -137,6 +144,13 @@
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)
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def destroy?

class DashboardPolicy < Struct.new(:user, :dashboard); end

module Project
class DashboardPolicy < Struct.new(:user, :dashboard); end
end

class Controller
include Pundit

Expand Down

0 comments on commit 9711b48

Please sign in to comment.