Skip to content

Commit

Permalink
fix: radio field (#3437)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
thiagoyoussef and Paul-Bob authored Nov 18, 2024
1 parent 9d85d0f commit 9a7a75e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
File renamed without changes.
6 changes: 6 additions & 0 deletions lib/avo/plugin_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions spec/features/avo/radio_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 9a7a75e

Please sign in to comment.