8000 Make sure Pundit auth policy target is undecorated · activeadmin/activeadmin@382de81 · GitHub
[go: up one dir, main page]

Skip to content

Commit 382de81

Browse files
committed
Make sure Pundit auth policy target is undecorated
When retrieving auth policies it is unfortunate if the policy is wrapped in a decorator. This uses an existing undecoration method to undecorate the target before asking pundit to fetch the policy. Related to #7933
1 parent e2cb957 commit 382de81

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/active_admin/pundit_adapter.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def policy_target(subject)
5656
case subject
5757
when nil then resource
5858
when Class then subject.new
59-
else subject
59+
else undecorate(subject)
6060
end
6161
end
6262

@@ -118,6 +118,8 @@ def policies
118118
@policies ||= {}
119119
end
120120

121+
def undecorate(subject)
122+
ResourceController::Decorators.undecorate(subject)
123+
end
121124
end
122-
123125
end

spec/unit/pundit_adapter_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,38 @@ def resolve
130130
end
131131
end
132132

133+
context "when decorator name contains policy namespace name" do
134+
before do
135+
allow(ActiveAdmin.application).to receive(:pundit_policy_namespace).and_return :foobar
136+
end
137+
138+
class Admin::PostDecorator
139+
attr_reader :object
140+
delegate_missing_to :object
141+
142+
def initialize(object)
143+
@object = object
144+
end
145+
146+
def decorated?
147+
true
148+
end
149+
150+
def model
151+
object
152+
end
153+
end
154+
155+
it "asks Pundit for the policy for the decorated object" do
156+
policy = DefaultPolicy.new(double, double)
157+
allow(policy).to receive(:show?).and_return(true)
158+
159+
expect(Pundit).to receive(:policy).with(anything, [:foobar, an_instance_of(Post)]).and_return(policy)
160+
161+
auth.authorized?(:read, Admin::PostDecorator.new(Post.new))
162+
end
163+
end
164+
133165
context "when Pundit is unable to find policy scope" do
134166
let(:collection) { double("collection", to_sym: :collection) }
135167
subject(:scope) { auth.scope_collection(collection, :read) }

0 commit comments

Comments
 (0)
0