Sha256: 849ccbe08d0773a75dd49fc6596758e19fe234161d7b1f119214e0ebd63a7500

Contents?: true

Size: 1.9 KB

Versions: 6

Compression:

Stored size: 1.9 KB

Contents

# 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

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
spree-enriquez-0.9.4 db/migrate/20090618203233_create_charges.rb
spree-0.9.4 db/migrate/20090618203233_create_charges.rb
spree-0.9.3 db/migrate/20090618203233_create_charges.rb
spree-0.9.2 db/migrate/20090618203233_create_charges.rb
spree-0.9.1 db/migrate/20090618203233_create_charges.rb
spree-0.9.0 db/migrate/20090618203233_create_charges.rb