diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index 6430aa9ad3..488fd68c03 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -1,9 +1,7 @@ Spree::LineItem.class_eval do - attr_accessible :max_quantity + belongs_to :shipping_method - def shipping_method - self.product.shipping_method_for_distributor(self.order.distributor) - end + attr_accessible :max_quantity def itemwise_shipping_cost order = OpenStruct.new :line_items => [self] diff --git a/db/migrate/20120919013335_add_shipping_method_to_line_items.rb b/db/migrate/20120919013335_add_shipping_method_to_line_items.rb new file mode 100644 index 0000000000..ac510b07bc --- /dev/null +++ b/db/migrate/20120919013335_add_shipping_method_to_line_items.rb @@ -0,0 +1,19 @@ +class AddShippingMethodToLineItems < ActiveRecord::Migration + def up + add_column :spree_line_items, :shipping_method_id, :integer + + Spree::LineItem.all.each do |li| + begin + shipping_method = li.product.shipping_method_for_distributor(li.order.distributor) + rescue ArgumentError + shipping_method = Spree::ShippingMethod.find_by_name 'Producer Delivery' + end + + li.update_attribute(:shipping_method_id, shipping_method.id) + end + end + + def down + remove_column :spree_line_items, :shipping_method_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 577e55d47c..2a22eedd1f 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 => 20120913054660) do +ActiveRecord::Schema.define(:version => 20120919013335) do create_table "distributors", :force => true do |t| t.string "name" @@ -180,11 +180,12 @@ ActiveRecord::Schema.define(:version => 20120913054660) 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" end add_index "spree_line_items", ["order_id"], :name => "index_line_items_on_order_id"