From f5e300a5debe44740f96db48d921d346ceec9b25 Mon Sep 17 00:00:00 2001 From: Zee Spencer <50284+zspencer@users.noreply.github.com> Date: Wed, 27 May 2020 11:47:07 -0700 Subject: [PATCH] Presenting a line item or variants options_text uses overriden values This changes how we display the description of weight, but it doesn't change the `Spree::OptionValue`s that are being created when someone adds a product to their cart. This takes us closer by making the UI look more correct; but it feels odd compared to settiong the `Spree::OptionValue` to the correct unit on creation. But on the other hand, that could possibly make things worse for the shipping calculation bits. --- .../variant_and_line_item_naming.rb | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) 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 7eed59b892..b38538a1a0 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 @@ module OpenFoodNetwork 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 @@ module OpenFoodNetwork 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