diff --git a/lib/spree/core/product_duplicator.rb b/lib/spree/core/product_duplicator.rb index 68a1ced2d7..15c999cf70 100644 --- a/lib/spree/core/product_duplicator.rb +++ b/lib/spree/core/product_duplicator.rb @@ -28,17 +28,28 @@ module Spree new_product.updated_at = nil new_product.product_properties = reset_properties new_product.master = duplicate_master + new_product.variants = duplicate_variants end end def duplicate_master master = product.master - master.dup.tap do |new_master| - new_master.sku = "" - new_master.deleted_at = nil - new_master.images = master.images.map { |image| duplicate_image image } - new_master.price = master.price - new_master.currency = master.currency + duplicate_variant(master) + end + + def duplicate_variants + product.variants.map do |variant| + duplicate_variant(variant) + end + end + + def duplicate_variant(variant) + variant.dup.tap do |new_variant| + new_variant.sku = "" + new_variant.deleted_at = nil + new_variant.images = variant.images.map { |image| duplicate_image image } + new_variant.price = variant.price + new_variant.currency = variant.currency end end diff --git a/spec/lib/spree/core/product_duplicator_spec.rb b/spec/lib/spree/core/product_duplicator_spec.rb index 452aa51c12..58047b1224 100644 --- a/spec/lib/spree/core/product_duplicator_spec.rb +++ b/spec/lib/spree/core/product_duplicator_spec.rb @@ -8,7 +8,8 @@ describe Spree::Core::ProductDuplicator do name: "foo", taxons: [], product_properties: [property], - master: variant, + master: master_variant, + variants: [variant], option_types: [] end @@ -25,7 +26,7 @@ describe Spree::Core::ProductDuplicator do double 'New Property' end - let(:variant) do + let(:master_variant) do double 'Variant', sku: "12345", price: 19.99, @@ -33,11 +34,24 @@ describe Spree::Core::ProductDuplicator do images: [image] end - let(:new_variant) do + let(:variant) do + double 'Variant 1', + sku: "67890", + price: 19.50, + currency: "AUD", + images: [image_variant] + end + + let(:new_master_variant) do double 'New Variant', sku: "12345" end + let(:new_variant) do + double 'New Variant 1', + sku: "67890" + end + let(:image) do double 'Image', attachment: double('Attachment') @@ -47,10 +61,22 @@ describe Spree::Core::ProductDuplicator do double 'New Image' end + let(:image_variant) do + double 'Image Variant', + attachment: double('Attachment') + end + + let(:new_image_variant) do + double 'New Image Variant', + attachment: double('Attachment') + end + before do expect(product).to receive(:dup).and_return(new_product) + expect(master_variant).to receive(:dup).and_return(new_master_variant) expect(variant).to receive(:dup).and_return(new_variant) expect(image).to receive(:dup).and_return(new_image) + expect(image_variant).to receive(:dup).and_return(new_image_variant) expect(property).to receive(:dup).and_return(new_property) end @@ -62,18 +88,28 @@ describe Spree::Core::ProductDuplicator do expect(new_product).to receive(:created_at=).with(nil) expect(new_product).to receive(:updated_at=).with(nil) expect(new_product).to receive(:deleted_at=).with(nil) - expect(new_product).to receive(:master=).with(new_variant) + expect(new_product).to receive(:master=).with(new_master_variant) expect(new_product).to receive(:option_types=).with([]) + expect(new_product).to receive(:variants=).with([new_variant]) + + expect(new_master_variant).to receive(:sku=).with("") + expect(new_master_variant).to receive(:deleted_at=).with(nil) + expect(new_master_variant).to receive(:images=).with([new_image]) + expect(new_master_variant).to receive(:price=).with(master_variant.price) + expect(new_master_variant).to receive(:currency=).with(master_variant.currency) expect(new_variant).to receive(:sku=).with("") expect(new_variant).to receive(:deleted_at=).with(nil) - expect(new_variant).to receive(:images=).with([new_image]) + expect(new_variant).to receive(:images=).with([new_image_variant]) expect(new_variant).to receive(:price=).with(variant.price) expect(new_variant).to receive(:currency=).with(variant.currency) expect(image).to receive(:attachment_blob) expect(new_image).to receive_message_chain(:attachment, :attach) + expect(image_variant).to receive(:attachment_blob) + expect(new_image_variant).to receive_message_chain(:attachment, :attach) + expect(new_property).to receive(:created_at=).with(nil) expect(new_property).to receive(:updated_at=).with(nil)