From 13e921a632bc74af4a2dcf5c6279c366a4eb6467 Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Mon, 8 Apr 2013 01:00:49 +0200 Subject: [PATCH] Fixed some errors in articles#sync. Show better error messages. --- app/controllers/articles_controller.rb | 56 ++++++++++++++++++-------- app/models/order.rb | 2 +- app/views/articles/sync.html.haml | 5 ++- config/routes.rb | 1 + 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb index 820242d6..dd67dab7 100644 --- a/app/controllers/articles_controller.rb +++ b/app/controllers/articles_controller.rb @@ -74,12 +74,12 @@ class ArticlesController < ApplicationController end # Updates all article of specific supplier - # deletes all articles from params[outlisted_articles] def update_all + invalid_articles = false + begin Article.transaction do unless params[:articles].blank? - invalid_articles = false # Update other article attributes... @articles = Article.find(params[:articles].keys) @articles.each do |article| @@ -88,25 +88,18 @@ class ArticlesController < ApplicationController end end - raise "Artikel sind fehlerhaft. Bitte überprüfe Deine Eingaben." if invalid_articles - end - # delete articles - if params[:outlisted_articles] - params[:outlisted_articles].keys.each {|id| Article.find(id).mark_as_deleted } + raise ActiveRecord::Rollback if invalid_articles # Rollback all changes end end + end + + if invalid_articles + # An error has occurred, transaction has been rolled back. + flash.now.alert = 'Artikel sind fehlerhaft. Bitte überprüfen.' + render :edit_all + else # Successfully done. redirect_to supplier_articles_path(@supplier), notice: "Alle Artikel und Preise wurden aktalisiert" - - rescue => e - # An error has occurred, transaction has been rolled back. - if params[:sync] - flash[:error] = "Es trat ein Fehler beim Aktualisieren des Artikels '#{current_article.name}' auf: #{e.message}" - redirect_to(supplier_articles_path(@supplier)) - else - flash.now.alert = e.message - render :edit_all - end end end @@ -232,4 +225,33 @@ class ArticlesController < ApplicationController redirect_to supplier_articles_path(@supplier), :notice => "Der Katalog ist aktuell." end end + + # Updates, deletes articles when sync form is submitted + def update_synchronized + begin + Article.transaction do + # delete articles + if params[:outlisted_articles] + Article.find(params[:outlisted_articles].keys).each(&:mark_as_deleted) + end + + # Update articles + params[:articles].each do |id, attrs| + Article.find(id).update_attributes! attrs + end + end + + # Successfully done. + redirect_to supplier_articles_path(@supplier), notice: "Alle Artikel und Preise wurden aktalisiert" + + rescue ActiveRecord::RecordInvalid => invalid + # An error has occurred, transaction has been rolled back. + redirect_to supplier_articles_path(@supplier), + alert: "Es trat ein Fehler beim Aktualisieren des Artikels '#{invalid.record.name}' auf: #{invalid.record.errors.full_messages}" + + rescue => error + redirect_to supplier_articles_path(@supplier), + alert: "Es trat ein Fehler auf: #{error.message}" + end + end end diff --git a/app/models/order.rb b/app/models/order.rb index 6c6719ec..77c3f0b2 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -158,7 +158,7 @@ class Order < ActiveRecord::Base goa.save_results! # Delete no longer required order-history (group_order_article_quantities) and # TODO: Do we need articles, which aren't ordered? (units_to_order == 0 ?) - goa.group_order_article_quantities.clear + #goa.group_order_article_quantities.clear end end diff --git a/app/views/articles/sync.html.haml b/app/views/articles/sync.html.haml index d297c445..3879d219 100644 --- a/app/views/articles/sync.html.haml +++ b/app/views/articles/sync.html.haml @@ -1,6 +1,6 @@ - title 'Artikel mit externer Datenbank synchronisieren' -= form_tag update_all_supplier_articles_path(@supplier, :sync => "1") do += form_tag update_synchronized_supplier_articles_path(@supplier) do %h2 Auslisten ... %p - unless @outlisted_articles.empty? @@ -11,6 +11,9 @@ %li = hidden_field_tag "outlisted_articles[#{article.id}]", '1' = article.name + - if article.in_open_order + .alert + Achtung, #{article.name} wird gerade in einer laufenden Bestellung verwendet. Bitte erst Bestellung anpassen. - else %i Es müssen keine Artikel gelöscht werden. %hr/ diff --git a/config/routes.rb b/config/routes.rb index 9fefac04..cbcf2828 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -117,6 +117,7 @@ Foodsoft::Application.routes.draw do get :shared get :import post :sync + post :update_synchronized end end end