From 3aa44c3e9a579e16c81a672ec5d67802c149e9dc Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 1 Feb 2013 17:06:29 +1100 Subject: [PATCH] Setting a remote distributor clears the order cycle, and vice versa --- .../spree/orders_controller_decorator.rb | 6 +- app/models/order_cycle.rb | 5 ++ app/models/spree/order_decorator.rb | 7 ++ spec/features/consumer/order_cycles_spec.rb | 32 ++++++++- spec/models/order_cycle_spec.rb | 10 +++ spec/models/order_spec.rb | 70 +++++++++++++++++++ 6 files changed, 124 insertions(+), 6 deletions(-) diff --git a/app/controllers/spree/orders_controller_decorator.rb b/app/controllers/spree/orders_controller_decorator.rb index 92b2328854..c5c8ffb38f 100644 --- a/app/controllers/spree/orders_controller_decorator.rb +++ b/app/controllers/spree/orders_controller_decorator.rb @@ -11,16 +11,14 @@ Spree::OrdersController.class_eval do if params[:commit] == 'Choose Hub' distributor = Enterprise.is_distributor.find params[:order][:distributor_id] - @order.distributor = distributor - @order.save! + @order.set_distributor! distributor flash[:notice] = 'Your hub has been selected.' redirect_to request.referer elsif params[:commit] == 'Choose Order Cycle' order_cycle = OrderCycle.active.find params[:order][:order_cycle_id] - @order.order_cycle = order_cycle - @order.save! + @order.set_order_cycle! order_cycle flash[:notice] = 'Your order cycle has been selected.' redirect_to request.referer diff --git a/app/models/order_cycle.rb b/app/models/order_cycle.rb index 8c753d97db..ec0d33f740 100644 --- a/app/models/order_cycle.rb +++ b/app/models/order_cycle.rb @@ -29,4 +29,9 @@ class OrderCycle < ActiveRecord::Base self.variants.map(&:product).uniq end + def has_distributor?(distributor) + self.distributors.include? distributor + end + + end diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 0861ae0d94..45123d08c7 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -16,8 +16,15 @@ Spree::Order.class_eval do errors.add(:distributor_id, "cannot supply the products in your cart") unless DistributorChangeValidator.new(self).can_change_to_distributor?(distributor) end + def set_order_cycle!(order_cycle) + self.order_cycle = order_cycle + self.distributor = nil unless self.order_cycle.has_distributor? distributor + save! + end + def set_distributor!(distributor) self.distributor = distributor + self.order_cycle = nil unless self.order_cycle.andand.has_distributor? distributor save! end diff --git a/spec/features/consumer/order_cycles_spec.rb b/spec/features/consumer/order_cycles_spec.rb index e291b87eff..82cd020d64 100644 --- a/spec/features/consumer/order_cycles_spec.rb +++ b/spec/features/consumer/order_cycles_spec.rb @@ -95,9 +95,37 @@ feature %q{ end end + scenario "selecing a remote order cycle clears the distributor" do + # When I go to the products listing page + visit spree.products_path - scenario "selecing an invalid distributor clears the order cycle" - scenario "selecing an invalid order cycle clears the distributor" + # And I choose a distributor + select @d1.name, from: 'order_distributor_id' + click_button 'Choose Hub' + + # And I choose a remote order cycle + choose @oc2.name + click_button 'Choose Order Cycle' + + # Then my distributor should be cleared + page.should_not have_selector "option[value='#{@d1.id}'][selected='selected']" + end + + scenario "selecing a remote distributor clears the order cycle" do + # When I go to the products listing page + visit spree.products_path + + # And I choose an order cycle + choose @oc1.name + click_button 'Choose Order Cycle' + + # And I choose a remote distributor + select @d2.name, from: 'order_distributor_id' + click_button 'Choose Hub' + + # Then my order cycle should be cleared + page.should_not have_selector "input[value='#{@oc1.id}'][checked='checked']" + end end # scenario "making an order cycle or distributor choice filters the remaining choices to valid options", js: true do diff --git a/spec/models/order_cycle_spec.rb b/spec/models/order_cycle_spec.rb index c7987799a2..9420869104 100644 --- a/spec/models/order_cycle_spec.rb +++ b/spec/models/order_cycle_spec.rb @@ -62,6 +62,16 @@ describe OrderCycle do oc.distributors.sort.should == [e1.receiver, e2.receiver].sort end + it "checks for existance of distributors" do + oc = create(:simple_order_cycle) + d1 = create(:distributor_enterprise) + d2 = create(:distributor_enterprise) + create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: d1) + + oc.should have_distributor(d1) + oc.should_not have_distributor(d2) + end + describe "product exchanges" do before(:each) do @oc = create(:simple_order_cycle) diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index dcd7f360aa..915061d4ea 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -28,6 +28,76 @@ describe Spree::Order do li.max_quantity.should == 3 end + describe "setting the distributor" do + before(:each) do + create(:itemwise_shipping_method) + end + + it "sets the distributor when no order cycle is set" do + d = create(:distributor_enterprise) + subject.set_distributor! d + subject.distributor.should == d + end + + it "keeps the order cycle when it is available at the new distributor" do + d = create(:distributor_enterprise) + oc = create(:simple_order_cycle) + create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: d) + + subject.order_cycle = oc + subject.set_distributor! d + + subject.distributor.should == d + subject.order_cycle.should == oc + end + + it "clears the order cycle if it is not available at that distributor" do + d = create(:distributor_enterprise) + oc = create(:simple_order_cycle) + + subject.order_cycle = oc + subject.set_distributor! d + + subject.distributor.should == d + subject.order_cycle.should be_nil + end + end + + describe "setting the order cycle" do + before(:each) do + create(:itemwise_shipping_method) + end + + it "sets the order cycle when no distributor is set" do + oc = create(:simple_order_cycle) + subject.set_order_cycle! oc + subject.order_cycle.should == oc + end + + it "keeps the distributor when it is available in the new order cycle" do + oc = create(:simple_order_cycle) + d = create(:distributor_enterprise) + create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: d) + + subject.distributor = d + subject.set_order_cycle! oc + + subject.order_cycle.should == oc + subject.distributor.should == d + end + + it "clears the order cycle if it is not available at that distributor" do + oc = create(:simple_order_cycle) + d = create(:distributor_enterprise) + + subject.distributor = d + subject.set_order_cycle! oc + + subject.order_cycle.should == oc + subject.distributor.should be_nil + end + end + context "validating distributor changes" do it "checks that a distributor is available when changing" do order_enterprise = FactoryGirl.create(:enterprise, id: 1, :name => "Order Enterprise")