Improve stock_article_selections

This commit is contained in:
Julius 2013-01-28 20:46:35 +01:00
parent 524819b86f
commit 396a47b6af
10 changed files with 104 additions and 30 deletions

View file

@ -1,8 +1,7 @@
class StockitController < ApplicationController
def index
@stock_articles = StockArticle.includes(:supplier, :article_category).
order('suppliers.name, article_categories.name, articles.name')
@stock_articles = StockArticle.elements_for_index
@stock_article_selection = StockArticleSelection.new
end

View file

@ -2,7 +2,7 @@
class StockitSelectionsController < ApplicationController
def index
@stock_article_selections = StockArticleSelection.all
@stock_article_selections = StockArticleSelection.find(:all, :order => 'created_at DESC')
end
def show
@ -14,43 +14,48 @@ class StockitSelectionsController < ApplicationController
@stock_article_selection.created_by = current_user
if @stock_article_selection.save
redirect_to(@stock_article_selection, :notice => 'Löschvorschlag für gewählte Artikel wurde erstellt.')
redirect_to(@stock_article_selection, :notice => 'Löschvorschlag für gewählte Artikel erstellt.')
else
@stock_articles = StockArticle.includes(:supplier, :article_category).
order('suppliers.name, article_categories.name, articles.name')
@stock_articles = StockArticle.elements_for_index
render 'stockit/index'
end
end
def destroy # destroy (open or finished) selection without deleting articles
def destroy # destroy selection without deleting articles
stock_article_selection = StockArticleSelection.find(params[:id])
stock_article_selection.destroy
redirect_to stock_article_selections_path, :notice => 'Löschvorschlag wurde verworfen.'
redirect_to stock_article_selections_path, :notice => 'Löschvorschlag verworfen.'
end
def articles # destroy articles, finish selection
def articles # destroy articles
stock_article_selection = StockArticleSelection.find(params[:id])
destroyed_articles_count = 0
failed_articles_count = 0
stock_article_selection.stock_articles.each do |article|
begin
article.destroy # article.delete would save some effort, but validations are important
article.destroy
destroyed_articles_count += 1
rescue => error # recover if article.destroy fails and continue with next article
failed_articles_count += 1
end
end
if destroyed_articles_count>0 # note that 1 successful article.destroy is enough to destroy selection
stock_article_selection.destroy
flash[:notice] = "#{destroyed_articles_count} gewählte Artikel sind nun gelöscht."
if destroyed_articles_count > 0
flash[:notice] = "#{destroyed_articles_count} gewählte Artikel gelöscht."
flash[:error] = "#{failed_articles_count} Artikel konnten nicht gelöscht werden." unless 0==failed_articles_count
else
flash[:error] = "Löschvorgang fehlgeschlagen. Es wurden keine Artikel gelöscht."
flash[:error] = 'Löschvorgang fehlgeschlagen. Keine Artikel gelöscht.'
end
redirect_to stock_articles_path
end
def finished # delete all finished selections
finished_selections = StockArticleSelection.all.select { |sel| sel.deletable_count + sel.nondeletable_count <= 0 }
finished_selections.each { |sel| sel.destroy }
redirect_to stock_article_selections_path, :notice => 'Alle erledigten Löschvorschläge entfernt.'
end
end