diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 034bbed043..aae90f41c4 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -32,7 +32,7 @@ Spree::Product.class_eval do validates_presence_of :variant_unit_name, if: -> p { p.variant_unit == 'items' } - after_initialize :ensure_first_standard_variant, :if => :new_record? + after_create :ensure_one_standard_variant after_initialize :set_available_on_to_now, :if => :new_record? after_save :update_units after_touch :touch_distributors @@ -206,7 +206,10 @@ Spree::Product.class_eval do Spree::OptionType.where('name LIKE ?', 'unit_%%') end - def ensure_first_standard_variant - self.variants << Spree::Variant.new + def ensure_one_standard_variant + variant = self.master.dup + variant.product = self + variant.is_master = false + self.variants << variant end end diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 73c6f2fe10..e3a72f19c5 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -85,11 +85,21 @@ module Spree end end - context "instatiating a new product" do - let!(:product) { Spree::Product.new } + context "saving a new product" do + let!(:product){ Spree::Product.new } - it "creates a standard (non-master) variant when created" do - product.variants.should_not be_empty + before do + product.primary_taxon = create(:taxon) + product.supplier = create(:supplier_enterprise) + product.name = "Product1" + product.on_hand = 3 + product.price = 4.27 + product.save! + end + + it "copies the properties on master variant to the first standard variant" do + standard_variant = product.variants(:reload).first + expect(standard_variant.price).to eq product.master.price end end