From ae43fd133392af19f0819f945a6d42e2c0d2b5bd Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 21 Jun 2013 11:36:24 +1000 Subject: [PATCH] Set distributor and order cycle in one step, avoiding validation errors. Update spec with new error messages. --- app/models/spree/order_decorator.rb | 6 ++++++ app/models/spree/order_populator_decorator.rb | 3 +-- spec/features/consumer/add_to_cart_spec.rb | 4 ++-- spec/models/order_populator_spec.rb | 3 +-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index acbafeea50..c6fa4f78f3 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -28,6 +28,12 @@ Spree::Order.class_eval do save! end + def set_distribution!(distributor, order_cycle) + self.distributor = distributor + self.order_cycle = order_cycle + save! + end + def set_variant_attributes(variant, attributes) line_item = find_line_item_by_variant(variant) diff --git a/app/models/spree/order_populator_decorator.rb b/app/models/spree/order_populator_decorator.rb index 315a989e64..002b0a0f74 100644 --- a/app/models/spree/order_populator_decorator.rb +++ b/app/models/spree/order_populator_decorator.rb @@ -62,8 +62,7 @@ Spree::OrderPopulator.class_eval do # to be set to zero @order = Spree::Order.find @order.id - @order.set_distributor! distributor - @order.set_order_cycle! order_cycle if order_cycle + @order.set_distribution! distributor, order_cycle end def distribution_can_supply_products_in_cart(distributor, order_cycle) diff --git a/spec/features/consumer/add_to_cart_spec.rb b/spec/features/consumer/add_to_cart_spec.rb index 44d4ed162c..d36ea4e0d9 100644 --- a/spec/features/consumer/add_to_cart_spec.rb +++ b/spec/features/consumer/add_to_cart_spec.rb @@ -226,10 +226,10 @@ feature %q{ click_button 'Add To Cart' # Then I should see an error message - page.should have_content "That order cycle is not available at the distributor that you chose. Please choose another." + page.should have_content "That product is not available from the chosen distributor or order cycle." # And the product should not be in my cart - Spree::Order.last.should be_nil + Spree::Order.last.line_items.should be_empty end diff --git a/spec/models/order_populator_spec.rb b/spec/models/order_populator_spec.rb index de64db40ab..e224aad805 100644 --- a/spec/models/order_populator_spec.rb +++ b/spec/models/order_populator_spec.rb @@ -164,8 +164,7 @@ module Spree it "sets cart distributor and order cycle" do Spree::Order.should_receive(:find).with(order.id).and_return(order) - order.should_receive(:set_distributor!).with(distributor) - order.should_receive(:set_order_cycle!).with(order_cycle) + order.should_receive(:set_distribution!).with(distributor, order_cycle) op.send(:set_cart_distributor_and_order_cycle, distributor, order_cycle) end