Sha256: 1b750def9a06871faa4de43caa03313be3a1e0722093b146c634a52bbc48b844

Contents?: true

Size: 1.64 KB

Versions: 4

Compression:

Stored size: 1.64 KB

Contents

# This migration comes from spree (originally 20130807024301)
class UpgradeAdjustments < ActiveRecord::Migration
  def up
    # 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
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
spree_weight_calculator-0.1.0 test/dummy/db/migrate/20160723192515_upgrade_adjustments.spree.rb
spree_order_reporting-0.0.3 spec/dummy/db/migrate/20160708163112_upgrade_adjustments.spree.rb
spree_order_reporting-0.0.2 spec/dummy/db/migrate/20160707103436_upgrade_adjustments.spree.rb
spree_order_reporting-0.0.1 spec/dummy/db/migrate/20160706112388_upgrade_adjustments.spree.rb