Setting a remote distributor clears the order cycle, and vice versa

This commit is contained in:
Rohan Mitchell
2013-02-01 17:06:29 +11:00
parent e1b2490259
commit 3aa44c3e9a
6 changed files with 124 additions and 6 deletions

View File

@@ -11,16 +11,14 @@ Spree::OrdersController.class_eval do
if params[:commit] == 'Choose Hub'
distributor = Enterprise.is_distributor.find params[:order][:distributor_id]
@order.distributor = distributor
@order.save!
@order.set_distributor! distributor
flash[:notice] = 'Your hub has been selected.'
redirect_to request.referer
elsif params[:commit] == 'Choose Order Cycle'
order_cycle = OrderCycle.active.find params[:order][:order_cycle_id]
@order.order_cycle = order_cycle
@order.save!
@order.set_order_cycle! order_cycle
flash[:notice] = 'Your order cycle has been selected.'
redirect_to request.referer

View File

@@ -29,4 +29,9 @@ class OrderCycle < ActiveRecord::Base
self.variants.map(&:product).uniq
end
def has_distributor?(distributor)
self.distributors.include? distributor
end
end

View File

@@ -16,8 +16,15 @@ Spree::Order.class_eval do
errors.add(:distributor_id, "cannot supply the products in your cart") unless DistributorChangeValidator.new(self).can_change_to_distributor?(distributor)
end
def set_order_cycle!(order_cycle)
self.order_cycle = order_cycle
self.distributor = nil unless self.order_cycle.has_distributor? distributor
save!
end
def set_distributor!(distributor)
self.distributor = distributor
self.order_cycle = nil unless self.order_cycle.andand.has_distributor? distributor
save!
end

View File

@@ -95,9 +95,37 @@ feature %q{
end
end
scenario "selecing a remote order cycle clears the distributor" do
# When I go to the products listing page
visit spree.products_path
scenario "selecing an invalid distributor clears the order cycle"
scenario "selecing an invalid order cycle clears the distributor"
# And I choose a distributor
select @d1.name, from: 'order_distributor_id'
click_button 'Choose Hub'
# And I choose a remote order cycle
choose @oc2.name
click_button 'Choose Order Cycle'
# Then my distributor should be cleared
page.should_not have_selector "option[value='#{@d1.id}'][selected='selected']"
end
scenario "selecing a remote distributor clears the order cycle" do
# When I go to the products listing page
visit spree.products_path
# And I choose an order cycle
choose @oc1.name
click_button 'Choose Order Cycle'
# And I choose a remote distributor
select @d2.name, from: 'order_distributor_id'
click_button 'Choose Hub'
# Then my order cycle should be cleared
page.should_not have_selector "input[value='#{@oc1.id}'][checked='checked']"
end
end
# scenario "making an order cycle or distributor choice filters the remaining choices to valid options", js: true do

View File

@@ -62,6 +62,16 @@ describe OrderCycle do
oc.distributors.sort.should == [e1.receiver, e2.receiver].sort
end
it "checks for existance of distributors" do
oc = create(:simple_order_cycle)
d1 = create(:distributor_enterprise)
d2 = create(:distributor_enterprise)
create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: d1)
oc.should have_distributor(d1)
oc.should_not have_distributor(d2)
end
describe "product exchanges" do
before(:each) do
@oc = create(:simple_order_cycle)

View File

@@ -28,6 +28,76 @@ describe Spree::Order do
li.max_quantity.should == 3
end
describe "setting the distributor" do
before(:each) do
create(:itemwise_shipping_method)
end
it "sets the distributor when no order cycle is set" do
d = create(:distributor_enterprise)
subject.set_distributor! d
subject.distributor.should == d
end
it "keeps the order cycle when it is available at the new distributor" do
d = create(:distributor_enterprise)
oc = create(:simple_order_cycle)
create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: d)
subject.order_cycle = oc
subject.set_distributor! d
subject.distributor.should == d
subject.order_cycle.should == oc
end
it "clears the order cycle if it is not available at that distributor" do
d = create(:distributor_enterprise)
oc = create(:simple_order_cycle)
subject.order_cycle = oc
subject.set_distributor! d
subject.distributor.should == d
subject.order_cycle.should be_nil
end
end
describe "setting the order cycle" do
before(:each) do
create(:itemwise_shipping_method)
end
it "sets the order cycle when no distributor is set" do
oc = create(:simple_order_cycle)
subject.set_order_cycle! oc
subject.order_cycle.should == oc
end
it "keeps the distributor when it is available in the new order cycle" do
oc = create(:simple_order_cycle)
d = create(:distributor_enterprise)
create(:exchange, order_cycle: oc, sender: oc.coordinator, receiver: d)
subject.distributor = d
subject.set_order_cycle! oc
subject.order_cycle.should == oc
subject.distributor.should == d
end
it "clears the order cycle if it is not available at that distributor" do
oc = create(:simple_order_cycle)
d = create(:distributor_enterprise)
subject.distributor = d
subject.set_order_cycle! oc
subject.order_cycle.should == oc
subject.distributor.should be_nil
end
end
context "validating distributor changes" do
it "checks that a distributor is available when changing" do
order_enterprise = FactoryGirl.create(:enterprise, id: 1, :name => "Order Enterprise")