-
Notifications
You must be signed in to change notification settings - Fork 643
Closed
Description
Ruby, Rails and pundit versions
Ruby version: 3.0
Rails version: 6.1.4.1
Pundit version: master
How I am using
# app/controllers/admin_controller.rb
class AdminController < ApplicationController
def authorize(record, query = nil, policy_class: nil)
super([:admin, record], query, policy_class: policy_class)
end
end
# app/controllers/admin/posts_controller.rb
class Admin::PostsController < AdminController
def show
@publication = authorize Post.find(params[:id]), policy_class: Admin::PublicationPolicy
end
end
Observed behaviour
Inside Admin::PublicationPolicy
I am getting record array (with namespace).
4: scope.all
5: end
6: end
7:
8: def show?
=> 9: binding.irb
10: end
11: end
3.0.2 :001 > record
=> [:admin, #<Post>]
Expected behaviour
Inside Admin::PublicationPolicy
, I should able to get Post
object.
Metadata
Metadata
Assignees
Labels
No labels
Activity
vivekmiyani commentedon Sep 23, 2021
Happy to raise an PR, if this is really an issue 😃 .
Pass record correctly when using `policy_class`
policy_class
#694policy_class
#697iggant commentedon Feb 18, 2022
Hi all. We have a trouble with current changes:
we were using this feature as current manner:
and inside CountryPolicy:
and now there is no way to get initial_country. Record is always target_country
dgmstuart commentedon Feb 19, 2022
@iggant I'm afraid this doesn't look like a valid use of Pundit?
When passing an array, this is interpreted as namespacing, as documented in the README: https://github.com/varvet/pundit#policy-namespacing
It seems that the thing which made your code work before was a bug in the code for handling namespacing, which was raised in this issue.
Pundit is designed to authorise access to individual resources: passing arbitrary ruby objects (like this array of two objects) is not something we can reasonably support.
I'm not sure I understand your use case without additional context, but in the code you've shown, there's no comparison between the two countries, so I believe it could be implemented like this:
dgmstuart commentedon Feb 19, 2022
@iggant or I guess maybe this?
Without seeing the rest of your code, it's hard to tell what the best way to implement this is, but hopefully you get the idea.