Raise error if distributor changed illegally

This commit is contained in:
Rohan Mitchell
2012-06-24 19:23:44 +10:00
parent a41c5d4735
commit 24ad4f53fc
4 changed files with 20 additions and 3 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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