Migrate line item shipping_method cache to distribution_fee/shipping_method_name

This commit is contained in:
Rohan Mitchell
2013-08-05 17:11:21 +10:00
parent 57e185af78
commit be1127b414
2 changed files with 48 additions and 6 deletions

View File

@@ -0,0 +1,41 @@
class UpdateLineItemCaching < ActiveRecord::Migration
class SpreeLineItem < ActiveRecord::Base
belongs_to :shipping_method, class_name: 'Spree::ShippingMethod'
def itemwise_shipping_cost
order = OpenStruct.new :line_items => [self]
shipping_method.compute_amount(order)
end
end
def up
add_column :spree_line_items, :distribution_fee, :decimal, precision: 10, scale: 2
add_column :spree_line_items, :shipping_method_name, :string
SpreeLineItem.all.each do |line_item|
line_item.update_column(:distribution_fee, line_item.itemwise_shipping_cost)
line_item.update_column(:shipping_method_name, line_item.shipping_method.name)
end
remove_column :spree_line_items, :shipping_method_id
end
def down
add_column :spree_line_items, :shipping_method_id, :integer
SpreeLineItem.all.each do |line_item|
shipping_method = Spree::ShippingMethod.find_by_name(line_item.shipping_method_name)
unless shipping_method
say "Shipping method #{line_item.shipping_method_name} not found, using the first available shipping method for LineItem #{line_item.id}"
shipping_method = Spree::ShippingMethod.where("name != 'Delivery'").first
end
line_item.update_column(:shipping_method_id, shipping_method.id)
end
remove_column :spree_line_items, :distribution_fee
remove_column :spree_line_items, :shipping_method_name
end
end

View File

@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20130801012854) do
ActiveRecord::Schema.define(:version => 20130805050109) do
create_table "cms_blocks", :force => true do |t|
t.integer "page_id", :null => false
@@ -364,13 +364,14 @@ ActiveRecord::Schema.define(:version => 20130801012854) do
create_table "spree_line_items", :force => true do |t|
t.integer "order_id"
t.integer "variant_id"
t.integer "quantity", :null => false
t.decimal "price", :precision => 8, :scale => 2, :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "quantity", :null => false
t.decimal "price", :precision => 8, :scale => 2, :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "max_quantity"
t.integer "shipping_method_id"
t.string "currency"
t.decimal "distribution_fee", :precision => 10, :scale => 2
t.string "shipping_method_name"
end
add_index "spree_line_items", ["order_id"], :name => "index_line_items_on_order_id"