From 9d85d0f1876dd10e62831c766bc252c0be2740f6 Mon Sep 17 00:00:00 2001 From: Thiago Youssef <43591948+thiagoyoussef@users.noreply.github.com> Date: Mon, 18 Nov 2024 06:50:38 -0300 Subject: [PATCH] feature: radio field (#3425) * feature: radio field * feature specs * pluggy radio constant --- app/avo/fields/radio_field.rb | 21 +++++++ .../radio_field/edit_component.html.erb | 10 ++++ .../avo/fields/radio_field/edit_component.rb | 4 ++ .../radio_field/index_component.html.erb | 3 + .../avo/fields/radio_field/index_component.rb | 4 ++ .../radio_field/show_component.html.erb | 3 + .../avo/fields/radio_field/show_component.rb | 4 ++ pluggy/lib/pluggy/railtie.rb | 2 +- spec/dummy/app/avo/resources/fish.rb | 1 + .../20241114165947_add_size_to_fishes.rb | 5 ++ spec/dummy/db/schema.rb | 3 +- spec/features/avo/radio_field_spec.rb | 60 +++++++++++++++++++ 12 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 app/avo/fields/radio_field.rb create mode 100644 app/components/avo/fields/radio_field/edit_component.html.erb create mode 100644 app/components/avo/fields/radio_field/edit_component.rb create mode 100644 app/components/avo/fields/radio_field/index_component.html.erb create mode 100644 app/components/avo/fields/radio_field/index_component.rb create mode 100644 app/components/avo/fields/radio_field/show_component.html.erb create mode 100644 app/components/avo/fields/radio_field/show_component.rb create mode 100644 spec/dummy/db/migrate/20241114165947_add_size_to_fishes.rb create mode 100644 spec/features/avo/radio_field_spec.rb diff --git a/app/avo/fields/radio_field.rb b/app/avo/fields/radio_field.rb new file mode 100644 index 0000000000..78895f9a7f --- /dev/null +++ b/app/avo/fields/radio_field.rb @@ -0,0 +1,21 @@ +module Avo + module Fields + class RadioField < BaseField + def initialize(id, **args, &block) + super(id, **args, &block) + + @options = args[:options] || {} + end + + def options + Avo::ExecutionContext.new( + target: @options, + record: record, + resource: resource, + view: view, + field: self + ).handle + end + end + end +end diff --git a/app/components/avo/fields/radio_field/edit_component.html.erb b/app/components/avo/fields/radio_field/edit_component.html.erb new file mode 100644 index 0000000000..8e304a1dcf --- /dev/null +++ b/app/components/avo/fields/radio_field/edit_component.html.erb @@ -0,0 +1,10 @@ +<%= field_wrapper **field_wrapper_args do %> +
+ <% @field.options.each do |key, value| %> +
+ <%= form.radio_button @field.id, key %> + <%= form.label @field.id, value: value %> +
+ <% end %> +
+<% end %> diff --git a/app/components/avo/fields/radio_field/edit_component.rb b/app/components/avo/fields/radio_field/edit_component.rb new file mode 100644 index 0000000000..114a70156e --- /dev/null +++ b/app/components/avo/fields/radio_field/edit_component.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class Avo::Fields::RadioField::EditComponent < Avo::Fields::EditComponent +end diff --git a/app/components/avo/fields/radio_field/index_component.html.erb b/app/components/avo/fields/radio_field/index_component.html.erb new file mode 100644 index 0000000000..8d4e9d5fda --- /dev/null +++ b/app/components/avo/fields/radio_field/index_component.html.erb @@ -0,0 +1,3 @@ +<%= index_field_wrapper **field_wrapper_args do %> + <%== @field.value %> +<% end %> diff --git a/app/components/avo/fields/radio_field/index_component.rb b/app/components/avo/fields/radio_field/index_component.rb new file mode 100644 index 0000000000..6e2b2b46c2 --- /dev/null +++ b/app/components/avo/fields/radio_field/index_component.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class Avo::Fields::RadioField::IndexComponent < Avo::Fields::IndexComponent +end diff --git a/app/components/avo/fields/radio_field/show_component.html.erb b/app/components/avo/fields/radio_field/show_component.html.erb new file mode 100644 index 0000000000..de7fe73231 --- /dev/null +++ b/app/components/avo/fields/radio_field/show_component.html.erb @@ -0,0 +1,3 @@ +<%= field_wrapper **field_wrapper_args do %> + <%== @field.value %> +<% end %> diff --git a/app/components/avo/fields/radio_field/show_component.rb b/app/components/avo/fields/radio_field/show_component.rb new file mode 100644 index 0000000000..65cac0d7a1 --- /dev/null +++ b/app/components/avo/fields/radio_field/show_component.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class Avo::Fields::RadioField::ShowComponent < Avo::Fields::ShowComponent +end diff --git a/pluggy/lib/pluggy/railtie.rb b/pluggy/lib/pluggy/railtie.rb index d9713e3150..6238105866 100644 --- a/pluggy/lib/pluggy/railtie.rb +++ b/pluggy/lib/pluggy/railtie.rb @@ -4,7 +4,7 @@ class Railtie < Rails::Railtie ActiveSupport.on_load(:avo_boot) do Avo.plugin_manager.register :pluggy - Avo.plugin_manager.register_field :radio, Pluggy::Fields::RadioField + Avo.plugin_manager.register_field :pluggy_radio, Pluggy::Fields::RadioField end end end diff --git a/spec/dummy/app/avo/resources/fish.rb b/spec/dummy/app/avo/resources/fish.rb index c8a619ac1d..d1d91463b0 100644 --- a/spec/dummy/app/avo/resources/fish.rb +++ b/spec/dummy/app/avo/resources/fish.rb @@ -23,6 +23,7 @@ def fields field :id, as: :id field :id, as: :number, only_on: :forms, readonly: -> { !view.new? } field :name, as: :text, required: -> { view.new? }, help: "help text" + field :size, as: :radio, options: {small: "Small", medium: "Medium", large: "Large"} field :secondary_field_for_name, as: :text, for_attribute: :name, diff --git a/spec/dummy/db/migrate/20241114165947_add_size_to_fishes.rb b/spec/dummy/db/migrate/20241114165947_add_size_to_fishes.rb new file mode 100644 index 0000000000..3dc777b6b3 --- /dev/null +++ b/spec/dummy/db/migrate/20241114165947_add_size_to_fishes.rb @@ -0,0 +1,5 @@ +class AddSizeToFishes < ActiveRecord::Migration[6.1] + def change + add_column :fish, :size, :string + end +end diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 3e1141a484..4dfeba42ca 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2024_07_24_090242) do +ActiveRecord::Schema[8.0].define(version: 2024_11_14_165947) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -123,6 +123,7 @@ t.datetime "updated_at", null: false t.string "type" t.bigint "user_id" + t.string "size" t.index ["user_id"], name: "index_fish_on_user_id" end diff --git a/spec/features/avo/radio_field_spec.rb b/spec/features/avo/radio_field_spec.rb new file mode 100644 index 0000000000..7c018857f8 --- /dev/null +++ b/spec/features/avo/radio_field_spec.rb @@ -0,0 +1,60 @@ +require "rails_helper" + +RSpec.describe "RadioField", type: :feature do + describe "when size is present" do + let!(:fish) { create :fish, size: "small" } + + context "index" do + it "displays the fish name" do + visit "/admin/resources/fish" + + expect(page).to have_text "Size" + expect(page).to have_text fish.size + end + end + + context "show" do + it "displays the fish size" do + visit "/admin/resources/fish/#{fish.id}" + + expect(page).to have_text fish.size + end + end + + context "edit" do + it "changes the fish size" do + visit "/admin/resources/fish/#{fish.id}/edit" + + 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" + + find("#fish_size_large").click + + expect(page).to_not have_checked_field "fish_size_small" + expect(page).to_not have_checked_field "fish_size_medium" + expect(page).to have_checked_field "fish_size_large" + + save + + fish.reload + + expect(fish.size).to eq "large" + end + end + end + + describe "when size is nil" do + let!(:fish) { create :fish, size: nil } + + context "edit" do + it "does not check radio buttons" do + visit "/admin/resources/fish/#{fish.id}/edit" + + expect(page).to_not 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" + end + end + end +end