Sha256: 0457c3e195b2ce197924a121103e60e30467b8afb94b9bf80e9395d6a8fc7f12

Contents?: true

Size: 1.38 KB

Versions: 11

Compression:

Stored size: 1.38 KB

Contents

module Spree
  module PromotionHandler
    class PromotionDuplicator
      def initialize(promotion)
        @promotion = promotion
      end

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

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

          if 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
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
spree_core-3.6.6 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-3.6.5 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-3.6.4 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-3.6.3 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-3.6.2 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-3.6.1 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-3.6.0 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-3.5.0 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-3.6.0.rc1 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-3.5.0.rc2 app/models/spree/promotion_handler/promotion_duplicator.rb
spree_core-3.5.0.rc1 app/models/spree/promotion_handler/promotion_duplicator.rb