Merge pull request #10952 from Matt-Yorkley/full-name

Improve nil-safety in variant naming methods
This commit is contained in:
Filipe
2023-06-08 16:26:28 +01:00
committed by GitHub
2 changed files with 28 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,31 @@ describe Spree::Variant do
expect(variant.product_and_full_name).to eq "Apple - Pink Lady"
end
end
context "handling nil values for related naming attributes" do
it "returns empty string or product name" do
product.name = "Apple"
product.variant_unit = "items"
product.display_as = nil
variant.display_as = nil
variant.display_name = nil
expect(variant.full_name).to eq ""
expect(variant.product_and_full_name).to eq product.name
end
it "uses the display name correctly" do
product.name = "Apple"
product.variant_unit = "items"
product.display_as = nil
variant.display_as = nil
variant.unit_presentation = nil
variant.display_name = "Green"
expect(variant.full_name).to eq "Green"
expect(variant.product_and_full_name).to eq "Apple - Green"
end
end
end
describe "calculating the price with enterprise fees" do