From ccf0d010b4f9c3aa7a63930fc7bf6db4ed863354 Mon Sep 17 00:00:00 2001
From: Julius <JuliusRapp@gmx.de>
Date: Mon, 25 Feb 2013 10:02:21 +0100
Subject: [PATCH] Apply AJAX for deleting a StockArticle

---
 app/controllers/stockit_controller.rb   | 7 ++++---
 app/views/stockit/_destroy_fail.js.haml | 7 +++++++
 app/views/stockit/destroy.js.haml       | 1 +
 app/views/stockit/index.html.haml       | 3 +--
 4 files changed, 13 insertions(+), 5 deletions(-)
 create mode 100644 app/views/stockit/_destroy_fail.js.haml
 create mode 100644 app/views/stockit/destroy.js.haml

diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb
index d555b4c3..36254f10 100644
--- a/app/controllers/stockit_controller.rb
+++ b/app/controllers/stockit_controller.rb
@@ -33,10 +33,11 @@ class StockitController < ApplicationController
 
   def destroy
     StockArticle.find(params[:id]).destroy
-    redirect_to stock_articles_path
+    render :layout => false,
+      :locals => { :destroyed_article_id => params[:id] }
   rescue => error
-    flash[:error] = "Ein Fehler ist aufgetreten: " + error.message
-    redirect_to stock_articles_path
+    render :partial => "destroy_fail", :layout => false,
+      :locals => { :fail_msg => "Ein Fehler ist aufgetreten: " + error.message }
   end
 
   #TODO: Fix this!!
diff --git a/app/views/stockit/_destroy_fail.js.haml b/app/views/stockit/_destroy_fail.js.haml
new file mode 100644
index 00000000..60b03d87
--- /dev/null
+++ b/app/views/stockit/_destroy_fail.js.haml
@@ -0,0 +1,7 @@
+-# please polish the following line if you know how
+var errorDiv = $('<div class="alert fade in alert-error"><a class="close" data-dismiss="alert" href="#">×</a></div>');
+
+-# next line, createTextNode called by .text(textString) helps escaping for html, but what about ' signs?
+errorDiv.text('#{fail_msg}');
+
+$('div.container-fluid').prepend(errorDiv);
diff --git a/app/views/stockit/destroy.js.haml b/app/views/stockit/destroy.js.haml
new file mode 100644
index 00000000..39602dfb
--- /dev/null
+++ b/app/views/stockit/destroy.js.haml
@@ -0,0 +1 @@
+console.log('#{destroyed_article_id}');
diff --git a/app/views/stockit/index.html.haml b/app/views/stockit/index.html.haml
index 89063bd7..b84b4f4f 100644
--- a/app/views/stockit/index.html.haml
+++ b/app/views/stockit/index.html.haml
@@ -64,8 +64,7 @@
           %td= article.article_category.name
           %td
             = link_to "Bearbeiten", edit_stock_article_path(article), class: 'btn btn-mini'
-            = link_to "Löschen", article, :method => :delete, :confirm => "Bist Du sicher?",
-              class: 'btn btn-mini btn-danger'
+            = link_to "Löschen", article, :method => :delete, :confirm => "Bist Du sicher?", class: 'btn btn-mini btn-danger', :remote => true
   .form-actions.unavailable
     = submit_tag "Artikel zum Löschen vormerken", { :class => 'unavailable btn' }
 %p