diff --git a/app/controllers/spree/distributors_controller.rb b/app/controllers/spree/distributors_controller.rb index ce35922de7..bee2dc444d 100644 --- a/app/controllers/spree/distributors_controller.rb +++ b/app/controllers/spree/distributors_controller.rb @@ -14,7 +14,7 @@ module Spree order = current_order(true) - if order.line_items.empty? + if order.can_change_distributor? order.distributor = distributor order.save! end @@ -25,7 +25,7 @@ module Spree def deselect order = current_order(true) - if order.line_items.empty? + if order.can_change_distributor? order.distributor = nil order.save! end diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 8818fe5465..6273a32faa 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -1,6 +1,11 @@ Spree::Order.class_eval do belongs_to :distributor + def can_change_distributor? + # Distributor may not be changed once an item has been added to the cart/order + line_items.empty? + end + # before_validation :shipping_address_from_distributor private diff --git a/app/views/spree/products/_source_sidebar.html.haml b/app/views/spree/products/_source_sidebar.html.haml index ff8e82c9ab..dc82c9ce0d 100644 --- a/app/views/spree/products/_source_sidebar.html.haml +++ b/app/views/spree/products/_source_sidebar.html.haml @@ -6,7 +6,10 @@ %h6.filter_name Shop by Distributor %ul.filter_choices + - order = current_order(false) - @distributors.each do |distributor| - %li.nowrap= link_to distributor.name, select_distributor_path(distributor) - - if current_distributor + %li.nowrap + - if order.nil? || order.can_change_distributor? + = link_to distributor.name, select_distributor_path(distributor) + - if current_distributor && order.can_change_distributor? %li.nowrap= link_to 'Leave distributor', deselect_distributors_path diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb new file mode 100644 index 0000000000..fe27b0b887 --- /dev/null +++ b/spec/models/order_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Spree::Order do + it "provides permissions for changing distributor" do + p = build(:product) + + subject.can_change_distributor?.should be_true + subject.add_variant(p.master, 1) + subject.can_change_distributor?.should be_false + end +end diff --git a/spec/requests/consumer/add_to_cart_spec.rb b/spec/requests/consumer/add_to_cart_spec.rb index 2155ba45d5..98fd5f8323 100644 --- a/spec/requests/consumer/add_to_cart_spec.rb +++ b/spec/requests/consumer/add_to_cart_spec.rb @@ -46,6 +46,7 @@ feature %q{ visit spree.root_path page.should_not have_selector 'a', :text => d1.name page.should_not have_selector 'a', :text => d2.name + page.should_not have_selector 'a', :text => 'Leave distributor' end context "adding a subsequent product to the cart" do