Use after_create callback to duplicate master variant

This commit is contained in:
Rob Harrington
2014-09-26 13:11:32 +10:00
parent 3f01a459ac
commit dfb513cce7
2 changed files with 20 additions and 7 deletions

View File

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

View File

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