mirror of
https://github.com/varvet/pundit.git
synced 2022-11-09 12:30:11 -05:00
Only pass last element of array to policy scope
This commit is contained in:
parent
33424381c1
commit
ac372bcf0d
3 changed files with 39 additions and 5 deletions
|
@ -80,7 +80,7 @@ module Pundit
|
||||||
# @return [Scope{#resolve}, nil] instance of scope class which can resolve to a scope
|
# @return [Scope{#resolve}, nil] instance of scope class which can resolve to a scope
|
||||||
def policy_scope(user, scope)
|
def policy_scope(user, scope)
|
||||||
policy_scope = PolicyFinder.new(scope).scope
|
policy_scope = PolicyFinder.new(scope).scope
|
||||||
policy_scope.new(user, scope).resolve if policy_scope
|
policy_scope.new(user, pundit_model(scope)).resolve if policy_scope
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
raise InvalidConstructorError, "Invalid #<#{policy_scope}> constructor is called"
|
raise InvalidConstructorError, "Invalid #<#{policy_scope}> constructor is called"
|
||||||
end
|
end
|
||||||
|
@ -95,7 +95,7 @@ module Pundit
|
||||||
# @return [Scope{#resolve}] instance of scope class which can resolve to a scope
|
# @return [Scope{#resolve}] instance of scope class which can resolve to a scope
|
||||||
def policy_scope!(user, scope)
|
def policy_scope!(user, scope)
|
||||||
policy_scope = PolicyFinder.new(scope).scope!
|
policy_scope = PolicyFinder.new(scope).scope!
|
||||||
policy_scope.new(user, scope).resolve
|
policy_scope.new(user, pundit_model(scope)).resolve
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
raise InvalidConstructorError, "Invalid #<#{policy_scope}> constructor is called"
|
raise InvalidConstructorError, "Invalid #<#{policy_scope}> constructor is called"
|
||||||
end
|
end
|
||||||
|
@ -124,7 +124,7 @@ module Pundit
|
||||||
# @return [Object] instance of policy class with query methods
|
# @return [Object] instance of policy class with query methods
|
||||||
def policy!(user, record)
|
def policy!(user, record)
|
||||||
policy = PolicyFinder.new(record).policy!
|
policy = PolicyFinder.new(record).policy!
|
||||||
policy.new(user, pundit_model(record)) if policy
|
policy.new(user, pundit_model(record))
|
||||||
rescue ArgumentError
|
rescue ArgumentError
|
||||||
raise InvalidConstructorError, "Invalid #<#{policy}> constructor is called"
|
raise InvalidConstructorError, "Invalid #<#{policy}> constructor is called"
|
||||||
end
|
end
|
||||||
|
|
|
@ -63,6 +63,14 @@ describe Pundit do
|
||||||
expect(Pundit.policy_scope(user, empty_comments_relation)).to eq CommentScope.new(empty_comments_relation)
|
expect(Pundit.policy_scope(user, empty_comments_relation)).to eq CommentScope.new(empty_comments_relation)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "returns an instantiated policy scope given an array of a symbol and plain model class" do
|
||||||
|
expect(Pundit.policy_scope(user, [:project, Post])).to eq :read
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns an instantiated policy scope given an array of a symbol and active model class" do
|
||||||
|
expect(Pundit.policy_scope(user, [:project, Comment])).to eq Comment
|
||||||
|
end
|
||||||
|
|
||||||
it "returns nil if the given policy scope can't be found" do
|
it "returns nil if the given policy scope can't be found" do
|
||||||
expect(Pundit.policy_scope(user, Article)).to be_nil
|
expect(Pundit.policy_scope(user, Article)).to be_nil
|
||||||
end
|
end
|
||||||
|
@ -101,6 +109,14 @@ describe Pundit do
|
||||||
end.to raise_error(Pundit::NotDefinedError, "Cannot scope NilClass")
|
end.to raise_error(Pundit::NotDefinedError, "Cannot scope NilClass")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "returns an instantiated policy scope given an array of a symbol and plain model class" do
|
||||||
|
expect(Pundit.policy_scope!(user, [:project, Post])).to eq :read
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns an instantiated policy scope given an array of a symbol and active model class" do
|
||||||
|
expect(Pundit.policy_scope!(user, [:project, Comment])).to eq Comment
|
||||||
|
end
|
||||||
|
|
||||||
it "raises an error with a invalid policy scope constructor" do
|
it "raises an error with a invalid policy scope constructor" do
|
||||||
expect do
|
expect do
|
||||||
Pundit.policy_scope(user, Wiki)
|
Pundit.policy_scope(user, Wiki)
|
||||||
|
|
|
@ -62,6 +62,10 @@ class Post < Struct.new(:user)
|
||||||
:published
|
:published
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.read
|
||||||
|
:read
|
||||||
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
"Post"
|
"Post"
|
||||||
end
|
end
|
||||||
|
@ -155,9 +159,23 @@ end
|
||||||
class CriteriaPolicy < Struct.new(:user, :criteria); end
|
class CriteriaPolicy < Struct.new(:user, :criteria); end
|
||||||
|
|
||||||
module Project
|
module Project
|
||||||
class CommentPolicy < Struct.new(:user, :comment); end
|
class CommentPolicy < Struct.new(:user, :comment)
|
||||||
|
class Scope < Struct.new(:user, :scope)
|
||||||
|
def resolve
|
||||||
|
scope
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class CriteriaPolicy < Struct.new(:user, :criteria); end
|
class CriteriaPolicy < Struct.new(:user, :criteria); end
|
||||||
class PostPolicy < Struct.new(:user, :post); end
|
|
||||||
|
class PostPolicy < Struct.new(:user, :post)
|
||||||
|
class Scope < Struct.new(:user, :scope)
|
||||||
|
def resolve
|
||||||
|
scope.read
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class DenierPolicy < Struct.new(:user, :record)
|
class DenierPolicy < Struct.new(:user, :record)
|
||||||
|
|
Loading…
Reference in a new issue