Skip to content

Instantly share code, notes, and snippets.

@palkan
Created July 25, 2015 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save palkan/352ec378fb7acd9bb68a to your computer and use it in GitHub Desktop.
Save palkan/352ec378fb7acd9bb68a to your computer and use it in GitHub Desktop.
Pundit#authorize_with
# Auhtorize any record to any policy
def authorize_with(klass, record, query = nil)
query ||= "#{action_name}?".to_sym
@_pundit_policy_authorized = @_policy_authorized = true # two vars for forward compatibility
policy = klass.new(pundit_user, record)
unless policy.public_send(query)
error = Pundit::NotAuthorizedError.new("not allowed to #{query} this #{record}")
error.query = query
error.record = record
error.policy = policy
fail error
end
true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment