Skip to content

Commit

Permalink
fix select show component when multiple: true
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Feb 5, 2025
1 parent 5eded33 commit 7f72e76
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
22 changes: 20 additions & 2 deletions lib/avo/fields/select_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ def options_for_select

def label
# If options are array don't need any pre-process
return value if options.is_a?(Array)
if options.is_a?(Array)
if @multiple
return value.join(", ")
else
return value
end
end

# If options are enum and display_value is true we return the Value of that key-value pair, else return key of that key-value pair
# WARNING: value here is the DB stored value and not the value of a key-value pair.
Expand All @@ -53,7 +59,19 @@ def label

# When code arrive here it means options are Hash
# If display_value is true we only need to return the value stored in DB
display_value ? value : options.invert[value]
if display_value
if @multiple
return value.join(", ")
else
return value
end
end

if @multiple
options.select { |_, v| value.include?(v.to_s) }.keys.join(", ")
else
options.invert[value]
end
end

def to_permitted_param
Expand Down
16 changes: 16 additions & 0 deletions spec/features/avo/select_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,32 +229,48 @@ 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
Expand Down

0 comments on commit 7f72e76

Please sign in to comment.