From 642cd85cb9bc477b1cf748c0c29d788889140c00 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Tue, 31 Oct 2023 18:40:24 +0100 Subject: [PATCH 01/10] update WeightsAndMeasures#scale_for_unit_value to only return units that are selected on 'Available Units' list --- app/services/weights_and_measures.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/services/weights_and_measures.rb b/app/services/weights_and_measures.rb index ada5523e22..d9e2c42cb8 100644 --- a/app/services/weights_and_measures.rb +++ b/app/services/weights_and_measures.rb @@ -46,7 +46,9 @@ class WeightsAndMeasures }.freeze def scales_for_variant_unit - @units[@variant.product.variant_unit] + @scales_for_variant_unit ||= @units[@variant.product.variant_unit].reject { |_scale, unit_info| + available_units.exclude?(unit_info['name']) + } end # Find the largest available and compatible unit where unit_value comes @@ -63,4 +65,8 @@ class WeightsAndMeasures largest_unit end + + def available_units + Spree::Config.preferences[:available_units].split(",") + end end From fb2467865483a3283dff461da86756ed4113c55c Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Tue, 31 Oct 2023 18:57:42 +0100 Subject: [PATCH 02/10] update units list on Spree::Admin::GeneralSettingsHelper#all_units added a test to make sure that all units on WeightsAndMeasures::UNITS are list on all_units --- app/helpers/spree/admin/general_settings_helper.rb | 7 ++++++- spec/services/weights_and_measures_spec.rb | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/helpers/spree/admin/general_settings_helper.rb b/app/helpers/spree/admin/general_settings_helper.rb index a370354c37..a40ef75940 100644 --- a/app/helpers/spree/admin/general_settings_helper.rb +++ b/app/helpers/spree/admin/general_settings_helper.rb @@ -4,7 +4,12 @@ module Spree module Admin module GeneralSettingsHelper def all_units - ["g", "oz", "lb", "kg", "T", "mL", "L", "kL"] + [ + "mg", "g", "kg", "T", + "oz", "lb", + "mL", "cL", "dL", "L", "kL", + "gal" + ] end end end diff --git a/spec/services/weights_and_measures_spec.rb b/spec/services/weights_and_measures_spec.rb index c6f98e6e5a..5be6e012f3 100644 --- a/spec/services/weights_and_measures_spec.rb +++ b/spec/services/weights_and_measures_spec.rb @@ -90,4 +90,12 @@ describe WeightsAndMeasures do end end end + + describe "UNITS" do + include Spree::Admin::GeneralSettingsHelper + it "should include all the available untis" do + units = WeightsAndMeasures::UNITS.values.flat_map(&:values).pluck("name").sort.uniq + expect(units).to eq(all_units.sort.uniq) + end + end end From e10d441153451144707f7e22a45baed63edf574a Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Wed, 1 Nov 2023 09:53:47 +0100 Subject: [PATCH 03/10] load GeneralSettingsHelper#all_units from WeightsAndMeasures::UNITS --- app/helpers/spree/admin/general_settings_helper.rb | 8 +++----- .../spree/admin/general_settings_helper_spec.rb | 12 ++++++++++++ spec/services/weights_and_measures_spec.rb | 8 -------- 3 files changed, 15 insertions(+), 13 deletions(-) create mode 100644 spec/helpers/spree/admin/general_settings_helper_spec.rb diff --git a/app/helpers/spree/admin/general_settings_helper.rb b/app/helpers/spree/admin/general_settings_helper.rb index a40ef75940..481658a3d0 100644 --- a/app/helpers/spree/admin/general_settings_helper.rb +++ b/app/helpers/spree/admin/general_settings_helper.rb @@ -5,11 +5,9 @@ module Spree module GeneralSettingsHelper def all_units [ - "mg", "g", "kg", "T", - "oz", "lb", - "mL", "cL", "dL", "L", "kL", - "gal" - ] + WeightsAndMeasures::UNITS['weight'].values.pluck('name'), + WeightsAndMeasures::UNITS['volume'].values.pluck('name') + ].flatten.uniq end end end diff --git a/spec/helpers/spree/admin/general_settings_helper_spec.rb b/spec/helpers/spree/admin/general_settings_helper_spec.rb new file mode 100644 index 0000000000..b63d7c8cdc --- /dev/null +++ b/spec/helpers/spree/admin/general_settings_helper_spec.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Spree::Admin::GeneralSettingsHelper, type: :helper do + describe "#all_units" do + it "returns all units" do + expect(helper.all_units).to eq(["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", + "kL", "gal"]) + end + end +end diff --git a/spec/services/weights_and_measures_spec.rb b/spec/services/weights_and_measures_spec.rb index 5be6e012f3..c6f98e6e5a 100644 --- a/spec/services/weights_and_measures_spec.rb +++ b/spec/services/weights_and_measures_spec.rb @@ -90,12 +90,4 @@ describe WeightsAndMeasures do end end end - - describe "UNITS" do - include Spree::Admin::GeneralSettingsHelper - it "should include all the available untis" do - units = WeightsAndMeasures::UNITS.values.flat_map(&:values).pluck("name").sort.uniq - expect(units).to eq(all_units.sort.uniq) - end - end end From 2733e45e161d2ab2ca6caf89ddb0e7706ee9b567 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Wed, 1 Nov 2023 10:11:33 +0100 Subject: [PATCH 04/10] update the list of the unit on VariantUnitManager --- .../products/services/variant_unit_manager.js.coffee | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/assets/javascripts/admin/products/services/variant_unit_manager.js.coffee b/app/assets/javascripts/admin/products/services/variant_unit_manager.js.coffee index fa3f1fbab2..8a36972d1f 100644 --- a/app/assets/javascripts/admin/products/services/variant_unit_manager.js.coffee +++ b/app/assets/javascripts/admin/products/services/variant_unit_manager.js.coffee @@ -2,6 +2,9 @@ angular.module("admin.products").factory "VariantUnitManager", (availableUnits) class VariantUnitManager @units: 'weight': + 0.001: + name: 'mg' + system: 'metric' 1.0: name: 'g' system: 'metric' @@ -21,12 +24,21 @@ angular.module("admin.products").factory "VariantUnitManager", (availableUnits) 0.001: name: 'mL' system: 'metric' + 0.01: + name: 'cL' + system: 'metric' + 0.1: + name: 'dL' + system: 'metric' 1.0: name: 'L' system: 'metric' 1000.0: name: 'kL' system: 'metric' + 4.54609: + name: 'gal' + system: 'metric' 'items': 1: name: 'items' From a9d85fd08f262d03245b55bb7f2b487cdf40eaa3 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Wed, 1 Nov 2023 10:48:23 +0100 Subject: [PATCH 05/10] fix existing tests --- app/services/weights_and_measures.rb | 2 +- spec/models/spree/line_item_spec.rb | 7 ++++ spec/services/unit_prices_spec.rb | 4 +++ .../variant_units/option_value_namer_spec.rb | 7 ++++ spec/services/weights_and_measures_spec.rb | 33 ++++++++++++++++++- spec/system/admin/product_import_spec.rb | 8 +++++ 6 files changed, 59 insertions(+), 2 deletions(-) diff --git a/app/services/weights_and_measures.rb b/app/services/weights_and_measures.rb index d9e2c42cb8..fed5a72fc3 100644 --- a/app/services/weights_and_measures.rb +++ b/app/services/weights_and_measures.rb @@ -46,7 +46,7 @@ class WeightsAndMeasures }.freeze def scales_for_variant_unit - @scales_for_variant_unit ||= @units[@variant.product.variant_unit].reject { |_scale, unit_info| + @scales_for_variant_unit ||= @units[@variant.product.variant_unit]&.reject { |_scale, unit_info| available_units.exclude?(unit_info['name']) } end diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index 527b86d551..8721121c86 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -695,6 +695,13 @@ module Spree let(:p2) { create(:product, name: 'Clear United States Honey', variant_unit_scale: 453.6) } let(:v2) { create(:variant, product: p2, unit_value: 453.6) } let(:li2) { create(:line_item, order: o, product: p2, variant: v2) } + let(:available_units) { + ["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",") + } + + before do + Spree::Config.set_preference(:available_units, available_units) + end it "returns options_text" do li = build_stubbed(:line_item) diff --git a/spec/services/unit_prices_spec.rb b/spec/services/unit_prices_spec.rb index eaec852e44..2cddf462c6 100644 --- a/spec/services/unit_prices_spec.rb +++ b/spec/services/unit_prices_spec.rb @@ -6,9 +6,13 @@ describe UnitPrice do subject { UnitPrice.new(variant) } let(:variant) { Spree::Variant.new } let(:product) { instance_double(Spree::Product) } + let(:available_units) { + ["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",") + } before do allow(variant).to receive(:product) { product } + Spree::Config.set_preference(:available_units, available_units) end describe "#unit" do diff --git a/spec/services/variant_units/option_value_namer_spec.rb b/spec/services/variant_units/option_value_namer_spec.rb index cf9178c244..4017943184 100644 --- a/spec/services/variant_units/option_value_namer_spec.rb +++ b/spec/services/variant_units/option_value_namer_spec.rb @@ -4,6 +4,13 @@ require 'spec_helper' module VariantUnits describe OptionValueNamer do + let(:available_units) { + ["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",") + } + + before do + Spree::Config.set_preference(:available_units, available_units) + end describe "generating option value name" do let(:v) { Spree::Variant.new } let(:p) { Spree::Product.new } diff --git a/spec/services/weights_and_measures_spec.rb b/spec/services/weights_and_measures_spec.rb index c6f98e6e5a..5197d2db72 100644 --- a/spec/services/weights_and_measures_spec.rb +++ b/spec/services/weights_and_measures_spec.rb @@ -6,9 +6,13 @@ describe WeightsAndMeasures do subject { WeightsAndMeasures.new(variant) } let(:variant) { Spree::Variant.new } let(:product) { instance_double(Spree::Product) } + let(:available_units) { + ["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",") + } before do allow(variant).to receive(:product) { product } + Spree::Config.set_preference(:available_units, available_units) end describe "#system" do @@ -69,16 +73,43 @@ describe WeightsAndMeasures do allow(variant).to receive(:unit_value) { 1500 } expect(subject.scale_for_unit_value).to eq([1000.0, "kg"]) end + + describe "should not display in kg if this unit is not selected" do + let(:available_units) { ["mg", "g", "T"].join(",") } + + it "should display in g" do + allow(product).to receive(:variant_unit_scale) { 1.0 } + allow(variant).to receive(:unit_value) { 1500 } + expect(subject.scale_for_unit_value).to eq([1.0, "g"]) + end + end end end context "volume" do - it "for a unit value that should display in L" do + it "for a unit value that should display in kL" do allow(product).to receive(:variant_unit) { "volume" } allow(product).to receive(:variant_unit_scale) { 1.0 } allow(variant).to receive(:unit_value) { 1500 } expect(subject.scale_for_unit_value).to eq([1000, "kL"]) end + + it "for a unit value that should display in dL" do + allow(product).to receive(:variant_unit) { "volume" } + allow(product).to receive(:variant_unit_scale) { 1.0 } + allow(variant).to receive(:unit_value) { 0.5 } + expect(subject.scale_for_unit_value).to eq([0.1, "dL"]) + end + + context "should not display in dL/cL if those units are not selected" do + let(:available_units){ ["mL", "L", "kL", "gal"].join(",") } + it "for a unit value that should display in mL" do + allow(product).to receive(:variant_unit) { "volume" } + allow(product).to receive(:variant_unit_scale) { 1.0 } + allow(variant).to receive(:unit_value) { 0.5 } + expect(subject.scale_for_unit_value).to eq([0.001, "mL"]) + end + end end context "items" do diff --git a/spec/system/admin/product_import_spec.rb b/spec/system/admin/product_import_spec.rb index 8671daa8c7..386156be0c 100644 --- a/spec/system/admin/product_import_spec.rb +++ b/spec/system/admin/product_import_spec.rb @@ -53,6 +53,14 @@ describe "Product Import" do let(:shipping_category_id_str) { Spree::ShippingCategory.all.first.id.to_s } + let(:available_units) { + ["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",") + } + + before do + Spree::Config.set_preference(:available_units, available_units) + end + describe "when importing products from uploaded file" do before do allow(Spree::Config).to receive(:available_units).and_return("g,lb,oz,kg,T,mL,L,kL") From 864f63b7b0017f0ee080bd8b24dcf2fc95e6061f Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Wed, 1 Nov 2023 11:38:10 +0100 Subject: [PATCH 06/10] update VariantUnitManger specs --- .../unit/admin/services/variant_unit_manager_spec.js.coffee | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee b/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee index 9a635078d0..4c1db69209 100644 --- a/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee +++ b/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee @@ -23,10 +23,10 @@ describe "VariantUnitManager", -> describe "unitScales", -> it "returns a sorted set of scales for unit type weight", -> - expect(VariantUnitManager.unitScales('weight')).toEqual [1.0, 28.35, 453.6, 1000.0, 1000000.0] + expect(VariantUnitManager.unitScales('weight')).toEqual [0.001, 1.0, 28.35, 453.6, 1000.0, 1000000.0] it "returns a sorted set of scales for unit type volume", -> - expect(VariantUnitManager.unitScales('volume')).toEqual [0.001, 1.0, 1000.0] + expect(VariantUnitManager.unitScales('volume')).toEqual [0.001, 0.01, 0.1, 1.0, 4.54609, 1000.0] describe "compatibleUnitScales", -> it "returns a sorted set of compatible scales based on the scale and unit type provided", -> From 72d854487a096a1db3c9a6c88d5bedacd07afb4a Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Wed, 1 Nov 2023 11:55:34 +0100 Subject: [PATCH 07/10] update VariantUnitManager#compatibleUnitScales to return units that are selected --- .../products/services/variant_unit_manager.js.coffee | 9 +++++++-- .../services/variant_unit_manager_spec.js.coffee | 12 ++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/admin/products/services/variant_unit_manager.js.coffee b/app/assets/javascripts/admin/products/services/variant_unit_manager.js.coffee index 8a36972d1f..6f6aefa336 100644 --- a/app/assets/javascripts/admin/products/services/variant_unit_manager.js.coffee +++ b/app/assets/javascripts/admin/products/services/variant_unit_manager.js.coffee @@ -72,8 +72,13 @@ angular.module("admin.products").factory "VariantUnitManager", (availableUnits) @compatibleUnitScales: (scale, unitType) -> scaleSystem = @units[unitType][scale]['system'] - (parseFloat(scale) for scale, scaleInfo of @units[unitType] when scaleInfo['system'] == scaleSystem).sort (a, b) -> - a - b + if availableUnits + available = availableUnits.split(",") + (parseFloat(scale) for scale, scaleInfo of @units[unitType] when scaleInfo['system'] == scaleSystem and available.includes(scaleInfo['name'])).sort (a, b) -> + a - b + else + (parseFloat(scale) for scale, scaleInfo of @units[unitType] when scaleInfo['system'] == scaleSystem).sort (a, b) -> + a - b @systemOfMeasurement: (scale, unitType) -> if @units[unitType][scale] diff --git a/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee b/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee index 4c1db69209..7a1d726774 100644 --- a/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee +++ b/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee @@ -33,6 +33,18 @@ describe "VariantUnitManager", -> expect(VariantUnitManager.compatibleUnitScales(1, "weight")).toEqual [1.0, 1000.0, 1000000.0] expect(VariantUnitManager.compatibleUnitScales(453.6, "weight")).toEqual [28.35, 453.6] + pending "should load only available unit", -> + beforeEach -> + module "admin.products" + module ($provide)-> + $provide.value "availableUnits", "g,T,mL,L,kL,lb" + null + inject (_VariantUnitManager_) -> + VariantUnitManager1 = _VariantUnitManager_ + it "returns a sorted set of compatible scales based on the scale and unit type provided", -> + expect(VariantUnitManager1.compatibleUnitScales(1, "weight")).toEqual [1.0, 1000000.0] + expect(VariantUnitManager1.compatibleUnitScales(453.6, "weight")).toEqual [453.6] + describe "variantUnitOptions", -> it "returns an array of options", -> expect(VariantUnitManager.variantUnitOptions()).toEqual [ From 91cb3df215d6a959a886650fdef1f9e9c7f4147e Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 3 Nov 2023 12:28:29 +1100 Subject: [PATCH 08/10] Use named config method And mock the preference value, rather than setting it. Before, the set preference could have leaked to other tests. (I noticed that it was already like this in product_import_spec.rb) --- app/services/weights_and_measures.rb | 2 +- spec/models/spree/line_item_spec.rb | 7 ++----- spec/services/unit_prices_spec.rb | 5 +---- .../services/variant_units/option_value_namer_spec.rb | 11 ++++------- spec/services/weights_and_measures_spec.rb | 2 +- spec/system/admin/product_import_spec.rb | 8 -------- 6 files changed, 9 insertions(+), 26 deletions(-) diff --git a/app/services/weights_and_measures.rb b/app/services/weights_and_measures.rb index fed5a72fc3..32e1811457 100644 --- a/app/services/weights_and_measures.rb +++ b/app/services/weights_and_measures.rb @@ -67,6 +67,6 @@ class WeightsAndMeasures end def available_units - Spree::Config.preferences[:available_units].split(",") + Spree::Config.available_units.split(",") end end diff --git a/spec/models/spree/line_item_spec.rb b/spec/models/spree/line_item_spec.rb index 8721121c86..2a1499a288 100644 --- a/spec/models/spree/line_item_spec.rb +++ b/spec/models/spree/line_item_spec.rb @@ -695,12 +695,9 @@ module Spree let(:p2) { create(:product, name: 'Clear United States Honey', variant_unit_scale: 453.6) } let(:v2) { create(:variant, product: p2, unit_value: 453.6) } let(:li2) { create(:line_item, order: o, product: p2, variant: v2) } - let(:available_units) { - ["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",") - } before do - Spree::Config.set_preference(:available_units, available_units) + allow(Spree::Config).to receive(:available_units).and_return("g,lb,oz,kg,T,mL,L,kL") end it "returns options_text" do @@ -709,7 +706,7 @@ module Spree expect(li.unit_to_display).to eq("ponies") end - it "returns options_text based on units" do + it "returns options_text based on configured units" do expect(li1.options_text).to eq("500g") expect(li2.options_text).to eq("1lb") end diff --git a/spec/services/unit_prices_spec.rb b/spec/services/unit_prices_spec.rb index 2cddf462c6..ef9aa86bee 100644 --- a/spec/services/unit_prices_spec.rb +++ b/spec/services/unit_prices_spec.rb @@ -6,13 +6,10 @@ describe UnitPrice do subject { UnitPrice.new(variant) } let(:variant) { Spree::Variant.new } let(:product) { instance_double(Spree::Product) } - let(:available_units) { - ["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",") - } before do allow(variant).to receive(:product) { product } - Spree::Config.set_preference(:available_units, available_units) + allow(Spree::Config).to receive(:available_units).and_return("g,lb,oz,kg,T,mL,L,kL") end describe "#unit" do diff --git a/spec/services/variant_units/option_value_namer_spec.rb b/spec/services/variant_units/option_value_namer_spec.rb index 4017943184..6d71ac3966 100644 --- a/spec/services/variant_units/option_value_namer_spec.rb +++ b/spec/services/variant_units/option_value_namer_spec.rb @@ -4,13 +4,6 @@ require 'spec_helper' module VariantUnits describe OptionValueNamer do - let(:available_units) { - ["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",") - } - - before do - Spree::Config.set_preference(:available_units, available_units) - end describe "generating option value name" do let(:v) { Spree::Variant.new } let(:p) { Spree::Product.new } @@ -69,6 +62,10 @@ module VariantUnits let(:v) { Spree::Variant.new } let(:subject) { OptionValueNamer.new v } + before do + allow(Spree::Config).to receive(:available_units).and_return("g,lb,oz,kg,T,mL,L,kL") + end + it "generates simple values" do p = double(:product, variant_unit: 'weight', variant_unit_scale: 1.0) allow(v).to receive(:product) { p } diff --git a/spec/services/weights_and_measures_spec.rb b/spec/services/weights_and_measures_spec.rb index 5197d2db72..d84c2c5b66 100644 --- a/spec/services/weights_and_measures_spec.rb +++ b/spec/services/weights_and_measures_spec.rb @@ -12,7 +12,7 @@ describe WeightsAndMeasures do before do allow(variant).to receive(:product) { product } - Spree::Config.set_preference(:available_units, available_units) + allow(Spree::Config).to receive(:available_units).and_return(available_units) end describe "#system" do diff --git a/spec/system/admin/product_import_spec.rb b/spec/system/admin/product_import_spec.rb index 386156be0c..8671daa8c7 100644 --- a/spec/system/admin/product_import_spec.rb +++ b/spec/system/admin/product_import_spec.rb @@ -53,14 +53,6 @@ describe "Product Import" do let(:shipping_category_id_str) { Spree::ShippingCategory.all.first.id.to_s } - let(:available_units) { - ["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",") - } - - before do - Spree::Config.set_preference(:available_units, available_units) - end - describe "when importing products from uploaded file" do before do allow(Spree::Config).to receive(:available_units).and_return("g,lb,oz,kg,T,mL,L,kL") From b872d7c3085e17d50d31d79279ffaf355e363a94 Mon Sep 17 00:00:00 2001 From: David Cook Date: Fri, 3 Nov 2023 12:49:29 +1100 Subject: [PATCH 09/10] Re-arrange spec Best viewed with white-space ignored --- .../variant_unit_manager_spec.js.coffee | 103 +++++++++--------- 1 file changed, 53 insertions(+), 50 deletions(-) diff --git a/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee b/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee index 7a1d726774..67753efef2 100644 --- a/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee +++ b/spec/javascripts/unit/admin/services/variant_unit_manager_spec.js.coffee @@ -1,60 +1,63 @@ describe "VariantUnitManager", -> VariantUnitManager = null - beforeEach -> - module "admin.products" - module ($provide)-> - $provide.value "availableUnits", "g,kg,T,mL,L,kL,lb,oz" - null + describe "with default units", -> + beforeEach -> + module "admin.products" + module ($provide)-> + $provide.value "availableUnits", "g,kg,T,mL,L,kL,lb,oz" + null - beforeEach inject (_VariantUnitManager_) -> - VariantUnitManager = _VariantUnitManager_ + beforeEach inject (_VariantUnitManager_) -> + VariantUnitManager = _VariantUnitManager_ - describe "getUnitName", -> - it "returns the unit name based on the scale and unit type (weight/volume) provided", -> - expect(VariantUnitManager.getUnitName(1, "weight")).toEqual "g" - expect(VariantUnitManager.getUnitName(1000, "weight")).toEqual "kg" - expect(VariantUnitManager.getUnitName(1000000, "weight")).toEqual "T" - expect(VariantUnitManager.getUnitName(0.001, "volume")).toEqual "mL" - expect(VariantUnitManager.getUnitName(1, "volume")).toEqual "L" - expect(VariantUnitManager.getUnitName(1000, "volume")).toEqual "kL" - expect(VariantUnitManager.getUnitName(453.6, "weight")).toEqual "lb" - expect(VariantUnitManager.getUnitName(28.35, "weight")).toEqual "oz" + describe "getUnitName", -> + it "returns the unit name based on the scale and unit type (weight/volume) provided", -> + expect(VariantUnitManager.getUnitName(1, "weight")).toEqual "g" + expect(VariantUnitManager.getUnitName(1000, "weight")).toEqual "kg" + expect(VariantUnitManager.getUnitName(1000000, "weight")).toEqual "T" + expect(VariantUnitManager.getUnitName(0.001, "volume")).toEqual "mL" + expect(VariantUnitManager.getUnitName(1, "volume")).toEqual "L" + expect(VariantUnitManager.getUnitName(1000, "volume")).toEqual "kL" + expect(VariantUnitManager.getUnitName(453.6, "weight")).toEqual "lb" + expect(VariantUnitManager.getUnitName(28.35, "weight")).toEqual "oz" - describe "unitScales", -> - it "returns a sorted set of scales for unit type weight", -> - expect(VariantUnitManager.unitScales('weight')).toEqual [0.001, 1.0, 28.35, 453.6, 1000.0, 1000000.0] + describe "unitScales", -> + it "returns a sorted set of scales for unit type weight", -> + expect(VariantUnitManager.unitScales('weight')).toEqual [0.001, 1.0, 28.35, 453.6, 1000.0, 1000000.0] - it "returns a sorted set of scales for unit type volume", -> - expect(VariantUnitManager.unitScales('volume')).toEqual [0.001, 0.01, 0.1, 1.0, 4.54609, 1000.0] + it "returns a sorted set of scales for unit type volume", -> + expect(VariantUnitManager.unitScales('volume')).toEqual [0.001, 0.01, 0.1, 1.0, 4.54609, 1000.0] - describe "compatibleUnitScales", -> - it "returns a sorted set of compatible scales based on the scale and unit type provided", -> - expect(VariantUnitManager.compatibleUnitScales(1, "weight")).toEqual [1.0, 1000.0, 1000000.0] - expect(VariantUnitManager.compatibleUnitScales(453.6, "weight")).toEqual [28.35, 453.6] - - pending "should load only available unit", -> - beforeEach -> - module "admin.products" - module ($provide)-> - $provide.value "availableUnits", "g,T,mL,L,kL,lb" - null - inject (_VariantUnitManager_) -> - VariantUnitManager1 = _VariantUnitManager_ + describe "compatibleUnitScales", -> it "returns a sorted set of compatible scales based on the scale and unit type provided", -> - expect(VariantUnitManager1.compatibleUnitScales(1, "weight")).toEqual [1.0, 1000000.0] - expect(VariantUnitManager1.compatibleUnitScales(453.6, "weight")).toEqual [453.6] + expect(VariantUnitManager.compatibleUnitScales(1, "weight")).toEqual [1.0, 1000.0, 1000000.0] + expect(VariantUnitManager.compatibleUnitScales(453.6, "weight")).toEqual [28.35, 453.6] - describe "variantUnitOptions", -> - it "returns an array of options", -> - expect(VariantUnitManager.variantUnitOptions()).toEqual [ - ["Weight (g)", "weight_1"], - ["Weight (oz)", "weight_28.35" ], - ["Weight (lb)", "weight_453.6" ] - ["Weight (kg)", "weight_1000"], - ["Weight (T)", "weight_1000000"], - ["Volume (mL)", "volume_0.001"], - ["Volume (L)", "volume_1"], - ["Volume (kL)", "volume_1000"], - ["Items", "items"] - ] + describe "variantUnitOptions", -> + it "returns an array of options", -> + expect(VariantUnitManager.variantUnitOptions()).toEqual [ + ["Weight (g)", "weight_1"], + ["Weight (oz)", "weight_28.35" ], + ["Weight (lb)", "weight_453.6" ] + ["Weight (kg)", "weight_1000"], + ["Weight (T)", "weight_1000000"], + ["Volume (mL)", "volume_0.001"], + ["Volume (L)", "volume_1"], + ["Volume (kL)", "volume_1000"], + ["Items", "items"] + ] + + describe "should only load available units", -> + beforeEach -> + module "admin.products" + module ($provide)-> + $provide.value "availableUnits", "g,T,mL,L,kL,lb" + null + beforeEach inject (_VariantUnitManager_) -> + VariantUnitManager = _VariantUnitManager_ + + describe "compatibleUnitScales", -> + it "returns a sorted set of compatible scales based on the scale and unit type provided", -> + expect(VariantUnitManager.compatibleUnitScales(1, "weight")).toEqual [1.0, 1000000.0] + expect(VariantUnitManager.compatibleUnitScales(453.6, "weight")).toEqual [453.6] From 847c3af4da4395afe891f6c227e321c21c3a6638 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 13 Nov 2023 09:06:51 +0100 Subject: [PATCH 10/10] update WeightsAndMeasures#scales_for_variant_unit to ignore available untis when needed --- app/services/weights_and_measures.rb | 8 +++++--- .../api/v0/order_cycles_controller_spec.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/services/weights_and_measures.rb b/app/services/weights_and_measures.rb index 32e1811457..7fd8528fe7 100644 --- a/app/services/weights_and_measures.rb +++ b/app/services/weights_and_measures.rb @@ -14,7 +14,7 @@ class WeightsAndMeasures end def system - return "custom" unless scales = scales_for_variant_unit + return "custom" unless scales = scales_for_variant_unit(ignore_available_units: true) return "custom" unless product_scale = @variant.product.variant_unit_scale scales[product_scale.to_f]['system'] @@ -45,8 +45,10 @@ class WeightsAndMeasures } }.freeze - def scales_for_variant_unit - @scales_for_variant_unit ||= @units[@variant.product.variant_unit]&.reject { |_scale, unit_info| + def scales_for_variant_unit(ignore_available_units: false) + return @units[@variant.product.variant_unit] if ignore_available_units + + @units[@variant.product.variant_unit]&.reject { |_scale, unit_info| available_units.exclude?(unit_info['name']) } end diff --git a/spec/controllers/api/v0/order_cycles_controller_spec.rb b/spec/controllers/api/v0/order_cycles_controller_spec.rb index 44a72be65a..dfc6010ec1 100644 --- a/spec/controllers/api/v0/order_cycles_controller_spec.rb +++ b/spec/controllers/api/v0/order_cycles_controller_spec.rb @@ -38,6 +38,18 @@ module Api expect(product_ids).to include product1.id, product2.id, product3.id end + context "when product's variant unit scale is not in the available units list" do + before do + allow(Spree::Config).to receive(:available_units).and_return("lb,oz,kg,T,mL,L,kL") + end + + it "loads products for distributed products in the order cycle" do + api_get :products, id: order_cycle.id, distributor: distributor.id + + expect(product_ids).to include product1.id, product2.id, product3.id + end + end + it "returns products that were searched for" do ransack_param = "name_or_meta_keywords_or_variants_display_as_or_" \ "variants_display_name_or_supplier_name_cont"