Merge pull request #12602 from chahmedejaz/bugfix/12570-variant-unit-field-out-of-sync

12570 - Fix Variant Unit field is Out of Sync with the Pop-out
This commit is contained in:
Konrad
2024-07-18 13:32:27 +02:00
committed by GitHub
5 changed files with 42 additions and 7 deletions

View File

@@ -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 || 1)
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

View File

@@ -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: }

View File

@@ -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

View File

@@ -39,12 +39,24 @@ RSpec.describe 'As an enterprise user, I can manage my products', feature: :admi
before { visit_products_page_as_admin }
it "hovering over the New variant button displays the text" do
page.find('button[aria-label="New variant"]', text: "New variant", visible: false)
new_variant_button
find("button.secondary.condensed.naked.icon-plus").hover
page.find('button[aria-label="New variant"]', text: "New variant", visible: true)
new_variant_button(visible: true)
expect(page).to have_content "New variant"
end
it "has the 1 unit value for the new variant display_as by default" do
new_variant_button.click
within new_variant_row do
unit_button = find('button[aria-label="Unit"]')
expect(unit_button.text.strip).to eq('1kg')
unit_button.click
expect(page).to have_field "Display unit as", placeholder: "1kg"
end
end
shared_examples "creating a new variant (bulk)" do |stock|
it "handles the #{stock} behaviour" do
# the product and the default variant is displayed
@@ -120,4 +132,12 @@ RSpec.describe 'As an enterprise user, I can manage my products', feature: :admi
login_as_admin
visit spree.admin_products_path
end
def new_variant_button(visible: false)
page.find('button[aria-label="New variant"]', text: "New variant", visible:)
end
def new_variant_row
'tr[data-new-record="true"]'
end
end

View File

@@ -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