From bb73d70e57d91cdb9f0c4d2f4f90c095afb47564 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 7 Jun 2023 12:17:50 +0100 Subject: [PATCH] Improve nil-safety in variant naming methods --- .../variant_units/variant_and_line_item_naming.rb | 5 +++-- spec/models/spree/variant_spec.rb | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/services/variant_units/variant_and_line_item_naming.rb b/app/services/variant_units/variant_and_line_item_naming.rb index e26c02b342..1ac0c79ba3 100644 --- a/app/services/variant_units/variant_and_line_item_naming.rb +++ b/app/services/variant_units/variant_and_line_item_naming.rb @@ -22,7 +22,8 @@ module VariantUnits end def product_and_full_name - return "#{product.name} - #{full_name}" unless full_name.start_with? product.name + return product.name if full_name.blank? + return "#{product.name} - #{full_name}" unless full_name.start_with?(product.name) full_name end @@ -51,7 +52,7 @@ module VariantUnits return display_as if has_attribute?(:display_as) && display_as.present? return variant.display_as if variant_display_as? - options_text + options_text.to_s end def assign_units diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index 72c897eb79..5fad1dcb28 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -432,6 +432,20 @@ describe Spree::Variant do expect(variant.product_and_full_name).to eq "Apple - Pink Lady" end end + + context "when related naming values are nil" do + before do + product.name = "Apples" + product.display_as = nil + variant.display_as = nil + variant.unit_presentation = nil + end + + it "returns empty string or product name" do + expect(variant.full_name).to eq "" + expect(variant.product_and_full_name).to eq product.name + end + end end describe "calculating the price with enterprise fees" do