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

12570 - Fix Variant Unit field is Out of Sync with the Pop-out #12602

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 18 additions & 0 deletions app/helpers/admin/products_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,23 @@ def product_image_form_path(product)
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
2 changes: 1 addition & 1 deletion app/views/admin/products_v3/_product_variant_row.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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: }
Expand Down
4 changes: 1 addition & 3 deletions app/views/admin/products_v3/_variant_row.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 22 additions & 2 deletions spec/system/admin/products_v3/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,24 @@
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
Expand Down Expand Up @@ -120,4 +132,12 @@ def visit_products_page_as_admin
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
1 change: 0 additions & 1 deletion spec/system/admin/products_v3/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,6 @@
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

Expand Down
Loading