Hello I am trying to access a Pundit
# avo-2
f
Hello, I am trying to access a Pundit policy from a resource. The method
policy()
is not accessible. I want to hide a field depending on :
, visible: ->(resource:) { policy(resource.model).show_accounting? }
l
try using
resource.authorization
. that should give you the policy
f
Thanks @lemon-wall-20836
resource.authorization returns Avo::Services::AuthorizationService
l
that should be an instance of that class
you can then use
resource.authorization.authorize_action(:index?)
f
I would like to access a custom method from the FooPolicy class def show_accounting? record.state_name.in?(%i[signed checked_in checked_out]) end
not a standard action
my authorizations are more granular
l
doesn't
resource.authorization.authorize_action(:show_accounting?)
work?
the
authorize_action
method should forward to the pundit policy
f
This is very strange, this is my code: tab 'Balance' do panel do row do field :balance_sheet, as: :text, hide_on: :forms, format_using: -> { number_to_currency(value, unit: "#{model.currency} ") }, name: 'Current Balance', visible: ->(resource:) { resource.authorization.authorize_action(:show_accounting?) } # FIXME : not working end end
then the whole form is not accessible, I cannot even create a new item
l
using this

https://cdn.discordapp.com/attachments/1136530471979855894/1136562330361147462/CleanShot_2023-08-03_at_10.32.482x.png

it works on that setup
you have
hide_on: :forms
option
f
yes, this is a calculated field on my model
i will dig in more details then
l
in theory this isn't a computed field. the computed fields end in a block
field .... do record.some_value end
if nothing pans out create a reproduction repo so we can have a look
f
@lemon-wall-20836 I could reproduce (what I think is) the bug. I am filling an issue with a reproduction repo
l
thank you!
f