Class: SolidusFriendlyPromotions::PromotionMigrator

Inherits:
Object
  • Object
show all
Defined in:
lib/solidus_friendly_promotions/promotion_migrator.rb

Constant Summary collapse

PROMOTION_IGNORED_ATTRIBUTES =
["id", "type", "promotion_category_id", "promotion_id"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(promotion_map) ⇒ PromotionMigrator

Returns a new instance of PromotionMigrator.



9
10
11
# File 'lib/solidus_friendly_promotions/promotion_migrator.rb', line 9

def initialize(promotion_map)
  @promotion_map = promotion_map
end

Instance Attribute Details

#promotion_mapObject (readonly)

Returns the value of attribute promotion_map.



7
8
9
# File 'lib/solidus_friendly_promotions/promotion_migrator.rb', line 7

def promotion_map
  @promotion_map
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/solidus_friendly_promotions/promotion_migrator.rb', line 13

def call
  SolidusFriendlyPromotions::PromotionCategory.destroy_all
  Spree::PromotionCategory.all.each do |promotion_category|
    SolidusFriendlyPromotions::PromotionCategory.create!(promotion_category.attributes.except("id"))
  end

  SolidusFriendlyPromotions::Promotion.destroy_all
  Spree::Promotion.all.each do |promotion|
    new_promotion = copy_promotion(promotion)
    if promotion.promotion_category&.name.present?
      new_promotion.category = SolidusFriendlyPromotions::PromotionCategory.find_by(
        name: promotion.promotion_category.name
      )
    end
    new_promotion.benefits = promotion.actions.flat_map do |old_promotion_action|
      generate_new_benefits(old_promotion_action)&.tap do |new_promotion_action|
        new_promotion_action.original_promotion_action = old_promotion_action
        new_promotion_action.conditions = promotion.rules.flat_map do |old_promotion_rule|
          generate_new_promotion_conditions(old_promotion_rule)
        end
      end
    end.compact
    new_promotion.save!
    copy_promotion_code_batches(new_promotion)
    copy_promotion_codes(new_promotion)
  end
end