# Legacy version of model so migrations work with legacy Spree data class Order < ActiveRecord::Base has_many :charges, :order => :position has_many :shipping_charges has_many :tax_charges end # Legacy version of model so migrations work with legacy Spree data class Charge < Adjustment belongs_to :order acts_as_list :scope => :order end # Legacy version of model so migrations work with legacy Spree data class ShippingCharge < Charge end # Legacy version of model so migrations work with legacy Spree data class TaxCharge < Charge end class CreateCharges < ActiveRecord::Migration def self.up create_table :charges do |t| t.references :order t.string :type t.decimal :amount, :precision => 8, :scale => 2, :default => 0.0, :null => false t.string :description t.integer :position t.timestamps end change_table :orders do |t| t.decimal :charge_total, :precision => 8, :scale => 2, :default => 0.0, :null => false end # create shipping and taxation charges for order, then drop columns Order.reset_column_information Order.all.each do |order| ship_total = order.attributes["ship_amount"] || 0 tax_total = order.attributes["tax_amount"] || 0 order.shipping_charges.reset execute "INSERT INTO charges (`order_id`, `amount`, `description`, `type`) VALUES (#{order.id}, #{ship_total}, 'Shipping', 'Shipping')" execute "INSERT INTO charges (`order_id`, `amount`, `description`, `type`) VALUES (#{order.id}, #{tax_total}, 'Tax', 'Tax')" execute "UPDATE orders SET charge_total = #{ship_total + tax_total} WHERE id = #{order.id}" end change_table :orders do |t| t.remove :ship_amount t.remove :tax_amount end end def self.down drop_table :charges change_table :orders do |t| t.remove :charge_total end end end