mirror of
https://github.com/varvet/pundit.git
synced 2022-11-09 12:30:11 -05:00
Raise InvalidConstructorError when policy and policy_scope raise ArgumentError
This commit is contained in:
parent
7d8de7871d
commit
8fc620159f
1 changed files with 12 additions and 2 deletions
|
@ -82,6 +82,8 @@ module Pundit
|
|||
def policy_scope(user, scope)
|
||||
policy_scope = PolicyFinder.new(scope).scope
|
||||
policy_scope.new(user, scope).resolve if policy_scope
|
||||
rescue ArgumentError
|
||||
raise InvalidConstructorError, "Invalid #{policy_scope.class} constructor is called."
|
||||
end
|
||||
|
||||
# Retrieves the policy scope for the given record.
|
||||
|
@ -92,7 +94,10 @@ module Pundit
|
|||
# @raise [NotDefinedError] if the policy scope cannot be found
|
||||
# @return [Scope{#resolve}] instance of scope class which can resolve to a scope
|
||||
def policy_scope!(user, scope)
|
||||
PolicyFinder.new(scope).scope!.new(user, scope).resolve
|
||||
policy_scope = PolicyFinder.new(scope).scope!
|
||||
policy_scope.new(user, scope).resolve
|
||||
rescue ArgumentError
|
||||
raise InvalidConstructorError, "Invalid #{policy_scope.class} constructor is called."
|
||||
end
|
||||
|
||||
# Retrieves the policy for the given record.
|
||||
|
@ -104,6 +109,8 @@ module Pundit
|
|||
def policy(user, record)
|
||||
policy = PolicyFinder.new(record).policy
|
||||
policy.new(user, record) if policy
|
||||
rescue ArgumentError
|
||||
raise InvalidConstructorError, "Invalid #{policy.class} constructor is called."
|
||||
end
|
||||
|
||||
# Retrieves the policy for the given record.
|
||||
|
@ -114,7 +121,10 @@ module Pundit
|
|||
# @raise [NotDefinedError] if the policy cannot be found
|
||||
# @return [Object] instance of policy class with query methods
|
||||
def policy!(user, record)
|
||||
PolicyFinder.new(record).policy!.new(user, record)
|
||||
policy = PolicyFinder.new(record).policy!
|
||||
policy.new(user, record)
|
||||
rescue ArgumentError
|
||||
raise InvalidConstructorError, "Invalid #{policy.class} constructor is called."
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue