diff --git a/app/models/exchange.rb b/app/models/exchange.rb index 080abe27cd..362edaf102 100644 --- a/app/models/exchange.rb +++ b/app/models/exchange.rb @@ -12,4 +12,6 @@ class Exchange < ActiveRecord::Base validates_presence_of :order_cycle, :sender, :receiver validates_uniqueness_of :sender_id, :scope => [:order_cycle_id, :receiver_id] + + accepts_nested_attributes_for :variants end diff --git a/lib/open_food_web/order_cycle_form_applicator.rb b/lib/open_food_web/order_cycle_form_applicator.rb index 6d4f0151c3..d6dbb7c49a 100644 --- a/lib/open_food_web/order_cycle_form_applicator.rb +++ b/lib/open_food_web/order_cycle_form_applicator.rb @@ -8,10 +8,12 @@ module OpenFoodWeb @touched_exchanges = [] @order_cycle.incoming_exchanges.each do |exchange| + 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) + update_exchange(exchange[:enterprise_id], @order_cycle.coordinator_id, variant_ids) else - add_exchange(exchange[:enterprise_id], @order_cycle.coordinator_id) + add_exchange(exchange[:enterprise_id], @order_cycle.coordinator_id, variant_ids) end end @@ -22,15 +24,15 @@ module OpenFoodWeb @order_cycle.exchanges.where(:sender_id => sender_id, :receiver_id => receiver_id).present? end - def add_exchange(sender_id, receiver_id) - exchange = @order_cycle.exchanges.create! :sender_id => sender_id, :receiver_id => receiver_id + 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 @touched_exchanges << exchange end - def update_exchange(sender_id, receiver_id) - # NOOP - when we're setting data on the exchange, we can do so here - + def update_exchange(sender_id, receiver_id, variant_ids) exchange = @order_cycle.exchanges.where(:sender_id => sender_id, :receiver_id => receiver_id).first + exchange.update_attributes!(:variant_ids => variant_ids) + @touched_exchanges << exchange end @@ -42,5 +44,11 @@ module OpenFoodWeb @order_cycle.exchanges - @touched_exchanges end + + private + + def exchange_variant_ids(exchange) + exchange[:exchange_variants].select { |k, v| v }.keys.map { |k| k.to_i } + end end 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 3b9edb00f6..85e1caf1a6 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 @@ -6,12 +6,15 @@ module OpenFoodWeb coordinator_id = 123 supplier_id = 456 - oc = double(:order_cycle, :coordinator_id => coordinator_id, :exchanges => [], :incoming_exchanges => [{:enterprise_id => supplier_id}]) + incoming_exchange = {:enterprise_id => supplier_id, :exchange_variants => {'1' => true, '2' => false, '3' => true}} + + oc = double(:order_cycle, :coordinator_id => coordinator_id, :exchanges => [], :incoming_exchanges => [incoming_exchange]) applicator = OrderCycleFormApplicator.new(oc) + applicator.should_receive(:exchange_variant_ids).with(incoming_exchange).and_return([1, 3]) applicator.should_receive(:exchange_exists?).and_return(false) - applicator.should_receive(:add_exchange).with(supplier_id, coordinator_id) + applicator.should_receive(:add_exchange).with(supplier_id, coordinator_id, [1, 3]) applicator.should_receive(:destroy_untouched_exchanges) applicator.go! @@ -21,15 +24,18 @@ module OpenFoodWeb coordinator_id = 123 supplier_id = 456 + incoming_exchange = {:enterprise_id => supplier_id, :exchange_variants => {'1' => true, '2' => false, '3' => true}} + oc = double(:order_cycle, :coordinator_id => coordinator_id, :exchanges => [double(:exchange, :sender_id => supplier_id, :receiver_id => coordinator_id)], - :incoming_exchanges => [{:enterprise_id => supplier_id}]) + :incoming_exchanges => [incoming_exchange]) applicator = OrderCycleFormApplicator.new(oc) + applicator.should_receive(:exchange_variant_ids).with(incoming_exchange).and_return([1, 3]) applicator.should_receive(:exchange_exists?).and_return(true) - applicator.should_receive(:update_exchange).with(supplier_id, coordinator_id) + applicator.should_receive(:update_exchange).with(supplier_id, coordinator_id, [1, 3]) applicator.should_receive(:destroy_untouched_exchanges) applicator.go! @@ -52,5 +58,11 @@ module OpenFoodWeb applicator.go! applicator.untouched_exchanges.should == [exchange] end + + it "converts exchange variant ids hash to an array of ids" do + applicator = OrderCycleFormApplicator.new(nil) + + applicator.send(:exchange_variant_ids, {:enterprise_id => 123, :exchange_variants => {'1' => true, '2' => false, '3' => true}}).should == [1, 3] + end end end diff --git a/spec/requests/admin/order_cycles_spec.rb b/spec/requests/admin/order_cycles_spec.rb index 74bc22db46..c6356b9e5a 100644 --- a/spec/requests/admin/order_cycles_spec.rb +++ b/spec/requests/admin/order_cycles_spec.rb @@ -31,9 +31,12 @@ feature %q{ end scenario "creating an order cycle" do - # Given a coordinating enterprise and a supplying enterprise + # Given a coordinating enterprise and a supplying enterprise with some products with variants create(:enterprise, name: 'My coordinator') - create(:supplier_enterprise, name: 'My supplier') + supplier = create(:supplier_enterprise, name: 'My supplier') + product = create(:product, supplier: supplier) + create(:variant, product: product) + create(:variant, product: product) # When I go to the new order cycle page login_to_admin_section @@ -46,9 +49,14 @@ feature %q{ fill_in 'order_cycle_orders_close_at', with: '2012-11-13 17:00:00' select 'My coordinator', from: 'order_cycle_coordinator_id' - # And I add a supplier and click Create + # And I add a supplier and some products select 'My supplier', from: 'new_supplier_id' click_button 'Add supplier' + click_button 'Products' + check 'order_cycle_exchange_0_exchange_variants_1' + check 'order_cycle_exchange_0_exchange_variants_3' + + # And I click Create click_button 'Create' # Then my order cycle should have been created @@ -61,6 +69,9 @@ feature %q{ page.should have_content 'My coordinator' page.should have_selector 'td.suppliers', text: 'My supplier' + + # And it should have some variants selected + OrderCycle.last.exchanges.first.variants.count.should == 2 end scenario "updating many order cycle opening/closing times at once" do