diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 6273a32faa..d2a982438f 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -6,6 +6,11 @@ Spree::Order.class_eval do line_items.empty? end + def distributor=(distributor) + raise "You cannot change the distributor of an order with products" unless can_change_distributor? + super(distributor) + 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 dc82c9ce0d..8cf876542d 100644 --- a/app/views/spree/products/_source_sidebar.html.haml +++ b/app/views/spree/products/_source_sidebar.html.haml @@ -11,5 +11,7 @@ %li.nowrap - if order.nil? || order.can_change_distributor? = link_to distributor.name, select_distributor_path(distributor) + - else + = distributor.name - if current_distributor && order.can_change_distributor? %li.nowrap= link_to 'Leave distributor', deselect_distributors_path diff --git a/spec/controllers/distributors_controller_spec.rb b/spec/controllers/distributors_controller_spec.rb index 7d97a53860..404090836f 100644 --- a/spec/controllers/distributors_controller_spec.rb +++ b/spec/controllers/distributors_controller_spec.rb @@ -40,8 +40,8 @@ describe Spree::DistributorsController do d2 = create(:distributor) p = create(:product, :distributors => [d1]) o = current_order(true) - o.add_variant(p.master, 1) o.distributor = d1 + o.add_variant(p.master, 1) o.save! # When I attempt to select a distributor @@ -57,8 +57,8 @@ describe Spree::DistributorsController do d = create(:distributor) p = create(:product, :distributors => [d]) o = current_order(true) - o.add_variant(p.master, 1) o.distributor = d + o.add_variant(p.master, 1) o.save! # When I attempt to deselect the distributor diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index fe27b0b887..872d1d949f 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -1,11 +1,21 @@ require 'spec_helper' describe Spree::Order do - it "provides permissions for changing distributor" do + it "reveals permission 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 + + it "raises an exception if distributor is changed without permission" do + p = build(:product) + subject.add_variant(p.master, 1) + subject.can_change_distributor?.should be_false + + expect do + subject.distributor = nil + end.to raise_error "You cannot change the distributor of an order with products" + end end