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

Merged
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e599393
Implement multiple select feature for the sizes column of the product…
zhephyn Jan 3, 2025
77d8daf
Merge branch 'main' into feature/multiple_select_field_for_products
zhephyn Jan 6, 2025
fdaf270
Update select_field.rb
zhephyn Jan 8, 2025
c833b6b
Update select_field_spec.rb
zhephyn Jan 8, 2025
4931ab8
Update select_field_spec.rb
zhephyn Jan 8, 2025
e91098e
Merge branch 'avo-hq:main' into feature/multiple_select_field_for_pro…
zhephyn Jan 13, 2025
4c9410b
Update to multiple select PR
zhephyn Jan 13, 2025
76185b2
Update select field spec file
zhephyn Jan 13, 2025
24c59f3
Move sanitization of multiple select field to be handled by the contr…
zhephyn Jan 14, 2025
bae862e
Fix Lint issues raised by Rubocop
zhephyn Jan 14, 2025
67a12b0
Fix migration version error thrown with rails 8 plus lint issues
zhephyn Jan 20, 2025
3446019
Merge branch 'main' into feature/multiple_select_field_for_products
zhephyn Jan 21, 2025
f688e59
fix migration version
Paul-Bob Jan 21, 2025
f8a4a3e
Update spec/dummy/db/schema.rb
Paul-Bob Jan 22, 2025
557e303
Merge branch 'main' into feature/multiple_select_field_for_products
Paul-Bob Jan 22, 2025
9f26661
Merge branch 'main' into feature/multiple_select_field_for_products
Paul-Bob Jan 30, 2025
5eded33
Merge branch 'main' into feature/multiple_select_field_for_products
Paul-Bob Feb 5, 2025
7f72e76
fix select show component when `multiple: true`
Paul-Bob Feb 5, 2025
4e0ecb1
show array on display_value
Paul-Bob Feb 5, 2025
f277d2b
Merge branch 'main' into feature/multiple_select_field_for_products
Paul-Bob Feb 5, 2025
8295709
fix multiple syntax
Paul-Bob Feb 5, 2025
452dd68
Merge branch 'feature/multiple_select_field_for_products' of github.c…
Paul-Bob Feb 5, 2025
439d056
Merge branch 'main' into feature/multiple_select_field_for_products
Paul-Bob Feb 6, 2025
c7dd0f0
show component tests
Paul-Bob Feb 6, 2025
28d62a1
Merge branch 'main' into feature/multiple_select_field_for_products
Paul-Bob Feb 6, 2025
4609f49
lint
Paul-Bob Feb 6, 2025
e56ecb6
Merge branch 'feature/multiple_select_field_for_products' of github.c…
Paul-Bob Feb 6, 2025
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
Contributor 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
Contributor 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