Last active
November 2, 2018 16:22
-
-
Save palkan/eb6fab36c5f60e899cccacd3d5649a93 to your computer and use it in GitHub Desktop.
ActionPolicy i18n basics
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
# See issue: https://github.com/palkan/action_policy/issues/15 | |
module ActionPolicy | |
module I18n | |
class << self | |
def full_message(policy_class, rule) | |
# generate candidates | |
candidates = [:"#{policy_class.identifier}.#{rule}"] | |
# add global fallbacks | |
candidates << rule # e.g. "action_policy.policy.index?" | |
candidates << ::I18n.t(:unauthorized, scope: [:action_policy]) | |
::I18n.t( | |
candidates.shift, | |
default: candidates, | |
scope: [:action_policy, :policy] | |
) | |
end | |
end | |
end | |
end | |
ActionPolicy::Policy::ExecutionResult.include(Module.new do | |
def message | |
ActionPolicy::I18n.full_message(policy, rule) | |
end | |
end) | |
ActionPolicy::Policy::FailureReasons.include(Module.new do | |
def full_messages | |
reasons.flat_map do |policy_klass, rules| | |
rules.map { |rule| ActionPolicy::I18n.full_message(policy_klass, rule) } | |
end | |
end | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment