From 9a7a75e8e853a70440afea46c42a28a0241673f4 Mon Sep 17 00:00:00 2001 From: Thiago Youssef <43591948+thiagoyoussef@users.noreply.github.com> Date: Mon, 18 Nov 2024 16:10:20 -0300 Subject: [PATCH] fix: radio field (#3437) * fix: radio field * code climate * fix double radio issue by setting the field_name_attribute on the pluggy field * Plugin manager register `field_name_attribute` --------- Co-authored-by: Paul Bob --- {app => lib}/avo/fields/radio_field.rb | 0 lib/avo/plugin_manager.rb | 6 ++++++ spec/features/avo/radio_field_spec.rb | 5 +++++ 3 files changed, 11 insertions(+) rename {app => lib}/avo/fields/radio_field.rb (100%) diff --git a/app/avo/fields/radio_field.rb b/lib/avo/fields/radio_field.rb similarity index 100% rename from app/avo/fields/radio_field.rb rename to lib/avo/fields/radio_field.rb diff --git a/lib/avo/plugin_manager.rb b/lib/avo/plugin_manager.rb index e051e31af6..c35a43527b 100644 --- a/lib/avo/plugin_manager.rb +++ b/lib/avo/plugin_manager.rb @@ -13,6 +13,12 @@ def register(name, priority: 10) end def register_field(method_name, klass) + # Avo.boot method is executed multiple times. + # During the first run, it correctly loads descendants of Avo::Fields::Base. + # Plugins are then loaded, introducing additional descendants to Avo::Fields::Base. + # On subsequent runs, Avo::Fields::Base descendants now include these plugin fields. + # This field_name_attribute assign forces the field name to retain the registered name instead of being computed dynamically from the field class. + klass.field_name_attribute = method_name Avo.field_manager.load_field method_name, klass end diff --git a/spec/features/avo/radio_field_spec.rb b/spec/features/avo/radio_field_spec.rb index 7c018857f8..3a211f7cc8 100644 --- a/spec/features/avo/radio_field_spec.rb +++ b/spec/features/avo/radio_field_spec.rb @@ -25,6 +25,11 @@ it "changes the fish size" do visit "/admin/resources/fish/#{fish.id}/edit" + expect(Avo.field_manager.fields).to include( + {name: "radio", class: Avo::Fields::RadioField}, + {name: "pluggy_radio", class: Pluggy::Fields::RadioField} + ) + expect(page).to have_checked_field "fish_size_small" expect(page).to_not have_checked_field "fish_size_medium" expect(page).to_not have_checked_field "fish_size_large"