From 7b92fcb6146005de1af018e351a99bfcd924c520 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sun, 24 Jun 2012 14:56:27 +1000 Subject: [PATCH] Do not allow deselecting distributor after product added to cart at controller level --- .../spree/distributors_controller.rb | 7 +++++-- .../controllers/distributors_controller_spec.rb | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/controllers/spree/distributors_controller.rb b/app/controllers/spree/distributors_controller.rb index bf1deff485..ce35922de7 100644 --- a/app/controllers/spree/distributors_controller.rb +++ b/app/controllers/spree/distributors_controller.rb @@ -24,8 +24,11 @@ module Spree def deselect order = current_order(true) - order.distributor = nil - order.save! + + if order.line_items.empty? + order.distributor = nil + order.save! + end redirect_back_or_default(root_path) end diff --git a/spec/controllers/distributors_controller_spec.rb b/spec/controllers/distributors_controller_spec.rb index 843c7e9f43..7d97a53860 100644 --- a/spec/controllers/distributors_controller_spec.rb +++ b/spec/controllers/distributors_controller_spec.rb @@ -52,6 +52,21 @@ describe Spree::DistributorsController do o.distributor.should == d1 end - it "does not allow deselecting distributors" + it "does not allow deselecting distributors" do + # Given a distributor and an order with a product + d = create(:distributor) + p = create(:product, :distributors => [d]) + o = current_order(true) + o.add_variant(p.master, 1) + o.distributor = d + o.save! + + # When I attempt to deselect the distributor + spree_get :deselect + + # Then my distributor should remain unchanged + o.reload + o.distributor.should == d + end end end