From be1127b414c911d803742fa4adc846eabd2233c9 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 5 Aug 2013 17:11:21 +1000 Subject: [PATCH] Migrate line item shipping_method cache to distribution_fee/shipping_method_name --- ...20130805050109_update_line_item_caching.rb | 41 +++++++++++++++++++ db/schema.rb | 13 +++--- 2 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20130805050109_update_line_item_caching.rb diff --git a/db/migrate/20130805050109_update_line_item_caching.rb b/db/migrate/20130805050109_update_line_item_caching.rb new file mode 100644 index 0000000000..1663b8ab4a --- /dev/null +++ b/db/migrate/20130805050109_update_line_item_caching.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index c051264988..5d091c29bf 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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"