Improve nil-safety in variant naming methods

This commit is contained in:
Matt-Yorkley
2023-06-07 12:17:50 +01:00
parent 2d28a57c6f
commit bb73d70e57
2 changed files with 17 additions and 2 deletions

View File

@@ -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

View File

@@ -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