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 ad16ae7cff5..845827adc4e 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 @@ -4,7 +4,15 @@ angular.module("admin.products").factory "VariantUnitManager", -> 'weight': 1.0: 'g' 1000.0: 'kg' - 1000000.0: 'T' + 1000000.0: 'T', + # This appears to be what needs to be set in order for + # products to have a mass value stored in the database + # when they are created. However, it does not appear to + # change the existing product(s), so if the scale value + # is changed, a data migration may be necessary to make sure + # the proper unit X to grams actually works. + # TODO: ^^^ Delete this + 453.592: 'lb' 'volume': 0.001: 'mL' 1.0: 'L' diff --git a/app/models/product_import/unit_converter.rb b/app/models/product_import/unit_converter.rb index e97cff43f96..c7e5464329d 100644 --- a/app/models/product_import/unit_converter.rb +++ b/app/models/product_import/unit_converter.rb @@ -32,6 +32,10 @@ def unit_scales { 'g' => { scale: 1, unit: 'weight' }, 'kg' => { scale: 1000, unit: 'weight' }, + # We have _no idea_ what this is doing. It has units? + # And it maybe is connected to something related to shipping? + # TODO: DELETE THIS ^^^ + 'lb' => { scale: 453.592, unit: 'weight' }, 't' => { scale: 1_000_000, unit: 'weight' }, 'ml' => { scale: 0.001, unit: 'volume' }, 'l' => { scale: 1, unit: 'volume' }, diff --git a/lib/open_food_network/option_value_namer.rb b/lib/open_food_network/option_value_namer.rb index 706a0b443de..896412856a6 100644 --- a/lib/open_food_network/option_value_namer.rb +++ b/lib/open_food_network/option_value_namer.rb @@ -63,7 +63,15 @@ def option_value_value_unit_scaled end def scale_for_unit_value - units = { 'weight' => { 1.0 => 'g', 1000.0 => 'kg', 1_000_000.0 => 'T' }, + # TODO: We _think_ this will cause the following weird results: + # 29g of product would use the `oz` measurement + # 445g of product would use the `lb` measurement + # So we probably want to keep the metric and imperial measures + # in their own lanes; perhaps using a configurable value on a per + # shop or producer basis? + + units = { 'weight' => { 1.0 => 'g', 1000.0 => 'kg', 1_000_000.0 => 'T', + 28.34952 => 'oz', 453.59237 => 'lb'}, '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. diff --git a/lib/open_food_network/variant_and_line_item_naming.rb b/lib/open_food_network/variant_and_line_item_naming.rb index 596b93beee5..19a7f7a6646 100644 --- a/lib/open_food_network/variant_and_line_item_naming.rb +++ b/lib/open_food_network/variant_and_line_item_naming.rb @@ -18,7 +18,18 @@ def options_text order("#{Spree::OptionType.table_name}.position asc") end - values.map(&:presentation).to_sentence(words_connector: ", ", two_words_connector: ", ") + values.map { |option_value| presentation(option_value) }.to_sentence(words_connector: ", ", two_words_connector: ", ") + end + + def presentation(option_value) + if option_value.option_type.name == "unit_weight" + if has_attribute?(:display_as) && display_as.present? + return display_as + elsif respond_to?(:variant) && variant.present? && variant.respond_to?(:display_as) && variant.display_as.present? + return variant.display_as + end + end + option_value.presentation end def product_and_full_name @@ -48,9 +59,13 @@ def name_to_display end def unit_to_display - return options_text if !has_attribute?(:display_as) || display_as.blank? - - display_as + if has_attribute?(:display_as) && display_as.present? + display_as + elsif respond_to?(:variant) && variant.present? && variant.respond_to?(:display_as) && variant.display_as.present? + variant.display_as + else + options_text + end end def update_units