mirror of
https://github.com/varvet/pundit.git
synced 2022-11-09 12:30:11 -05:00
Adds support for policy_class
model instance/class for custom Policy
This commit is contained in:
parent
1a257bdcb1
commit
4fc13620ee
2 changed files with 26 additions and 1 deletions
|
@ -7,7 +7,11 @@ module Pundit
|
|||
end
|
||||
|
||||
def name
|
||||
if object.respond_to?(:model_name)
|
||||
if object.respond_to?(:policy_class)
|
||||
object.policy_class.name.gsub(/Policy$/, '')
|
||||
elsif object.class.respond_to?(:policy_class)
|
||||
object.class.policy_class.name.gsub(/Policy$/, '')
|
||||
elsif object.respond_to?(:model_name)
|
||||
object.model_name.to_s
|
||||
elsif object.class.respond_to?(:model_name)
|
||||
object.class.model_name.to_s
|
||||
|
|
|
@ -34,12 +34,21 @@ class Comment; extend ActiveModel::Naming; end
|
|||
|
||||
class Article; end
|
||||
|
||||
class BlogPolicy < Struct.new(:user, :blog); end
|
||||
class Blog; end
|
||||
class ArtificialBlog < Blog
|
||||
def self.policy_class
|
||||
BlogPolicy
|
||||
end
|
||||
end
|
||||
|
||||
describe Pundit do
|
||||
let(:user) { stub }
|
||||
let(:post) { Post.new(user) }
|
||||
let(:comment) { Comment.new }
|
||||
let(:article) { Article.new }
|
||||
let(:controller) { stub(:current_user => user, :params => { :action => "update" }).tap { |c| c.extend(Pundit) } }
|
||||
let(:artificial_blog) { ArtificialBlog.new }
|
||||
|
||||
describe ".policy_scope" do
|
||||
it "returns an instantiated policy scope given a plain model class" do
|
||||
|
@ -76,6 +85,18 @@ describe Pundit do
|
|||
policy.post.should == post
|
||||
end
|
||||
|
||||
it "returns an instantiated policy given a plain model instance with policy_class class method set" do
|
||||
policy = Pundit.policy(user, artificial_blog)
|
||||
policy.user.should == user
|
||||
policy.blog.should == artificial_blog
|
||||
end
|
||||
|
||||
it "returns an instantiated policy given a plain model class with policy_class class method set" do
|
||||
policy = Pundit.policy(user, ArtificialBlog)
|
||||
policy.user.should == user
|
||||
policy.blog.should == ArtificialBlog
|
||||
end
|
||||
|
||||
it "returns an instantiated policy given an active model instance" do
|
||||
policy = Pundit.policy(user, comment)
|
||||
policy.user.should == user
|
||||
|
|
Loading…
Reference in a new issue