diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index f0405baed6..e11b0c2375 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -76,6 +76,10 @@ Spree::Product.class_eval do self.class.in_order_cycle(order_cycle).include? self end + def product_distribution_for(distributor) + self.product_distributions.find_by_distributor_id(distributor) + 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 @@ -83,7 +87,7 @@ Spree::Product.class_eval do # 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) + distribution = product_distribution_for distributor unless distribution Bugsnag.notify(Exception.new "No product distribution for product #{id} at distributor #{distributor.andand.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.") diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index 8704dce6be..b6d9e5ac36 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -155,7 +155,7 @@ module Spree end end - describe 'access roles' do + describe "access roles" do before(:each) do @e1 = create(:enterprise) @e2 = create(:enterprise) @@ -185,6 +185,13 @@ module Spree end describe "finders" do + it "finds the product distribution for a particular distributor" do + distributor = create(:distributor_enterprise) + product = create(:product) + product_distribution = create(:product_distribution, product: product, distributor: distributor) + product.product_distribution_for(distributor).should == product_distribution + end + it "finds the shipping method for a particular distributor" do shipping_method = create(:shipping_method) distributor = create(:distributor_enterprise)