avo-hq / avo

Build Ruby on Rails apps 10x faster
https://avohq.io
Other
1.51k stars 242 forks source link

Polymorphic belongs field to does not work in actions #3075

Open iainbeeston opened 2 months ago

iainbeeston commented 2 months ago

Describe the bug

I'd like to use a polymorphic belongs to field in an action. I would expect if I do that, the action handle method will receive a type and an id value for that field. However, it only receives a type value (no id) and that produces an error.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Create a new action
  2. Add a fields block with a polymorphic belongs to, for example:
def fields
  field :reviewable, as: :belongs_to, polymorphic_as: :reviewable, types: [::Post, ::Team]
end

and a handle block that looks like this:

def handle(fields:)
  warn fields.inspect
end
  1. Try using the action and select something from the polymorphic belongs to

Expected behavior & Actual behavior

It should show a warning similar to: '{"reviewable_type": "Post", "reviewable_id": 12345}' but instead it will show just '{"reviewable_type": "Post"}' (with no id field).

Models and resource files

In the example above I've assumed the resources in the avo demo app

System configuration

Avo version: 3.10.9

Rails version: 7.0.8.4

Ruby version: 3.3.2

License type:

Are you using Avo monkey patches, overriding views or view components?

Screenshots or screen recordings

Additional context

I've looked at the code and the issue seems to be with Avo::BaseAction#handle_action in this bit of code:

        action_fields = get_field_definitions.map do |field|
          field.hydrate(resource: @resource)

          [field.id, field]
        end.to_h

At this point, the get_field_definitions returns the field for the polymorphic belongs to and I can see that the fields passed to the action has values for both the type and id inputs, but after this block of code there is only a value for the type (and the id is lost).

I have a workaround for now, which is I've added an extra hidden field to the action for the id of the polymorphic belongs to. If I do that then both values are returned from this block.

Impact

Urgency

Paul-Bob commented 2 months ago

Thanks for the detailed description @iainbeeston we'll have a look