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

Require variant category and supplier when creating new product variants [OFN-12666] #12690

Merged
merged 3 commits into from
Jul 31, 2024
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
8 changes: 6 additions & 2 deletions app/assets/javascripts/admin/bulk_product_update.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ angular.module("ofn.admin").controller "AdminProductEditCtrl", ($scope, $timeout
DisplayProperties.setShowVariants 0, showVariants

$scope.addVariant = (product) ->
# Set new variant category to same as last product variant category to keep compactibility with deleted variant callback to set new variant category
newVariantId = $scope.nextVariantId();
newVariantCategoryId = product.variants[product.variants.length - 1]?.category_id
Comment on lines +129 to +131
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why we are doing this, It would make more sense to set the category to blank same as the new screen. I am not sure what you are referring to when you say "to keep compactibility with deleted variant callback to set new variant category"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what you are referring to when you say "to keep compactibility with deleted variant callback to set new variant category"

I think he means to maintain the existing behaviour on the old screen. So there would be no apparent change when using the old screen. But as you say,

It would make more sense to set the category to blank same as the new screen

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the old screen the supplier is set to blank, I think it would be better to have a consistent behaviour.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😮 Oh, so this change makes it worse than before? In that case we need to do better.
Wandji would it be hard to make the old screen behave like the new one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, @dacook and @rioug for identifying this one.
I noticed that the columns preference default for the category column in old screens have visible: false
Not so sure why we have this by default but I will gladly appreciate your input on this again.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that's a good point Wandji. If the Category column is hidden by default, it's not intuitive for the user to choose it.
I just tried it out and can see what you mean.

If your PR ensures the old screen maintains the existing behaviour, I think it's ok to proceed as it is. It adds a bit of technical debt, but that's ok because we're planning to remove the old screen later, once the transition to the new screen is complete.

Is that ok with you Gaetan?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that ok with you Gaetan?

Yeah that's fine, if we get complaint we can always tell them to use the new screen 😉

product.variants.push
id: $scope.nextVariantId()
id: newVariantId
unit_value: null
unit_description: null
on_demand: false
Expand All @@ -136,8 +139,9 @@ angular.module("ofn.admin").controller "AdminProductEditCtrl", ($scope, $timeout
on_hand: null
price: null
tax_category_id: null
category_id: null
category_id: newVariantCategoryId
DisplayProperties.setShowVariants product.id, true
DirtyProducts.addVariantProperty(product.id, newVariantId, 'category_id', newVariantCategoryId)


$scope.nextVariantId = ->
Expand Down
5 changes: 0 additions & 5 deletions app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class Variant < ApplicationRecord
before_validation :ensure_unit_value
before_validation :update_weight_from_unit_value, if: ->(v) { v.product.present? }
before_validation :convert_variant_weight_to_decimal
before_validation :assign_related_taxon, if: ->(v) { v.primary_taxon.blank? }

before_save :assign_units, if: ->(variant) {
variant.new_record? || variant.changed_attributes.keys.intersection(NAME_FIELDS).any?
Expand Down Expand Up @@ -217,10 +216,6 @@ def total_on_hand

private

def assign_related_taxon
self.primary_taxon ||= product.variants.last&.primary_taxon
end

def check_currency
return unless currency.nil?

Expand Down
10 changes: 7 additions & 3 deletions app/views/admin/products_v3/_variant_row.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,24 @@
= f.label :on_demand do
= f.check_box :on_demand, 'data-action': 'change->toggle-control#disableIfPresent change->popout#closeIfChecked'
= t(:on_demand)
%td.col-producer.naked_inputs
%td.col-producer.field.naked_inputs
= render(SearchableDropdownComponent.new(form: f,
name: :supplier_id,
aria_label: t('.producer_field_name'),
options: producer_options,
selected_option: variant.supplier_id,
placeholder_value: t('admin.products_v3.filters.search_for_producers')))
include_blank: t('admin.products_v3.filters.select_producer'),
placeholder_value: t('admin.products_v3.filters.select_producer')))
= error_message_on variant, :supplier
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks for adding error_message_on. It probably should have been added in the beginning.

%td.col-category.field.naked_inputs
= render(SearchableDropdownComponent.new(form: f,
name: :primary_taxon_id,
options: category_options,
selected_option: variant.primary_taxon_id,
aria_label: t('.category_field_name'),
placeholder_value: t('admin.products_v3.filters.search_for_categories')))
include_blank: t('admin.products_v3.filters.select_category'),
placeholder_value: t('admin.products_v3.filters.select_category')))
= error_message_on variant, :primary_taxon
%td.col-tax_category.field.naked_inputs
= render(SearchableDropdownComponent.new(form: f,
name: :tax_category_id,
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,10 @@ en:
filters:
search_products: Search for products
search_for_producers: Search for producers
select_producer: Select producer
all_producers: All producers
search_for_categories: Search for categories
select_category: Select category
all_categories: All categories
producers:
label: Producers
Expand Down
12 changes: 9 additions & 3 deletions spec/javascripts/unit/admin/bulk_product_update_spec.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -797,14 +797,20 @@ describe "AdminProductEditCtrl", ->
spyOn DisplayProperties, 'setShowVariants'

it "adds first and subsequent variants", ->
product = {id: 123, variants: []}
product = {id: 123, variants: [
{
id: 1, price: 10.0, unit_value: 1, tax_category_id: null, unit_description: '1kg',
on_demand: false, on_hand: null, display_as: null, display_name: null, category_id: 2
}
]}
$scope.addVariant(product)
$scope.addVariant(product)
expect(product).toEqual
id: 123
variants: [
{id: -1, price: null, unit_value: null, tax_category_id: null, unit_description: null, on_demand: false, on_hand: null, display_as: null, display_name: null, category_id: null}
{id: -2, price: null, unit_value: null, tax_category_id: null, unit_description: null, on_demand: false, on_hand: null, display_as: null, display_name: null, category_id: null}
{id: 1, price: 10.0, unit_value: 1, tax_category_id: null, unit_description: '1kg', on_demand: false, on_hand: null, display_as: null, display_name: null, category_id: 2}
{id: -1, price: null, unit_value: null, tax_category_id: null, unit_description: null, on_demand: false, on_hand: null, display_as: null, display_name: null, category_id: 2}
{id: -2, price: null, unit_value: null, tax_category_id: null, unit_description: null, on_demand: false, on_hand: null, display_as: null, display_name: null, category_id: 2}
]

it "shows the variant(s)", ->
Expand Down
5 changes: 4 additions & 1 deletion spec/services/sets/product_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,10 @@
[
{ id: product.variants.first.id.to_s }, # default variant unchanged
# omit ID for new variant
{ sku: "new sku", price: "5.00", unit_value: "5", supplier_id: supplier.id },
{
sku: "new sku", price: "5.00", unit_value: "5",
supplier_id: supplier.id, primary_taxon_id: create(:taxon).id
},
]
}
let(:supplier) { create(:supplier_enterprise) }
Expand Down
6 changes: 4 additions & 2 deletions spec/system/admin/products_v3/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
login_as user
end

let(:producer_search_selector) { 'input[placeholder="Search for producers"]' }
let(:categories_search_selector) { 'input[placeholder="Search for categories"]' }
let(:producer_search_selector) { 'input[placeholder="Select producer"]' }
let(:categories_search_selector) { 'input[placeholder="Select category"]' }
let(:tax_categories_search_selector) { 'input[placeholder="Search for tax categories"]' }

describe "with no products" do
Expand Down Expand Up @@ -520,6 +520,7 @@ def expect_other_columns_visible
expect(page).to have_select(
'_products_0_variants_attributes_0_supplier_id',
options: [
'Select producer',
supplier_managed1.name, supplier_managed2.name, supplier_permitted.name
], selected: supplier_managed1.name
)
Expand All @@ -529,6 +530,7 @@ def expect_other_columns_visible
expect(page).to have_select(
'_products_1_variants_attributes_0_supplier_id',
options: [
'Select producer',
supplier_managed1.name, supplier_managed2.name, supplier_permitted.name
], selected: supplier_permitted.name
)
Expand Down
9 changes: 7 additions & 2 deletions spec/system/admin/products_v3/create_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
include AuthenticationHelper
include WebHelper

let!(:supplier) { create(:supplier_enterprise) }
let!(:taxon) { create(:taxon) }

describe "creating a new product" do
let!(:stock_location) { create(:stock_location, backorderable_default: false) }
let!(:supplier) { create(:supplier_enterprise) }
let!(:distributor) { create(:distributor_enterprise) }
let!(:shipping_category) { create(:shipping_category) }
let!(:taxon) { create(:taxon) }

before { visit_products_page_as_admin }

Expand Down Expand Up @@ -81,6 +82,10 @@
find('input[id$="_display_as"]').fill_in with: "2 grams"
find('button[aria-label="On Hand"]').click
find('input[id$="_price"]').fill_in with: "11.1"

select supplier.name, from: 'Producer'
select taxon.name, from: 'Category'
Comment on lines +86 to +87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually test the newly added requirements, that the producer and category dropdowns were blank at the beginning.
That would have been nice, but I think this is still ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes here were to fix failing specs
I agree it will be nice to check when the variant category and producer values are blank


if stock == "on_hand"
find('input[id$="_on_hand"]').fill_in with: "66"
elsif stock == "on_demand"
Expand Down
12 changes: 11 additions & 1 deletion spec/system/admin/products_v3/update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
let(:tax_categories_search_selector) { 'input[placeholder="Search for tax categories"]' }

describe "updating" do
let!(:taxon) { create(:taxon) }
let!(:variant_a1) {
product_a.variants.first.tap{ |v|
v.update! display_name: "Medium box", sku: "APL-01", price: 5.25, on_hand: 5,
Expand Down Expand Up @@ -349,6 +350,9 @@

click_on "On Hand" # activate popout
fill_in "On Hand", with: "3"

select producer.name, from: 'Producer'
select taxon.name, from: 'Category'
end

expect {
Expand Down Expand Up @@ -409,7 +413,8 @@
end

click_on "New variant"
second_new_variant_row = find_field("Name", placeholder: "Apples", with: "").ancestor("tr")
second_new_variant_row = find_field("Name", placeholder: "Apples",
with: "").ancestor("tr")
within second_new_variant_row do
fill_in "Name", with: "Huge box"
end
Expand Down Expand Up @@ -516,6 +521,8 @@
expect(page).to have_field "Name", with: "N" * 256
expect(page).to have_field "SKU", with: "n" * 256
expect(page).to have_content "is too long"
expect(page.find('.col-producer')).to have_content('must exist')
expect(page.find('.col-category')).to have_content('must exist')
expect(page.find_button("Unit")).to have_text "" # have_button selector don't work here
expect(page).to have_field "Price", with: "10.25" # other updated value is retained
end
Expand All @@ -541,6 +548,9 @@

click_on "Unit" # activate popout
fill_in "Unit value", with: "200"

select producer.name, from: 'Producer'
select taxon.name, from: 'Category'
end

expect {
Expand Down
Loading