https://avo.cool logo
Restricting the information (not the field) bas...
# fields
l
Restricting the information (not the field) based on policies.
m
Thread automatically created by Lucas Geron in #1141725718364045394
l
Hey @loud-jewelry-99127 , Descobri que você também fala português 🇧🇷 ⛅ Awesome. So, getting back to the topic, I'm looking for a way to restrict the field's information based on its policy. Here are some approaches I've tried so far. ### Field Visibility Hides the entire field (not exactly what I want to do)
Copy code
rb
field :created_by, as: :belongs_to, only_on: [:show], visible: lambda {
  resource.authorization.authorize_action(:view_created_by?, raise_exception: false)
}
### Field Formatting Allows you to restrict the field as needed - also allows using a helper for this, which can help me with this task.
Copy code
rb
field :created_by, as: :text, only_on: [:show], format_using: lambda {
  resource.authorization.authorize_action(:view_created_by?, raise_exception: false) ? link_to(value.name, resources_user_path(value)) : 'Restricted'
}
### Field Block Allows you to restrict the field as needed.
Copy code
rb
field :created_by, as: :text, only_on: [:show] do
  resource.authorization.authorize_action(:view_created_by?, raise_exception: false) ? link_to(record.created_by.name, resources_user_path(record)) : 'Restricted'
end
However, all these approaches use the field
as: :text
, while the ideal would be to keep it as:
as: :belongs_to
, making it cleaner and more readable. I could use something like:
Copy code
rb
field :created_by, as: :belongs_to, only_on: [:show] do
  return nil unless resource.authorization.authorize_action(:view_created_by?, raise_exception: false)
end
However,
nil
and 'exists, but you don't have access' are different things; this means, i want to display to the user that he don't have access to that data instead of giving the impression of a blank value for that field. The question is, is there a better way to do this than this? If so, how?