Skip to content

Commit

Permalink
Require varian t category when creating new product variant [OFN-12666]
Browse files Browse the repository at this point in the history
  • Loading branch information
wandji20 committed Jul 24, 2024
1 parent 99acf75 commit 1c865af
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
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
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
%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
7 changes: 7 additions & 0 deletions 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 @@ -541,6 +545,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

0 comments on commit 1c865af

Please sign in to comment.