From ae93d02c9cff71f503b47064c8e2558bc08ca60d Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 18 Jan 2013 14:23:42 +1100 Subject: [PATCH] OrderCycleFormApplicator applies outgoing exchange collection details --- .../order_cycle_form_applicator.rb | 17 +++++++++-------- .../order_cycle_form_applicator_spec.rb | 16 ++++++++-------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/open_food_web/order_cycle_form_applicator.rb b/lib/open_food_web/order_cycle_form_applicator.rb index 614e6168eb..66ea0775b9 100644 --- a/lib/open_food_web/order_cycle_form_applicator.rb +++ b/lib/open_food_web/order_cycle_form_applicator.rb @@ -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 diff --git a/spec/lib/open_food_web/order_cycle_form_applicator_spec.rb b/spec/lib/open_food_web/order_cycle_form_applicator_spec.rb index c7e158c16a..160ae61952 100644 --- a/spec/lib/open_food_web/order_cycle_form_applicator_spec.rb +++ b/spec/lib/open_food_web/order_cycle_form_applicator_spec.rb @@ -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]