Skip to content

Commit

Permalink
fix: add details to deny reasons
Browse files Browse the repository at this point in the history
Fixes #276
  • Loading branch information
palkan committed Nov 21, 2024
1 parent 29048c0 commit 4b60048
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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][])
Expand Down
2 changes: 1 addition & 1 deletion lib/action_policy/policy/reasons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test/action_policy/i18n_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand Down Expand Up @@ -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

0 comments on commit 4b60048

Please sign in to comment.