diff --git a/app/assets/javascripts/admin/services/option_value_namer.js.coffee b/app/assets/javascripts/admin/services/option_value_namer.js.coffee index ea723d8b57..1bea9b82f1 100644 --- a/app/assets/javascripts/admin/services/option_value_namer.js.coffee +++ b/app/assets/javascripts/admin/services/option_value_namer.js.coffee @@ -1,4 +1,4 @@ -angular.module("admin.products").factory "OptionValueNamer", -> +angular.module("admin.products").factory "OptionValueNamer", (VariantUnitManager) -> class OptionValueNamer constructor: (@variant) -> @@ -40,26 +40,16 @@ angular.module("admin.products").factory "OptionValueNamer", -> [value, unit_name] scale_for_unit_value: -> - units = - 'weight': - 1.0: 'g' - 1000.0: 'kg' - 1000000.0: 'T' - 'volume': - 0.001: 'mL' - 1.0: 'L' - 1000.0: 'kL' - # Find the largest available unit where unit_value comes to >= 1 when expressed in it. # If there is none available where this is true, use the smallest available unit. - unit = ([scale, unit_name] for scale, unit_name of units[@variant.product.variant_unit] when @variant.unit_value / scale >= 1).reduce (unit, [scale, unit_name]) -> + unit = ([scale, unit_name] for scale, unit_name of VariantUnitManager.unitNames[@variant.product.variant_unit] when @variant.unit_value / scale >= 1).reduce (unit, [scale, unit_name]) -> if (unit && scale > unit[0]) || !unit? [scale, unit_name] else unit , null if !unit? - unit = ([scale, unit_name] for scale, unit_name of units[@variant.product.variant_unit]).reduce (unit, [scale, unit_name]) -> + unit = ([scale, unit_name] for scale, unit_name of VariantUnitManager.unitNames[@variant.product.variant_unit]).reduce (unit, [scale, unit_name]) -> if scale < unit[0] then [scale, unit_name] else unit , [Infinity,""] diff --git a/app/assets/javascripts/admin/services/variant_unit_manager.js.coffee b/app/assets/javascripts/admin/services/variant_unit_manager.js.coffee index 2514e34535..dbdac44808 100644 --- a/app/assets/javascripts/admin/services/variant_unit_manager.js.coffee +++ b/app/assets/javascripts/admin/services/variant_unit_manager.js.coffee @@ -1,5 +1,25 @@ angular.module("admin.products").factory "VariantUnitManager", -> class VariantUnitManager + @unitNames: + 'weight': + 1.0: 'g' + 1000.0: 'kg' + 1000000.0: 'T' + 'volume': + 0.001: 'mL' + 1.0: 'L' + 1000.0: 'kL' + + @variant_unit_options: [ + ["Weight (g)", "weight_1"], + ["Weight (kg)", "weight_1000"], + ["Weight (T)", "weight_1000000"], + ["Volume (mL)", "volume_0.001"], + ["Volume (L)", "volume_1"], + ["Volume (kL)", "volume_1000"], + ["Items", "items"] + ] + @getScale = (value, unitType) -> scaledValue = null validScales = [] @@ -12,23 +32,10 @@ angular.module("admin.products").factory "VariantUnitManager", -> unitScales[0] @getUnitName: (scale, unitType) -> - unitNames = - 'weight': {1.0: 'g', 1000.0: 'kg', 1000000.0: 'T'} - 'volume': {0.001: 'mL', 1.0: 'L', 1000.0: 'kL'} - unitNames[unitType][scale] + @unitNames[unitType][scale] @unitScales: (unitType) -> unitScales = 'weight': [1.0, 1000.0, 1000000.0] 'volume': [0.001, 1.0, 1000.0] unitScales[unitType] - - @variant_unit_options: [ - ["Weight (g)", "weight_1"], - ["Weight (kg)", "weight_1000"], - ["Weight (T)", "weight_1000000"], - ["Volume (mL)", "volume_0.001"], - ["Volume (L)", "volume_1"], - ["Volume (kL)", "volume_1000"], - ["Items", "items"] - ]