Skip to content

Commit ba532bc

Browse files
andrebarretofvpalkan
authored andcommitted
Fix aliases in policies with reasons
Fixes #134
1 parent 0fa8d89 commit ba532bc

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

lib/action_policy/policy/reasons.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,12 @@ def details
185185
def allowed_to?(rule, record = :__undef__, **options)
186186
res =
187187
if (record == :__undef__ || record == self.record) && options.empty?
188+
rule = resolve_rule(rule)
188189
policy = self
189190
with_clean_result { apply(rule) }
190191
else
191192
policy = policy_for(record: record, **options)
193+
rule = policy.resolve_rule(rule)
192194

193195
policy.apply(rule)
194196
policy.result

test/action_policy/policy/aliases_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ def create?
3737
end
3838
end
3939

40+
class AliasesRuleTestBasePolicy < ActionPolicy::Base
41+
def manage?
42+
user.admin?
43+
end
44+
45+
def update_or_destroy?
46+
allowed_to?(:update?) || allowed_to?(:destroy?)
47+
end
48+
end
49+
4050
class TestPolicyAliasesRule < Minitest::Test
4151
def setup
4252
@policy = AliasesRuleTestPolicy.new
@@ -69,6 +79,12 @@ def test_alias_rule_within_policy
6979
assert policy.apply(:update_or_destroy?)
7080
end
7181

82+
def test_alias_rule_within_base_policy
83+
policy = AliasesRuleTestBasePolicy.new(user: User.new("admin"))
84+
85+
assert policy.apply(:update_or_destroy?)
86+
end
87+
7288
if defined?(::DidYouMean::SpellChecker)
7389
def test_suggest
7490
policy = AliasesRuleTestPolicy::NoDefault.new

0 commit comments

Comments
 (0)