Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add array-based syntax for namespaced headless policies #216

Merged
1 commit merged into from
Mar 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this be put more simply as: object.join('/') since to_s is implicitly called or am I missing something?

2.2.0 :001 > [:a, :b].join('/')
 => "a/b" 
2.2.0 :002 > class Foo
2.2.0 :003?>   end
 => nil 
2.2.0 :004 > [Foo.new, :b].join('/')
 => "#<Foo:0x007f861a1686e8>/b" 

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Fixed.

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