class Solidus::Migrations::PromotionWithCodeHandlers::MoveToSpreePromotionCode

Public Instance Methods

call() click to toggle source
# File lib/solidus/migrations/promotions_with_code_handlers.rb, line 34
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