Replace shipping method lookup with relation

This commit is contained in:
Rob H
2012-09-19 12:06:43 +10:00
parent 7492522066
commit fe3743af16
3 changed files with 27 additions and 9 deletions

View File

@@ -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]

View File

@@ -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

View File

@@ -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"