Sha256: 3f268dd7576813b605b38718f306d0036e1c18f2c52c60e1fe3f62e3968ce925

Contents?: true

Size: 1.9 KB

Versions: 3

Compression:

Stored size: 1.9 KB

Contents

# This migration comes from spree (originally 20130807024301)
class UpgradeAdjustments < ActiveRecord::Migration[4.2]
  def up
    # Add Temporary index
    add_index :spree_adjustments, :originator_type unless index_exists?(:spree_adjustments, :originator_type)

    # Temporarily make originator association available
    Spree::Adjustment.class_eval do
      belongs_to :originator, polymorphic: true
    end
    # Shipping adjustments are now tracked as fields on the object
    Spree::Adjustment.where(source_type: "Spree::Shipment").find_each do |adjustment|
      # Account for possible invalid data
      next if adjustment.source.nil?
      adjustment.source.update_column(:cost, adjustment.amount)
      adjustment.destroy!
    end

    # Tax adjustments have their sources altered
    Spree::Adjustment.where(originator_type: "Spree::TaxRate").find_each do |adjustment|
      adjustment.source_id = adjustment.originator_id
      adjustment.source_type = "Spree::TaxRate"
      adjustment.save!
    end

    # Promotion adjustments have their source altered also
    Spree::Adjustment.where(originator_type: "Spree::PromotionAction").find_each do |adjustment|
      next if adjustment.originator.nil?
      adjustment.source = adjustment.originator
      begin
        if adjustment.source.calculator_type == "Spree::Calculator::FreeShipping"
          # Previously this was a Spree::Promotion::Actions::CreateAdjustment
          # And it had a calculator to work out FreeShipping
          # In Spree 2.2, the "calculator" is now the action itself.
          adjustment.source.becomes(Spree::Promotion::Actions::FreeShipping)
        end
      rescue
        # Fail silently. This is primarily in instances where the calculator no longer exists
      end

      adjustment.save!
    end

    # Remove Temporary index
    remove_index :spree_adjustments, :originator_type if index_exists?(:spree_adjustments, :originator_type)
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
spree_purchase_order-3.7.0 spec/dummy/db/migrate/20191113195793_upgrade_adjustments.spree.rb
spree_billing_sisow-0.9.2 spec/dummy/db/migrate/20190729091734_upgrade_adjustments.spree.rb
spree_billing_sisow-0.9.1 spec/dummy/db/migrate/20190729091734_upgrade_adjustments.spree.rb