Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ramaboo/pundit into ramab…
Browse files Browse the repository at this point in the history
…oo-master
  • Loading branch information
jnicklas committed Jan 14, 2016
2 parents 1d070d1 + 4f22dc1 commit 12df094
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 26 deletions.
28 changes: 17 additions & 11 deletions lib/pundit/policy_finder.rb
Expand Up @@ -40,21 +40,27 @@ def find
elsif object.class.respond_to?(:policy_class)
object.class.policy_class
else
klass = if object.respond_to?(:model_name)
object.model_name
elsif object.class.respond_to?(:model_name)
object.class.model_name
elsif object.is_a?(Class)
object
elsif object.is_a?(Symbol)
object.to_s.camelize
elsif object.is_a?(Array)
object.join('/').camelize
klass = if object.is_a?(Array)
object.map { |x| find_class_name(x) }.join('::')
else
object.class
find_class_name(object)
end
"#{klass}#{SUFFIX}"
end
end

def find_class_name(subject)
if subject.respond_to?(:model_name)
subject.model_name
elsif subject.class.respond_to?(:model_name)
subject.class.model_name
elsif subject.is_a?(Class)
subject
elsif subject.is_a?(Symbol)
subject.to_s.camelize
else
subject.class
end
end
end
end
107 changes: 92 additions & 15 deletions spec/pundit_spec.rb
Expand Up @@ -4,13 +4,17 @@
let(:user) { double }
let(:post) { Post.new(user) }
let(:customer_post) { Customer::Post.new(user) }
let(:post_four_five_six) { PostFourFiveSix.new(user) }
let(:comment) { Comment.new }
let(:comment_four_five_six) { CommentFourFiveSix.new }
let(:article) { Article.new }
let(:controller) { Controller.new(user, { :action => 'update' }) }
let(:artificial_blog) { ArtificialBlog.new }
let(:article_tag) { ArticleTag.new }
let(:comments_relation) { CommentsRelation.new }
let(:empty_comments_relation) { CommentsRelation.new(true) }
let(:tag_four_five_six) { ProjectOneTwoThree::TagFourFiveSix.new(user) }
let(:avatar_four_five_six) { ProjectOneTwoThree::AvatarFourFiveSix.new }

describe ".authorize" do
it "infers the policy and authorizes based on it" do
Expand Down Expand Up @@ -104,6 +108,93 @@
expect(policy.comment).to eq Comment
end

it "returns an instantiated policy given a symbol" do
policy = Pundit.policy(user, :criteria)
expect(policy.class).to eq CriteriaPolicy
expect(policy.user).to eq user
expect(policy.criteria).to eq :criteria
end

it "returns an instantiated policy given an array of symbols" do
policy = Pundit.policy(user, [:project, :criteria])
expect(policy.class).to eq Project::CriteriaPolicy
expect(policy.user).to eq user
expect(policy.criteria).to eq [:project, :criteria]
end

it "returns an instantiated policy given an array of a symbol and plain model instance" do
policy = Pundit.policy(user, [:project, post])
expect(policy.class).to eq Project::PostPolicy
expect(policy.user).to eq user
expect(policy.post).to eq [:project, post]
end

it "returns an instantiated policy given an array of a symbol and an active model instance" do
policy = Pundit.policy(user, [:project, comment])
expect(policy.class).to eq Project::CommentPolicy
expect(policy.user).to eq user
expect(policy.post).to eq [:project, comment]
end

it "returns an instantiated policy given an array of a symbol and a plain model class" do
policy = Pundit.policy(user, [:project, Post])
expect(policy.class).to eq Project::PostPolicy
expect(policy.user).to eq user
expect(policy.post).to eq [:project, Post]
end

it "returns an instantiated policy given an array of a symbol and an active model class" do
policy = Pundit.policy(user, [:project, Comment])
expect(policy.class).to eq Project::CommentPolicy
expect(policy.user).to eq user
expect(policy.post).to eq [:project, Comment]
end

it "returns correct policy class for an array of a multi-word symbols" do
policy = Pundit.policy(user, [:project_one_two_three, :criteria_four_five_six])
expect(policy.class).to eq ProjectOneTwoThree::CriteriaFourFiveSixPolicy
end

it "returns correct policy class for an array of a multi-word symbol and a multi-word plain model instance" do
policy = Pundit.policy(user, [:project_one_two_three, post_four_five_six])
expect(policy.class).to eq ProjectOneTwoThree::PostFourFiveSixPolicy
end

it "returns correct policy class for an array of a multi-word symbol and a multi-word active model instance" do
policy = Pundit.policy(user, [:project_one_two_three, comment_four_five_six])
expect(policy.class).to eq ProjectOneTwoThree::CommentFourFiveSixPolicy
end

it "returns correct policy class for an array of a multi-word symbol and a multi-word plain model class" do
policy = Pundit.policy(user, [:project_one_two_three, PostFourFiveSix])
expect(policy.class).to eq ProjectOneTwoThree::PostFourFiveSixPolicy
end

it "returns correct policy class for an array of a multi-word symbol and a multi-word active model class" do
policy = Pundit.policy(user, [:project_one_two_three, CommentFourFiveSix])
expect(policy.class).to eq ProjectOneTwoThree::CommentFourFiveSixPolicy
end

it "returns correct policy class for a multi-word scoped plain model class" do
policy = Pundit.policy(user, ProjectOneTwoThree::TagFourFiveSix)
expect(policy.class).to eq ProjectOneTwoThree::TagFourFiveSixPolicy
end

it "returns correct policy class for a multi-word scoped plain model instance" do
policy = Pundit.policy(user, tag_four_five_six)
expect(policy.class).to eq ProjectOneTwoThree::TagFourFiveSixPolicy
end

it "returns correct policy class for a multi-word scoped active model class" do
policy = Pundit.policy(user, ProjectOneTwoThree::AvatarFourFiveSix)
expect(policy.class).to eq ProjectOneTwoThree::AvatarFourFiveSixPolicy
end

it "returns correct policy class for a multi-word scoped active model instance" do
policy = Pundit.policy(user, avatar_four_five_six)
expect(policy.class).to eq ProjectOneTwoThree::AvatarFourFiveSixPolicy
end

it "returns nil if the given policy can't be found" do
expect(Pundit.policy(user, article)).to be_nil
expect(Pundit.policy(user, Article)).to be_nil
Expand Down Expand Up @@ -137,20 +228,6 @@
expect(policy.user).to eq user
expect(policy.tag).to eq ArticleTag
end

it "returns an instantiated policy given a symbol" do
policy = Pundit.policy(user, :criteria)
expect(policy.class).to eq CriteriaPolicy
expect(policy.user).to eq user
expect(policy.criteria).to eq :criteria
end

it "returns an instantiated policy given an array" do
policy = Pundit.policy(user, [:project, :criteria])
expect(policy.class).to eq Project::CriteriaPolicy
expect(policy.user).to eq user
expect(policy.criteria).to eq [:project, :criteria]
end
end
end

Expand Down Expand Up @@ -186,7 +263,7 @@
expect(policy.criteria).to eq :criteria
end

it "returns an instantiated policy given an array" do
it "returns an instantiated policy given an array of symbols" do
policy = Pundit.policy!(user, [:project, :criteria])
expect(policy.class).to eq Project::CriteriaPolicy
expect(policy.user).to eq user
Expand Down
15 changes: 15 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -107,7 +107,9 @@ def destroy?
class CriteriaPolicy < Struct.new(:user, :criteria); end

module Project
class CommentPolicy < Struct.new(:user, :post); end
class CriteriaPolicy < Struct.new(:user, :criteria); end
class PostPolicy < Struct.new(:user, :post); end
end

class DenierPolicy < Struct.new(:user, :record)
Expand Down Expand Up @@ -138,3 +140,16 @@ def initialize(*)
raise "I'm only here to be annoying!"
end
end

class PostFourFiveSix < Struct.new(:user); end
class CommentFourFiveSix; extend ActiveModel::Naming; end

module ProjectOneTwoThree
class CommentFourFiveSixPolicy < Struct.new(:user, :post); end
class CriteriaFourFiveSixPolicy < Struct.new(:user, :criteria); end
class PostFourFiveSixPolicy < Struct.new(:user, :post); end
class TagFourFiveSix < Struct.new(:user); end
class TagFourFiveSixPolicy < Struct.new(:user, :tag); end
class AvatarFourFiveSix; extend ActiveModel::Naming; end
class AvatarFourFiveSixPolicy < Struct.new(:user, :avatar); end
end

0 comments on commit 12df094

Please sign in to comment.