From 5e6c4de34b17fd79326ffadc27428b1ca3e39acd Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Sat, 23 Jun 2012 12:33:38 +1000 Subject: [PATCH] Leave a distributor --- app/controllers/spree/distributors_controller.rb | 8 ++++++++ app/views/spree/products/_source_sidebar.html.haml | 2 ++ config/routes.rb | 1 + spec/requests/consumer/distributors_spec.rb | 13 ++++++++++++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/app/controllers/spree/distributors_controller.rb b/app/controllers/spree/distributors_controller.rb index d3a3375ebf..f0e195b88b 100644 --- a/app/controllers/spree/distributors_controller.rb +++ b/app/controllers/spree/distributors_controller.rb @@ -18,5 +18,13 @@ module Spree redirect_back_or_default(root_path) end + + def deselect + order = current_order(true) + order.distributor = nil + order.save! + + redirect_back_or_default(root_path) + end end end diff --git a/app/views/spree/products/_source_sidebar.html.haml b/app/views/spree/products/_source_sidebar.html.haml index 19a9516c18..ff8e82c9ab 100644 --- a/app/views/spree/products/_source_sidebar.html.haml +++ b/app/views/spree/products/_source_sidebar.html.haml @@ -8,3 +8,5 @@ %ul.filter_choices - @distributors.each do |distributor| %li.nowrap= link_to distributor.name, select_distributor_path(distributor) + - if current_distributor + %li.nowrap= link_to 'Leave distributor', deselect_distributors_path diff --git a/config/routes.rb b/config/routes.rb index 93c3e9d19d..60cec94135 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,7 @@ Spree::Core::Engine.routes.prepend do resources :suppliers resources :distributors do get :select, :on => :member + get :deselect, :on => :collection end namespace :admin do diff --git a/spec/requests/consumer/distributors_spec.rb b/spec/requests/consumer/distributors_spec.rb index 4540dc2f2f..24e5c44c3d 100644 --- a/spec/requests/consumer/distributors_spec.rb +++ b/spec/requests/consumer/distributors_spec.rb @@ -39,7 +39,18 @@ feature %q{ it "splits the product listing by local/remote distributor" - it "allows the user to leave the distributor" + it "allows the user to leave the distributor" do + # Given a distributor + d = create(:distributor, :name => 'Melb Uni Co-op') + + # When I select the distributor and then leave it + visit spree.root_path + click_link d.name + click_link 'Leave distributor' + + # Then I should have left the distributor + page.should_not have_selector '#current-distributor', :text => 'You are shopping at Melb Uni Co-op' + end context "viewing a product" do it "provides a choice of distributor when adding to cart" # Test product at remote distributor