diff --git a/app/helpers/admin/products_helper.rb b/app/helpers/admin/products_helper.rb index e29b378342..20318ecc14 100644 --- a/app/helpers/admin/products_helper.rb +++ b/app/helpers/admin/products_helper.rb @@ -9,5 +9,23 @@ module Admin new_admin_product_image_path(product.id) end end + + def prepare_new_variant(product) + product.variants.build do |variant| + variant.unit_value = 1.0 * product.variant_unit_scale + variant.unit_presentation = VariantUnits::OptionValueNamer.new(variant).name + end + end + + def unit_value_with_description(variant) + scaled_unit_value = variant.unit_value / (variant.product.variant_unit_scale || 1) + precised_unit_value = number_with_precision( + scaled_unit_value, + precision: nil, + strip_insignificant_zeros: true + ) + + [precised_unit_value, variant.unit_description].compact_blank.join(" ") + end end end diff --git a/app/views/admin/products_v3/_product_variant_row.html.haml b/app/views/admin/products_v3/_product_variant_row.html.haml index 5c2765ab2d..b41e7c56b5 100644 --- a/app/views/admin/products_v3/_product_variant_row.html.haml +++ b/app/views/admin/products_v3/_product_variant_row.html.haml @@ -11,7 +11,7 @@ %tr.condensed{ id: dom_id(variant), 'data-controller': "variant", 'class': "nested-form-wrapper", 'data-new-record': variant.new_record? ? "true" : false } = render partial: 'variant_row', locals: { variant:, f: variant_form, category_options:, tax_category_options:, producer_options: } - = form.fields_for("products][#{product_index}][variants_attributes][NEW_RECORD", product.variants.build) do |new_variant_form| + = form.fields_for("products][#{product_index}][variants_attributes][NEW_RECORD", prepare_new_variant(product)) do |new_variant_form| %template{ 'data-nested-form-target': "template" } %tr.condensed{ 'data-controller': "variant", 'class': "nested-form-wrapper", 'data-new-record': "true" } = render partial: 'variant_row', locals: { variant: new_variant_form.object, f: new_variant_form, category_options:, tax_category_options:, producer_options: } diff --git a/app/views/admin/products_v3/_variant_row.html.haml b/app/views/admin/products_v3/_variant_row.html.haml index b137c25de0..a1244e1d47 100644 --- a/app/views/admin/products_v3/_variant_row.html.haml +++ b/app/views/admin/products_v3/_variant_row.html.haml @@ -17,10 +17,8 @@ -# Show a composite field for unit_value and unit_description = f.hidden_field :unit_value = f.hidden_field :unit_description - -# todo: create a method for value_with_description = f.text_field :unit_value_with_description, - value: [number_with_precision((variant.unit_value || 1) / (variant.product.variant_unit_scale || 1), precision: nil, strip_insignificant_zeros: true), variant.unit_description].compact_blank.join(" "), - 'aria-label': t('admin.products_page.columns.unit_value'), required: true + value: unit_value_with_description(variant), 'aria-label': t('admin.products_page.columns.unit_value'), required: true .field = f.label :display_as, t('admin.products_page.columns.display_as') = f.text_field :display_as, placeholder: VariantUnits::OptionValueNamer.new(variant).name diff --git a/spec/system/admin/products_v3/update_spec.rb b/spec/system/admin/products_v3/update_spec.rb index 9495838ff3..bce91c3429 100644 --- a/spec/system/admin/products_v3/update_spec.rb +++ b/spec/system/admin/products_v3/update_spec.rb @@ -517,7 +517,6 @@ RSpec.describe 'As an enterprise user, I can update my products', feature: :admi expect(page).to have_field "SKU", with: "n" * 256 expect(page).to have_content "is too long" expect(page.find_button("Unit")).to have_text "" # have_button selector don't work here - expect(page).to have_content "can't be blank" expect(page).to have_field "Price", with: "10.25" # other updated value is retained end