Created
May 21, 2019 10:23
-
-
Save gamecreature/7d180e4704ae478d6a80b3d7069964f9 to your computer and use it in GitHub Desktop.
Pundit Helper to invoke different resolve method in Pundit scope class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PunditExtensionHelper | |
# this method is the same as the pundit_scope method | |
# the difference is, it doesn't invoke the resolve method | |
def policy_scoper(scope_in, method = :resolve) | |
scope = scope_in.is_a?(Array) ? scope_in.last : scope_in | |
policy_scope_class = Pundit::PolicyFinder.new(scope).scope! | |
return unless policy_scope_class | |
begin | |
scoper = policy_scope_class.new(pundit_user, scope) | |
rescue ArgumentError | |
raise InvalidConstructorError, "Invalid #<#{policy_scope_class}> constructor is called" | |
end | |
return scoper unless method | |
skip_policy_scope | |
scoper.send(method) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment