diff --git a/lib/open_food_web/order_cycle_form_applicator.rb b/lib/open_food_web/order_cycle_form_applicator.rb index d6dbb7c49a..5b50910df0 100644 --- a/lib/open_food_web/order_cycle_form_applicator.rb +++ b/lib/open_food_web/order_cycle_form_applicator.rb @@ -20,6 +20,11 @@ module OpenFoodWeb destroy_untouched_exchanges end + + private + + attr_accessor :touched_exchanges + def exchange_exists?(sender_id, receiver_id) @order_cycle.exchanges.where(:sender_id => sender_id, :receiver_id => receiver_id).present? end @@ -45,8 +50,6 @@ module OpenFoodWeb end - private - def exchange_variant_ids(exchange) exchange[:exchange_variants].select { |k, v| v }.keys.map { |k| k.to_i } 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 d7a123c862..f73c693a84 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 @@ -12,14 +12,31 @@ module OpenFoodWeb exchange = FactoryGirl.create(:exchange, order_cycle: oc) applicator = OrderCycleFormApplicator.new(oc) - applicator.exchange_exists?(exchange.sender_id, exchange.receiver_id).should be_true - applicator.exchange_exists?(exchange.receiver_id, exchange.sender_id).should be_false - applicator.exchange_exists?(exchange.sender_id, 999).should be_false - applicator.exchange_exists?(999, exchange.receiver_id).should be_false - applicator.exchange_exists?(999, 888).should be_false + applicator.send(:exchange_exists?, exchange.sender_id, exchange.receiver_id).should be_true + applicator.send(:exchange_exists?, exchange.receiver_id, exchange.sender_id).should be_false + applicator.send(:exchange_exists?, exchange.sender_id, 999).should be_false + applicator.send(:exchange_exists?, 999, exchange.receiver_id).should be_false + applicator.send(:exchange_exists?, 999, 888).should be_false end - it "adds exchanges" + it "adds exchanges" do + oc = FactoryGirl.create(:simple_order_cycle) + applicator = OrderCycleFormApplicator.new(oc) + sender = FactoryGirl.create(:enterprise) + receiver = FactoryGirl.create(:enterprise) + variant1 = FactoryGirl.create(:variant) + variant2 = FactoryGirl.create(:variant) + + applicator.send(:touched_exchanges=, []) + applicator.send(:add_exchange, sender.id, receiver.id, [variant1.id, variant2.id]) + + exchange = Exchange.last + exchange.sender.should == sender + exchange.receiver.should == receiver + exchange.variants.sort.should == [variant1, variant2].sort + + applicator.send(:touched_exchanges).should == [exchange] + end it "updates exchanges" end @@ -79,7 +96,7 @@ module OpenFoodWeb applicator.should_receive(:destroy_untouched_exchanges) applicator.go! - applicator.untouched_exchanges.should == [exchange] + applicator.send(:untouched_exchanges).should == [exchange] end it "converts exchange variant ids hash to an array of ids" do