Work around order cycles not having fees / shipping methods yet - notify bugsnag and provide an undefined shipping method

This commit is contained in:
Rohan Mitchell
2013-05-27 12:59:47 +10:00
parent 796be2ee6d
commit e3a419993c
3 changed files with 27 additions and 6 deletions

View File

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

View File

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

View File

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