avo icon indicating copy to clipboard operation
avo copied to clipboard

Allow Procs on `use_resource` option

Open Paul-Bob opened this issue 1 year ago • 2 comments
trafficstars

Feature

More context here https://github.com/avo-hq/avo/issues/2455

use_resource option should allow Procs, example:

field :comments, as: :has_many, use_resource: -> { current_user.is_admin? ? Avo::Resources::Comment : Avo::Resources::PhotoComment }

Current workarounds

Since def fields is a resource's instance method current_user can be directly accessed.

def fields
    comments_resource = current_user.is_admin? ? Avo::Resources::Comment : Avo::Resources::PhotoComment

    field :comments, as: :has_many, use_resource: comments_resource
end

record can also be accessed but be aware that record is not always there, for example on index views, so make sure to use a safe navigation or a presence check.

Approach

use_resource should be executed as a target of Avo::ExecutionContext

Paul-Bob avatar Feb 07 '24 08:02 Paul-Bob

Is there a difference in results if one uses a ternary instead of a block? Because if there aren't differences, then I'd skip this "feature".

adrianthedev avatar Feb 07 '24 14:02 adrianthedev

I don't think there is any difference, same apply to all other options that allow Procs.

Paul-Bob avatar Feb 07 '24 14:02 Paul-Bob