Sha256: c2a08c59ce5879080677cd34fe0d138c252b8dbef96b13ede80770d037424e1a

Contents?: true

Size: 1.64 KB

Versions: 14

Compression:

Stored size: 1.64 KB

Contents

module Spree
  module PromotionHandler
    class PromotionDuplicator
      def initialize(promotion, random_string: nil)
        @promotion = promotion
        @random_string = random_string || generate_random_string(4)
      end

      def duplicate
        @new_promotion = @promotion.dup
        @new_promotion.path = "#{@promotion.path}_#{@random_string}"
        @new_promotion.name = "New #{@promotion.name}"
        @new_promotion.code = "#{@promotion.code}_#{@random_string}"

        ActiveRecord::Base.transaction do
          @new_promotion.save
          copy_rules
          copy_actions
        end

        @new_promotion
      end

      private

      def copy_rules
        @promotion.promotion_rules.each do |rule|
          new_rule = rule.dup
          @new_promotion.promotion_rules << new_rule

          new_rule.users = rule.users if rule.try(:users)
          new_rule.taxons = rule.taxons if rule.try(:taxons)
          new_rule.products = rule.products if rule.try(:products)
        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
          new_action.calculator = action.calculator.dup if action.try(:calculator)

          @new_promotion.promotion_actions << new_action

          next unless action.try(:promotion_action_line_items)

          action.promotion_action_line_items.each do |item|
            new_action.promotion_action_line_items << item.dup
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
spree_core-4.2.7 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.6 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.5 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.4 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.3.1 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.3 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.2 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.1 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.0 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.0.rc5 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.0.rc4 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.0.rc3 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.0.rc2 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-4.2.0.rc1 app/models/spree/promotion_handler/promotion_duplicator.rb