OrderCycleFormApplicator applies outgoing exchange collection details

This commit is contained in:
Rohan Mitchell
2013-01-18 14:23:42 +11:00
parent d8a7570a61
commit ae93d02c9c
2 changed files with 17 additions and 16 deletions

View File

@@ -11,9 +11,9 @@ module OpenFoodWeb
variant_ids = exchange_variant_ids(exchange)
if exchange_exists?(exchange[:enterprise_id], @order_cycle.coordinator_id)
update_exchange(exchange[:enterprise_id], @order_cycle.coordinator_id, variant_ids)
update_exchange(exchange[:enterprise_id], @order_cycle.coordinator_id, {variant_ids: variant_ids})
else
add_exchange(exchange[:enterprise_id], @order_cycle.coordinator_id, variant_ids)
add_exchange(exchange[:enterprise_id], @order_cycle.coordinator_id, {variant_ids: variant_ids})
end
end
@@ -21,9 +21,9 @@ module OpenFoodWeb
variant_ids = exchange_variant_ids(exchange)
if exchange_exists?(@order_cycle.coordinator_id, exchange[:enterprise_id])
update_exchange(@order_cycle.coordinator_id, exchange[:enterprise_id], variant_ids)
update_exchange(@order_cycle.coordinator_id, exchange[:enterprise_id], {variant_ids: variant_ids, pickup_time: exchange[:pickup_time], pickup_instructions: exchange[:pickup_instructions]})
else
add_exchange(@order_cycle.coordinator_id, exchange[:enterprise_id], variant_ids)
add_exchange(@order_cycle.coordinator_id, exchange[:enterprise_id], {variant_ids: variant_ids, pickup_time: exchange[:pickup_time], pickup_instructions: exchange[:pickup_instructions]})
end
end
@@ -39,14 +39,15 @@ module OpenFoodWeb
@order_cycle.exchanges.where(:sender_id => sender_id, :receiver_id => receiver_id).present?
end
def add_exchange(sender_id, receiver_id, variant_ids)
exchange = @order_cycle.exchanges.create! :sender_id => sender_id, :receiver_id => receiver_id, :variant_ids => variant_ids
def add_exchange(sender_id, receiver_id, attrs={})
attrs = attrs.reverse_merge(:sender_id => sender_id, :receiver_id => receiver_id)
exchange = @order_cycle.exchanges.create! attrs
@touched_exchanges << exchange
end
def update_exchange(sender_id, receiver_id, variant_ids)
def update_exchange(sender_id, receiver_id, attrs={})
exchange = @order_cycle.exchanges.where(:sender_id => sender_id, :receiver_id => receiver_id).first
exchange.update_attributes!(:variant_ids => variant_ids)
exchange.update_attributes!(attrs)
@touched_exchanges << exchange
end

View File

@@ -15,7 +15,7 @@ module OpenFoodWeb
applicator.should_receive(:exchange_variant_ids).with(incoming_exchange).and_return([1, 3])
applicator.should_receive(:exchange_exists?).with(supplier_id, coordinator_id).and_return(false)
applicator.should_receive(:add_exchange).with(supplier_id, coordinator_id, [1, 3])
applicator.should_receive(:add_exchange).with(supplier_id, coordinator_id, {:variant_ids => [1, 3]})
applicator.should_receive(:destroy_untouched_exchanges)
applicator.go!
@@ -25,7 +25,7 @@ module OpenFoodWeb
coordinator_id = 123
distributor_id = 456
outgoing_exchange = {:enterprise_id => distributor_id, :variants => {'1' => true, '2' => false, '3' => true}}
outgoing_exchange = {:enterprise_id => distributor_id, :variants => {'1' => true, '2' => false, '3' => true}, :pickup_time => 'pickup time', :pickup_instructions => 'pickup instructions'}
oc = double(:order_cycle, :coordinator_id => coordinator_id, :exchanges => [], :incoming_exchanges => [], :outgoing_exchanges => [outgoing_exchange])
@@ -33,7 +33,7 @@ module OpenFoodWeb
applicator.should_receive(:exchange_variant_ids).with(outgoing_exchange).and_return([1, 3])
applicator.should_receive(:exchange_exists?).with(coordinator_id, distributor_id).and_return(false)
applicator.should_receive(:add_exchange).with(coordinator_id, distributor_id, [1, 3])
applicator.should_receive(:add_exchange).with(coordinator_id, distributor_id, {:variant_ids => [1, 3], :pickup_time => 'pickup time', :pickup_instructions => 'pickup instructions'})
applicator.should_receive(:destroy_untouched_exchanges)
applicator.go!
@@ -55,7 +55,7 @@ module OpenFoodWeb
applicator.should_receive(:exchange_variant_ids).with(incoming_exchange).and_return([1, 3])
applicator.should_receive(:exchange_exists?).with(supplier_id, coordinator_id).and_return(true)
applicator.should_receive(:update_exchange).with(supplier_id, coordinator_id, [1, 3])
applicator.should_receive(:update_exchange).with(supplier_id, coordinator_id, {:variant_ids => [1, 3]})
applicator.should_receive(:destroy_untouched_exchanges)
applicator.go!
@@ -65,7 +65,7 @@ module OpenFoodWeb
coordinator_id = 123
distributor_id = 456
outgoing_exchange = {:enterprise_id => distributor_id, :variants => {'1' => true, '2' => false, '3' => true}}
outgoing_exchange = {:enterprise_id => distributor_id, :variants => {'1' => true, '2' => false, '3' => true}, :pickup_time => 'pickup time', :pickup_instructions => 'pickup instructions'}
oc = double(:order_cycle,
:coordinator_id => coordinator_id,
@@ -77,7 +77,7 @@ module OpenFoodWeb
applicator.should_receive(:exchange_variant_ids).with(outgoing_exchange).and_return([1, 3])
applicator.should_receive(:exchange_exists?).with(coordinator_id, distributor_id).and_return(true)
applicator.should_receive(:update_exchange).with(coordinator_id, distributor_id, [1, 3])
applicator.should_receive(:update_exchange).with(coordinator_id, distributor_id, {:variant_ids => [1, 3], :pickup_time => 'pickup time', :pickup_instructions => 'pickup instructions'})
applicator.should_receive(:destroy_untouched_exchanges)
applicator.go!
@@ -135,7 +135,7 @@ module OpenFoodWeb
variant2 = FactoryGirl.create(:variant)
applicator.send(:touched_exchanges=, [])
applicator.send(:add_exchange, sender.id, receiver.id, [variant1.id, variant2.id])
applicator.send(:add_exchange, sender.id, receiver.id, {:variant_ids => [variant1.id, variant2.id]})
exchange = Exchange.last
exchange.sender.should == sender
@@ -157,7 +157,7 @@ module OpenFoodWeb
exchange = FactoryGirl.create(:exchange, order_cycle: oc, sender: sender, receiver: receiver, variant_ids: [variant1, variant2])
applicator.send(:touched_exchanges=, [])
applicator.send(:update_exchange, sender.id, receiver.id, [variant1.id, variant3.id])
applicator.send(:update_exchange, sender.id, receiver.id, {:variant_ids => [variant1.id, variant3.id]})
exchange.reload
exchange.variant_ids.should == [variant1.id, variant3.id]