From f44228eef73576e200928b824c22869f9eed135d Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Wed, 19 Oct 2022 10:45:56 +0100 Subject: [PATCH 1/6] extract variant duplication logic into a separated function --- lib/spree/core/product_duplicator.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/spree/core/product_duplicator.rb b/lib/spree/core/product_duplicator.rb index 68a1ced2d7..486faacbce 100644 --- a/lib/spree/core/product_duplicator.rb +++ b/lib/spree/core/product_duplicator.rb @@ -33,12 +33,16 @@ module Spree 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_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 From 788093c7cf2e21bce3897612de1dae9da93f4db5 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Wed, 19 Oct 2022 11:04:02 +0100 Subject: [PATCH 2/6] implement variant duplication --- lib/spree/core/product_duplicator.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/spree/core/product_duplicator.rb b/lib/spree/core/product_duplicator.rb index 486faacbce..15c999cf70 100644 --- a/lib/spree/core/product_duplicator.rb +++ b/lib/spree/core/product_duplicator.rb @@ -28,6 +28,7 @@ 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 @@ -36,6 +37,12 @@ module Spree 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 = "" From 11bc5c775c56d793c7b3aebafbf5667b1defdd1f Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 24 Oct 2022 09:29:07 +0100 Subject: [PATCH 3/6] rename the variant double to master_variant --- spec/lib/spree/core/product_duplicator_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/lib/spree/core/product_duplicator_spec.rb b/spec/lib/spree/core/product_duplicator_spec.rb index 452aa51c12..691e9e80f0 100644 --- a/spec/lib/spree/core/product_duplicator_spec.rb +++ b/spec/lib/spree/core/product_duplicator_spec.rb @@ -8,7 +8,7 @@ describe Spree::Core::ProductDuplicator do name: "foo", taxons: [], product_properties: [property], - master: variant, + master: master_variant, option_types: [] end @@ -25,7 +25,7 @@ describe Spree::Core::ProductDuplicator do double 'New Property' end - let(:variant) do + let(:master_variant) do double 'Variant', sku: "12345", price: 19.99, @@ -49,7 +49,7 @@ describe Spree::Core::ProductDuplicator do before do expect(product).to receive(:dup).and_return(new_product) - expect(variant).to receive(:dup).and_return(new_variant) + expect(master_variant).to receive(:dup).and_return(new_variant) expect(image).to receive(:dup).and_return(new_image) expect(property).to receive(:dup).and_return(new_property) end @@ -68,8 +68,8 @@ describe Spree::Core::ProductDuplicator do 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(:price=).with(variant.price) - expect(new_variant).to receive(:currency=).with(variant.currency) + expect(new_variant).to receive(:price=).with(master_variant.price) + expect(new_variant).to receive(:currency=).with(master_variant.currency) expect(image).to receive(:attachment_blob) expect(new_image).to receive_message_chain(:attachment, :attach) From 6cabbee1a21f939b4ef939aaade4c4015953a5cf Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 24 Oct 2022 09:30:35 +0100 Subject: [PATCH 4/6] rename new_variant double to new_master_variant --- spec/lib/spree/core/product_duplicator_spec.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/lib/spree/core/product_duplicator_spec.rb b/spec/lib/spree/core/product_duplicator_spec.rb index 691e9e80f0..c7e2fff1ea 100644 --- a/spec/lib/spree/core/product_duplicator_spec.rb +++ b/spec/lib/spree/core/product_duplicator_spec.rb @@ -33,7 +33,7 @@ describe Spree::Core::ProductDuplicator do images: [image] end - let(:new_variant) do + let(:new_master_variant) do double 'New Variant', sku: "12345" end @@ -49,7 +49,7 @@ describe Spree::Core::ProductDuplicator do before do expect(product).to receive(:dup).and_return(new_product) - expect(master_variant).to receive(:dup).and_return(new_variant) + expect(master_variant).to receive(:dup).and_return(new_master_variant) expect(image).to receive(:dup).and_return(new_image) expect(property).to receive(:dup).and_return(new_property) end @@ -62,14 +62,14 @@ 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_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(:price=).with(master_variant.price) - expect(new_variant).to receive(:currency=).with(master_variant.currency) + 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(image).to receive(:attachment_blob) expect(new_image).to receive_message_chain(:attachment, :attach) From 826b1e95ef5d6315f1b43e7fe0ad927fad56c2af Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 24 Oct 2022 09:43:39 +0100 Subject: [PATCH 5/6] add a product variant. --- .../lib/spree/core/product_duplicator_spec.rb | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/spec/lib/spree/core/product_duplicator_spec.rb b/spec/lib/spree/core/product_duplicator_spec.rb index c7e2fff1ea..0c1c3023a6 100644 --- a/spec/lib/spree/core/product_duplicator_spec.rb +++ b/spec/lib/spree/core/product_duplicator_spec.rb @@ -9,6 +9,7 @@ describe Spree::Core::ProductDuplicator do taxons: [], product_properties: [property], master: master_variant, + variants: [variant], option_types: [] end @@ -33,11 +34,24 @@ describe Spree::Core::ProductDuplicator do images: [image] end + 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,6 +61,16 @@ 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) From 41440af533fa3e764523d49eafb7348989305b89 Mon Sep 17 00:00:00 2001 From: Mohamed ABDELLANI Date: Mon, 24 Oct 2022 09:50:05 +0100 Subject: [PATCH 6/6] test if product's variant is duplicated properly --- spec/lib/spree/core/product_duplicator_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/lib/spree/core/product_duplicator_spec.rb b/spec/lib/spree/core/product_duplicator_spec.rb index 0c1c3023a6..58047b1224 100644 --- a/spec/lib/spree/core/product_duplicator_spec.rb +++ b/spec/lib/spree/core/product_duplicator_spec.rb @@ -74,7 +74,9 @@ describe Spree::Core::ProductDuplicator do 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 @@ -88,6 +90,7 @@ describe Spree::Core::ProductDuplicator do expect(new_product).to receive(:deleted_at=).with(nil) 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) @@ -95,9 +98,18 @@ describe Spree::Core::ProductDuplicator do 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_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)