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")