From e3a419993cfc59dc5583b785e448fc2e83cb403c Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Mon, 27 May 2013 12:59:47 +1000 Subject: [PATCH] Work around order cycles not having fees / shipping methods yet - notify bugsnag and provide an undefined shipping method --- app/models/spree/product_decorator.rb | 14 ++++++++++++-- spec/models/product_spec.rb | 10 ++++++---- spec/support/spree/init.rb | 9 +++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 6eeba8d147..3fbb44d342 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -61,10 +61,20 @@ Spree::Product.class_eval do self.class.in_order_cycle(order_cycle).include? self end + # This method is called on products that are distributed via order cycles, but at the time of + # writing (27-5-2013), order cycle fees were not implemented, so there's no defined result + # that this method should return. As a stopgap, we notify Bugsnag of the situation and return + # an undefined, but valid shipping method. When order cycle fees are implemented, this method + # should return the order cycle shipping method, or raise an exepction with the message, + # "This product is not available through that distributor". def shipping_method_for_distributor(distributor) distribution = self.product_distributions.find_by_distributor_id(distributor) - raise ArgumentError, "This product is not available through that distributor" unless distribution - distribution.shipping_method + + unless distribution + Bugsnag.notify(Exception.new "No product distribution for product #{id} at distributor #{distributor.id}. Perhaps this product is distributed via an order cycle? This is a warning that OrderCycle fees and shipping methods are not yet implemented, and the shipping fee charged is undefined until then.") + end + + distribution.andand.shipping_method || Spree::ShippingMethod.where("name != 'Delivery'").last end diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index 08dd2e2a70..8ea4a3510e 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -144,12 +144,14 @@ module Spree product.shipping_method_for_distributor(distributor).should == shipping_method end - it "raises an error if distributor is not found" do + it "logs an error and returns an undefined shipping method if distributor is not found" do distributor = create(:distributor_enterprise) product = create(:product) - expect do - product.shipping_method_for_distributor(distributor) - end.to raise_error "This product is not available through that distributor" + + Bugsnag.should_receive(:notify) + + product.shipping_method_for_distributor(distributor).should == + Spree::ShippingMethod.where("name != 'Delivery'").last end end diff --git a/spec/support/spree/init.rb b/spec/support/spree/init.rb index 2e88b76b4e..6458039c3e 100644 --- a/spec/support/spree/init.rb +++ b/spec/support/spree/init.rb @@ -11,6 +11,15 @@ ProductDistribution.class_eval do end end +Spree::Product.class_eval do + before_validation :init_shipping_method + + def init_shipping_method + FactoryGirl.create(:shipping_method) if Spree::ShippingMethod.where("name != 'Delivery'").empty? + end + +end + # Create a default shipping method, required when creating orders Spree::Order.class_eval do before_create :init_shipping_method