diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index 8701990bb7..2ffb07d4e1 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -56,9 +56,8 @@ Spree::Variant.class_eval do delete_unit_option_values option_type = self.product.variant_unit_option_type - option_value_namer = OpenFoodNetwork::OptionValueNamer.new self if option_type - name = option_value_namer.name + name = option_value_name ov = Spree::OptionValue.where(option_type_id: option_type, name: name, presentation: name).first || Spree::OptionValue.create!({option_type: option_type, name: name, presentation: name}, without_protection: true) option_values << ov end diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index 932630d640..69002d2aa4 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -229,6 +229,26 @@ module Spree end end + context "when the variant already has a value set (and all required option values exist)" do + let!(:p0) { create(:simple_product, variant_unit: 'weight', variant_unit_scale: 1) } + let!(:v0) { create(:variant, product: p0, unit_value: 10, unit_description: 'foo') } + + let!(:p) { create(:simple_product, variant_unit: 'weight', variant_unit_scale: 1) } + let!(:v) { create(:variant, product: p, unit_value: 5, unit_description: 'bar') } + + it "removes the old option value and assigns the new one" do + ov_orig = v.option_values.last + ov_new = v0.option_values.last + + expect { + v.update_attributes!(unit_value: 10, unit_description: 'foo') + }.to change(Spree::OptionValue, :count).by(0) + + v.option_values.should_not include ov_orig + v.option_values.should include ov_new + end + end + context "when the variant does not have a display_as value set" do let!(:p) { create(:simple_product, variant_unit: 'weight', variant_unit_scale: 1) } let!(:v) { create(:variant, product: p, unit_value: 5, unit_description: 'bar', display_as: '') }