Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: radio field #3425

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/avo/fields/radio_field.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions app/components/avo/fields/radio_field/edit_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<%= field_wrapper **field_wrapper_args do %>
<div class="flex flex-col gap-2">
<% @field.options.each do |key, value| %>
<div>
<%= form.radio_button @field.id, key %>
<%= form.label @field.id, value: value %>
</div>
<% end %>
</div>
<% end %>
4 changes: 4 additions & 0 deletions app/components/avo/fields/radio_field/edit_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class Avo::Fields::RadioField::EditComponent < Avo::Fields::EditComponent
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= index_field_wrapper **field_wrapper_args do %>
<%== @field.value %>
<% end %>
4 changes: 4 additions & 0 deletions app/components/avo/fields/radio_field/index_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class Avo::Fields::RadioField::IndexComponent < Avo::Fields::IndexComponent
end
3 changes: 3 additions & 0 deletions app/components/avo/fields/radio_field/show_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= field_wrapper **field_wrapper_args do %>
<%== @field.value %>
<% end %>
4 changes: 4 additions & 0 deletions app/components/avo/fields/radio_field/show_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class Avo::Fields::RadioField::ShowComponent < Avo::Fields::ShowComponent
end
2 changes: 1 addition & 1 deletion pluggy/lib/pluggy/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/avo/resources/fish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions spec/dummy/db/migrate/20241114165947_add_size_to_fishes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSizeToFishes < ActiveRecord::Migration[6.1]
def change
add_column :fish, :size, :string
end
end
3 changes: 2 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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

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