From 9aad44f093d221a052a2c97a02a8224474012ab8 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Tue, 13 Aug 2013 07:15:15 +1000 Subject: [PATCH] Strip shipping method mechanics out of LineItem and Order --- app/models/spree/line_item_decorator.rb | 25 ---------------- app/models/spree/order_decorator.rb | 38 +------------------------ spec/models/order_spec.rb | 13 --------- 3 files changed, 1 insertion(+), 75 deletions(-) diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb index edca66c6fb..3b7ee891b8 100644 --- a/app/models/spree/line_item_decorator.rb +++ b/app/models/spree/line_item_decorator.rb @@ -1,28 +1,3 @@ Spree::LineItem.class_eval do attr_accessible :max_quantity - - before_create :set_distribution_fee - - - def update_distribution_fee_without_callbacks!(distributor) - set_distribution_fee(distributor) - update_column(:distribution_fee, distribution_fee) - update_column(:shipping_method_name, shipping_method_name) - end - - - private - - def shipping_method(distributor=nil) - distributor ||= self.order.distributor - self.product.shipping_method_for_distributor(distributor) - end - - def set_distribution_fee(distributor=nil) - order = OpenStruct.new :line_items => [self] - sm = shipping_method(distributor) - - self.distribution_fee = sm.compute_amount(order) - self.shipping_method_name = sm.name - end end diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 7a01b408bb..415b15ecfa 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -9,13 +9,11 @@ Spree::Order.class_eval do belongs_to :distributor, :class_name => 'Enterprise' belongs_to :cart - before_validation :shipping_address_from_distributor validate :products_available_from_new_distribution, :if => lambda { distributor_id_changed? || order_cycle_id_changed? } attr_accessible :order_cycle_id, :distributor_id before_validation :shipping_address_from_distributor - before_save :update_line_item_shipping_methods - after_create :set_default_shipping_method + # -- Scopes scope :managed_by, lambda { |user| @@ -43,12 +41,6 @@ Spree::Order.class_eval do save! end - def empty! - line_items.destroy_all - adjustments.destroy_all - set_default_shipping_method - end - def set_distributor!(distributor) self.distributor = distributor self.order_cycle = nil unless self.order_cycle.andand.has_distributor? distributor @@ -86,27 +78,6 @@ Spree::Order.class_eval do private - # On creation of the order (when the first item is added to the user's cart), set the - # shipping method to the first one available and create a shipment. - # order.create_shipment! creates an adjustment for the shipping cost on the order, - # which means that the customer can see their shipping cost at every step of the - # checkout process, not just after the delivery step. - # This is based on the assumption that there's only one shipping method visible to the user, - # which is a method using the itemwise shipping calculator. - def set_default_shipping_method - self.shipping_method = itemwise_shipping_method - if self.shipping_method - self.save! - self.create_shipment! - else - raise 'No default shipping method found' - end - end - - def itemwise_shipping_method - Spree::ShippingMethod.all.find { |sm| sm.calculator.is_a? OpenFoodWeb::Calculator::Itemwise } - end - def shipping_address_from_distributor if distributor self.ship_address = distributor.address.clone @@ -119,13 +90,6 @@ Spree::Order.class_eval do end end - def update_line_item_shipping_methods - if %w(cart address delivery resumed).include? state - self.line_items.each { |li| li.update_distribution_fee_without_callbacks!(distributor) } - self.update! - end - end - def product_distribution_for(line_item) line_item.variant.product.product_distribution_for self.distributor end diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index 2cb7dc97e8..fe8051ee3c 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -1,19 +1,6 @@ require 'spec_helper' describe Spree::Order do - it "initialises a default shipping method after creation" do - shipping_method_regular = create(:shipping_method) - shipping_method_itemwise = create(:itemwise_shipping_method) - - subject.shipping_method.should be_nil - subject.adjustments.should be_empty - - subject.save! - - subject.shipping_method.should == shipping_method_itemwise - subject.adjustments.where(:label => "Shipping").should be_present - end - it "sets attributes on line items for variants" do d = create(:distributor_enterprise) p = create(:product, :distributors => [d])