diff --git a/lib/avo/fields/select_field.rb b/lib/avo/fields/select_field.rb index 2aaf6ab7b5..3b8834ecfd 100644 --- a/lib/avo/fields/select_field.rb +++ b/lib/avo/fields/select_field.rb @@ -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. @@ -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 diff --git a/spec/features/avo/select_field_spec.rb b/spec/features/avo/select_field_spec.rb index fd798b5f5a..40de395dcc 100644 --- a/spec/features/avo/select_field_spec.rb +++ b/spec/features/avo/select_field_spec.rb @@ -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