From 1bb257c41baacb4b072d63c9851a0cb5fe2a5acc Mon Sep 17 00:00:00 2001 From: wvengen Date: Fri, 13 Sep 2013 15:37:30 +0200 Subject: [PATCH 1/2] remember member order when order article is deleted + test --- .../finance/order_articles_controller.rb | 10 ++++++- spec/integration/balancing_spec.rb | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/controllers/finance/order_articles_controller.rb b/app/controllers/finance/order_articles_controller.rb index eed715eb..cc3351b0 100644 --- a/app/controllers/finance/order_articles_controller.rb +++ b/app/controllers/finance/order_articles_controller.rb @@ -42,6 +42,14 @@ class Finance::OrderArticlesController < ApplicationController def destroy @order_article = OrderArticle.find(params[:id]) - @order_article.destroy + # only destroy if there are no associated GroupOrders; if we would, the requested + # quantity and tolerance would be gone. Instead of destroying, we set all result + # quantities to zero. + if @order_article.group_order_articles.count == 0 + @order_article.destroy + else + @order_article.group_order_articles.each { |goa| goa.update_attribute(:result, 0) } + @order_article.update_results! + end end end diff --git a/spec/integration/balancing_spec.rb b/spec/integration/balancing_spec.rb index 75133131..63245849 100644 --- a/spec/integration/balancing_spec.rb +++ b/spec/integration/balancing_spec.rb @@ -50,6 +50,32 @@ describe 'settling an order', :type => :feature do end end + it 'keeps ordered quantities when article is deleted from resulting order' do + within("#order_article_#{oa.id}") do + click_link I18n.t('ui.delete') + page.driver.browser.switch_to.alert.accept + end + expect(page).to_not have_selector("#order_article_#{oa.id}") + expect(OrderArticle.exists?(oa.id)).to be_true + oa.reload + expect(oa.quantity).to eq(4) + expect(oa.tolerance).to eq(0) + expect(oa.units_to_order).to eq(0) + expect(goa1.reload.result).to eq(0) + expect(goa2.reload.result).to eq(0) + end + + it 'deletes an OrderArticle with no GroupOrderArticles' do + goa1.destroy + goa2.destroy + within("#order_article_#{oa.id}") do + click_link I18n.t('ui.delete') + page.driver.browser.switch_to.alert.accept + end + expect(page).to_not have_selector("#order_article_#{oa.id}") + expect(OrderArticle.exists?(oa.id)).to be_false + end + end end From f224735718f1ac7d25bebffd91edc40c6286d7e6 Mon Sep 17 00:00:00 2001 From: wvengen Date: Tue, 17 Sep 2013 14:19:46 +0200 Subject: [PATCH 2/2] remember what member ordered when deleted in balancing screen --- .../group_order_articles_controller.rb | 8 +++++- spec/integration/balancing_spec.rb | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/controllers/finance/group_order_articles_controller.rb b/app/controllers/finance/group_order_articles_controller.rb index 705e2b2f..3596ec40 100644 --- a/app/controllers/finance/group_order_articles_controller.rb +++ b/app/controllers/finance/group_order_articles_controller.rb @@ -65,7 +65,13 @@ class Finance::GroupOrderArticlesController < ApplicationController def destroy group_order_article = GroupOrderArticle.find(params[:id]) - group_order_article.destroy + # only destroy if quantity and tolerance was zero already, so that we don't + # lose what the user ordered, if any + if group_order_article.quantity > 0 or group_order_article.tolerance >0 + group_order_article.update_attribute(:result, 0) + else + group_order_article.destroy + end update_summaries(group_order_article) @order_article = group_order_article.order_article diff --git a/spec/integration/balancing_spec.rb b/spec/integration/balancing_spec.rb index 63245849..8e2321ac 100644 --- a/spec/integration/balancing_spec.rb +++ b/spec/integration/balancing_spec.rb @@ -76,6 +76,33 @@ describe 'settling an order', :type => :feature do expect(OrderArticle.exists?(oa.id)).to be_false end + it 'keeps ordered quantities when GroupOrderArticle is deleted from resulting order' do + click_link article.name + expect(page).to have_selector("#group_order_article_#{goa1.id}") + within("#group_order_article_#{goa1.id}") do + click_link I18n.t('ui.delete') + end + expect(page).to_not have_selector("#group_order_article_#{goa1.id}") + expect(OrderArticle.exists?(oa.id)).to be_true + expect(GroupOrderArticle.exists?(goa1.id)).to be_true + goa1.reload + expect(goa1.result).to eq(0) + expect(goa1.quantity).to eq(3) + expect(goa1.tolerance).to eq(0) + end + + it 'deletes a GroupOrderArticle with no ordered amounts' do + goa1.update_attributes({:quantity => 0, :tolerance => 0}) + click_link article.name + expect(page).to have_selector("#group_order_article_#{goa1.id}") + within("#group_order_article_#{goa1.id}") do + click_link I18n.t('ui.delete') + end + expect(page).to_not have_selector("#group_order_article_#{goa1.id}") + expect(OrderArticle.exists?(oa.id)).to be_true + expect(GroupOrderArticle.exists?(goa1.id)).to be_false + end + end end