Skip to content

Instantly share code, notes, and snippets.

@gamecreature
Created May 21, 2019 10:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamecreature/7d180e4704ae478d6a80b3d7069964f9 to your computer and use it in GitHub Desktop.
Save gamecreature/7d180e4704ae478d6a80b3d7069964f9 to your computer and use it in GitHub Desktop.
Pundit Helper to invoke different resolve method in Pundit scope class
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