mirror of
https://github.com/varvet/pundit.git
synced 2022-11-09 12:30:11 -05:00
find correct policy when model is namespaced
This commit is contained in:
parent
43ab612837
commit
bf29cae128
3 changed files with 6 additions and 1 deletions
|
@ -15,7 +15,7 @@ module Pundit
|
|||
|
||||
def policy
|
||||
klass = find
|
||||
klass = namespace.const_get(klass) if klass.is_a?(String)
|
||||
klass = namespace.const_get(klass.demodulize) if klass.is_a?(String)
|
||||
klass
|
||||
rescue NameError
|
||||
nil
|
||||
|
|
|
@ -4,6 +4,7 @@ describe Pundit do
|
|||
let(:user) { double }
|
||||
let(:post) { Post.new(user) }
|
||||
let(:comment) { Comment.new }
|
||||
let(:nested_comment) { Admin::Comment.new }
|
||||
let(:article) { Article.new }
|
||||
let(:controller) { Controller.new(user, { :action => 'update' }) }
|
||||
let(:artificial_blog) { ArtificialBlog.new }
|
||||
|
@ -199,6 +200,7 @@ describe Pundit do
|
|||
|
||||
it "looks up the policy class based on the caller's namespace" do
|
||||
expect(nested_controller.policy(comment).class).to eq Admin::CommentPolicy
|
||||
expect(nested_controller.policy(nested_comment).class).to eq Admin::CommentPolicy
|
||||
end
|
||||
|
||||
it "falls back to the non-namespaced policy class if there isn't a namespaced one" do
|
||||
|
|
|
@ -32,6 +32,9 @@ class CommentPolicy::Scope < Struct.new(:user, :scope)
|
|||
end
|
||||
end
|
||||
class Comment; extend ActiveModel::Naming; end
|
||||
module Admin
|
||||
class Comment; end
|
||||
end
|
||||
|
||||
class Article; end
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue