avo
avo copied to clipboard
Allow Procs on `use_resource` option
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
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".
I don't think there is any difference, same apply to all other options that allow Procs.