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: Implement multiple option for select field #3572

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<%= field_wrapper **field_wrapper_args do %>
<%= @form.select @field.id, options_for_select(@field.options_for_select, selected: @field.value), {
include_blank: @field.include_blank
include_blank: @field.include_blank,
multiple: @field.multiple
},
aria: {
placeholder: @field.placeholder
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20250103192051_add_sizes_to_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSizesToProducts < ActiveRecord::Migration[6.1]
def change
add_column :products, :sizes, :string, array: true, default: []
end
end
15 changes: 14 additions & 1 deletion lib/avo/fields/select_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Fields
class SelectField < BaseField
include Avo::Fields::FieldExtensions::HasIncludeBlank

attr_reader :display_value
attr_reader :display_value, :multiple

def initialize(id, **args, &block)
args[:placeholder] ||= I18n.t("avo.choose_an_option")
Expand All @@ -19,6 +19,7 @@ def initialize(id, **args, &block)
end

@enum = args[:enum]
@multiple = args[:multiple]
@display_value = args[:display_value] || false
end

Expand Down Expand Up @@ -55,6 +56,18 @@ def label
display_value ? value : options.invert[value]
end

def to_permitted_param
@multiple ? {"#{id}": []} : id
end

def fill_field(record, key, value, params)
if @multiple
value = value.reject(&:blank?)
end

super
end

private

def options
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/avo/resources/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ def fields
field :description, as: :tiptap, placeholder: "Enter text", always_show: false
field :image, as: :file, is_image: true
field :category, as: :select, enum: ::Product.categories
field :sizes, as: :select, multiple: true, options: {Large: :large, Medium: :medium, Small: :small}
end
end
7 changes: 4 additions & 3 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[8.0].define(version: 2024_11_14_165947) do
ActiveRecord::Schema[8.0].define(version: 2025_01_03_192051) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
enable_extension "pg_catalog.plpgsql"

create_table "action_text_rich_texts", force: :cascade do |t|
t.string "name", null: false
Expand Down Expand Up @@ -173,12 +173,13 @@
t.datetime "updated_at", null: false
t.integer "price_cents", default: 0, null: false
t.string "price_currency", default: "'USD'::character varying", null: false
t.string "sizes", default: [], array: true
end

create_table "projects", force: :cascade do |t|
t.string "name"
t.string "status"
t.string "stage"
t.string "stage", default: [], array: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zhephyn let's start by fixing the schema, the AddSizesToProducts migration is adding the sizes array column to the products table however the schema is also changing the stage column to array on the projects table.

Suggested change
t.string "stage", default: [], array: true
t.string "stage"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm directly committing this change to speed up the process

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay no problem. Im not directly in contact with my computer at the moment

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Paul-Bob . Though I didn't quite understand what you referenced, you specified a migration named AddSizesToProducts, yet according to the suggested change, it's like you're changing the stage column to a non array column, and the stage column is part of the projects table not the products table. Could you kindly clarify if this is the case?

t.string "budget"
t.string "country"
t.integer "users_required"
Expand Down
30 changes: 30 additions & 0 deletions spec/features/avo/select_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,34 @@ def test_array
end
end
end
describe "when options are multiple select" do
context "new" do
it "allow selection of multiple values" do
visit avo.new_resources_product_path
expect(page).to have_select "product_sizes", multiple: true, options: ["Large", "Medium", "Small"]
select "Large", from: "product_sizes"
select "Medium", from: "product_sizes"
save
expect(Product.last.sizes).to match_array(["large", "medium"])
end
end
context "edit" do
let(:product) { create :product, sizes: [:large] }
it "allow changing of selected values" do
visit avo.edit_resources_product_path(product)
expect(page).to have_select "product_sizes", selected: ["Large"], multiple: true, options: ["Large", "Medium", "Small"]
select "Medium", from: "product_sizes"
select "Small", from: "product_sizes"
save
expect(Product.last.sizes).to match_array(["medium", "small", "large"])
end
it "allow deselecting of previously selected values" do
visit avo.edit_resources_product_path(product)
expect(page).to have_select "product_sizes", selected: ["Large"], multiple: true, options: ["Large", "Medium", "Small"]
page.unselect "Large", from: "Sizes"
save
expect(Product.last.sizes).to match_array([])
end
end
end
end
Loading