diff --git a/CHANGELOG.md b/CHANGELOG.md index 092bcc4..c61ffce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## master +- Fix missing details in deny! message interpolation. ([@palkan][]) + - Fix implicit authorization target in anonymous controllers. ([@palkan][]) - Improve default `ActionPolicy::Unauthorized` error message. ([@Spone][]) diff --git a/lib/action_policy/policy/reasons.rb b/lib/action_policy/policy/reasons.rb index 7ab1d46..72fa74f 100644 --- a/lib/action_policy/policy/reasons.rb +++ b/lib/action_policy/policy/reasons.rb @@ -220,7 +220,7 @@ def allowed_to?(rule, record = :__undef__, inline_reasons: false, **options) end def deny!(reason = nil) - result&.reasons&.add(self, reason) if reason + result&.reasons&.add(self, reason, result.details) if reason super() end end diff --git a/test/action_policy/i18n_test.rb b/test/action_policy/i18n_test.rb index b460fc1..4d5c467 100644 --- a/test/action_policy/i18n_test.rb +++ b/test/action_policy/i18n_test.rb @@ -209,6 +209,12 @@ def admin? details[:role] = user.name user.admin? end + + def deny? + details[:role] = user.name + deny!(:denied) unless user.admin? + allow! + end end class TestI18nNestedPolicies < Minitest::Test @@ -217,6 +223,7 @@ def setup :en, action_policy: { policy: { + denied: "I deny you: %{role}", i18n_nested: { show?: "Stop looking at me!", admin?: "Only for admins but you are: %{role}" @@ -258,4 +265,10 @@ def test_full_messages_for_nested_policy assert_includes policy.result.reasons.full_messages, "Only for admins but you are: guest" end + + def test_result_message_with_deny_and_interpolation + refute policy.apply(:deny?) + assert_includes policy.result.reasons.full_messages, + "I deny you: guest" + end end