Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Add random string instead of 'new' in a duplicated promotion.

Damian Kaczmarczyk6852661e51ae
diff --git a/core/app/models/spree/promotion_handler/promotion_duplicator.rb b/core/app/models/spree/promotion_handler/promotion_duplicator.rb
index bac5b01428..e57140a173 100644
--- a/core/app/models/spree/promotion_handler/promotion_duplicator.rb
+++ b/core/app/models/spree/promotion_handler/promotion_duplicator.rb
@@ -7,9 +7,10 @@ module Spree
def duplicate
@new_promotion = @promotion.dup
- @new_promotion.path = "#{@promotion.path}_new"
+ random_string = generate_random_string(4)
+ @new_promotion.path = "#{@promotion.path}_#{random_string}"
@new_promotion.name = "New #{@promotion.name}"
- @new_promotion.code = "#{@promotion.code}_new"
+ @new_promotion.code = "#{@promotion.code}_#{random_string}"
ActiveRecord::Base.transaction do
@new_promotion.save
@@ -33,6 +34,11 @@ module Spree
end
end
+ def generate_random_string(number)
+ charset = Array('A'..'Z') + Array('a'..'z')
+ Array.new(number) { charset.sample }.join
+ end
+
def copy_actions
@promotion.promotion_actions.each do |action|
new_action = action.dup
diff --git a/core/spec/models/spree/promotion_handler/promotion_duplicator_spec.rb b/core/spec/models/spree/promotion_handler/promotion_duplicator_spec.rb
index 57908e0fc5..848b960d9a 100644
--- a/core/spec/models/spree/promotion_handler/promotion_duplicator_spec.rb
+++ b/core/spec/models/spree/promotion_handler/promotion_duplicator_spec.rb
@@ -30,9 +30,9 @@ describe Spree::PromotionHandler::PromotionDuplicator do
let(:excluded_fields) { ['code', 'name', 'path', 'id', 'created_at', 'updated_at', 'deleted_at'] }
it 'returns a duplicate of a promotion with the path, name and code fields changed' do
- expect("#{promotion.path}_new").to eq new_promotion.path
expect("New #{promotion.name}").to eq new_promotion.name
- expect("#{promotion.code}_new").to eq new_promotion.code
+ expect(new_promotion.path).to match /#{promotion.path}_[a-zA-Z]{4}/
+ expect(new_promotion.code).to match /#{promotion.code}_[a-zA-Z]{4}/
end
it 'returns a duplicate of a promotion with all the fields (except the path, name and code fields) the same' do