Class: SolidusLegacyPromotions::Migrations::PromotionWithCodeHandlers::MoveToSpreePromotionCode

Inherits:
Base
  • Object
show all
Defined in:
lib/solidus_legacy_promotions/migrations/promotions_with_code_handlers.rb

Instance Attribute Summary

Attributes inherited from Base

#migration_context, #promotions

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from SolidusLegacyPromotions::Migrations::PromotionWithCodeHandlers::Base

Instance Method Details

#callObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/solidus_legacy_promotions/migrations/promotions_with_code_handlers.rb', line 36

def call
  # This is another possible approach, it will convert Spree::Promotion#code
  # to a Spree::PromotionCode before removing the `code` field.
  #
  # NOTE: promotion codes will be downcased and stripped
  promotions.find_each do |promotion|
    normalized_code = promotion.code.downcase.strip

    PromotionCode.find_or_create_by!(
      value: normalized_code,
      promotion_id: promotion.id
    ) do
      migration_context.say "Creating Spree::PromotionCode with value "\
       "'#{normalized_code}' for Spree::Promotion with id '#{promotion.id}'"
    end
  end
end