Created
July 25, 2015 13:51
-
-
Save palkan/352ec378fb7acd9bb68a to your computer and use it in GitHub Desktop.
Pundit#authorize_with
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
# 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