From 82178161fab7511322ad7b90dd262b9b791d10fc Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sun, 24 Feb 2013 23:26:16 +0100 Subject: [PATCH 1/6] Show usefull error message for uniqueness_of_name with acts_as_paranoid. Fixed #88 --- app/models/group.rb | 2 +- app/models/ordergroup.rb | 12 +++++++++++- app/models/supplier.rb | 18 ++++++++++++++---- app/models/workgroup.rb | 1 + config/locales/de.yml | 1 + 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/models/group.rb b/app/models/group.rb index c1976830..68cb015c 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -4,7 +4,7 @@ class Group < ActiveRecord::Base has_many :memberships, :dependent => :destroy has_many :users, :through => :memberships - validates :name, :presence => true, :length => {:in => 1..25}, :uniqueness => true + validates :name, :presence => true, :length => {:in => 1..25} attr_reader :user_tokens diff --git a/app/models/ordergroup.rb b/app/models/ordergroup.rb index 3d863834..dbfe3171 100644 --- a/app/models/ordergroup.rb +++ b/app/models/ordergroup.rb @@ -16,7 +16,7 @@ class Ordergroup < Group has_many :orders, :through => :group_orders validates_numericality_of :account_balance, :message => 'ist keine gültige Zahl' - validate :uniqueness_of_members + validate :uniqueness_of_name, :uniqueness_of_members after_create :update_stats! @@ -106,6 +106,16 @@ class Ordergroup < Group errors.add :user_tokens, "#{user.nick} ist schon in einer anderen Bestellgruppe" if user.groups.where(:type => 'Ordergroup').size > 1 end end + + # Make sure, the name is uniq, add usefull message if uniq group is already deleted + def uniqueness_of_name + id = new_record? ? '' : self.id + group = Ordergroup.with_deleted.where('groups.id != ? AND groups.name = ?', id, name).first + if group.present? + message = group.deleted? ? :taken_with_deleted : :taken + errors.add :name, message + end + end end diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 05dbeb6c..384c5973 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -12,15 +12,13 @@ class Supplier < ActiveRecord::Base attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number, :delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity - validates :name, :presence => true, :length => { :in => 4..30 }, :uniqueness => true + validates :name, :presence => true, :length => { :in => 4..30 } validates :phone, :presence => true, :length => { :in => 8..20 } validates :address, :presence => true, :length => { :in => 8..50 } validates_length_of :order_howto, :note, maximum: 250 -# validates_length_of :name, :in => 4..30 -# validates_uniqueness_of :name - validates_length_of :phone, :in => 8..20 validates_length_of :address, :in => 8..50 + validate :uniqueness_of_name # sync all articles with the external database # returns an array with articles(and prices), which should be updated (to use in a form) @@ -66,5 +64,17 @@ class Supplier < ActiveRecord::Base end return [updated_articles, outlisted_articles] end + + protected + + # Make sure, the name is uniq, add usefull message if uniq group is already deleted + def uniqueness_of_name + id = new_record? ? '' : self.id + supplier = Supplier.with_deleted.where('suppliers.id != ? AND suppliers.name = ?', id, name).first + if supplier.present? + message = supplier.deleted? ? :taken_with_deleted : :taken + errors.add :name, message + end + end end diff --git a/app/models/workgroup.rb b/app/models/workgroup.rb index 5b201932..20448839 100644 --- a/app/models/workgroup.rb +++ b/app/models/workgroup.rb @@ -5,6 +5,7 @@ class Workgroup < Group # returns all non-finished tasks has_many :open_tasks, :class_name => 'Task', :conditions => ['done = ?', false], :order => 'due_date ASC' + validates_uniqueness_of :name validates_presence_of :task_name, :weekday, :task_required_users, :next_weekly_tasks_number, :if => :weekly_task validates_numericality_of :next_weekly_tasks_number, :greater_than => 0, :less_than => 21, :only_integer => true, diff --git a/config/locales/de.yml b/config/locales/de.yml index 213c3c97..6f6d108a 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -143,6 +143,7 @@ de: odd: muss ungerade sein record_invalid: ! 'Gültigkeitsprüfung ist fehlgeschlagen: %{errors}' taken: ist bereits vergeben + taken_with_deleted: ist bereits vergeben (eine gelöschte Gruppe) too_long: ist zu lang (nicht mehr als %{count} Zeichen) too_short: ist zu kurz (nicht weniger als %{count} Zeichen) wrong_length: hat die falsche Länge (muss genau %{count} Zeichen haben) From 6685012a3e1ea8cdb8291dab669893e99cce9d4a Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sun, 24 Feb 2013 23:54:59 +0100 Subject: [PATCH 2/6] Fixed bad syntax (from already dropped meta_where gem). Thanks to wvengen, closed #91 --- app/models/workgroup.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/workgroup.rb b/app/models/workgroup.rb index 20448839..5a5855e7 100644 --- a/app/models/workgroup.rb +++ b/app/models/workgroup.rb @@ -62,7 +62,7 @@ class Workgroup < Group # add validation check on update # Return an error if this is the last group with admin role and role_admin should set to false def last_admin_on_earth - if !role_admin && Workgroup.where(:role_admin => true, :id.ne => id).empty? + if !role_admin && !Workgroup.where('role_admin = ? AND id != ?', true, id).exists? errors.add(:role_admin, "Der letzten Gruppe mit Admin-Rechten darf die Admin-Rolle nicht entzogen werden") end end From ccf0d010b4f9c3aa7a63930fc7bf6db4ed863354 Mon Sep 17 00:00:00 2001 From: Julius Date: Mon, 25 Feb 2013 10:02:21 +0100 Subject: [PATCH 3/6] 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 = $(''); + +-# 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 From 8f15cfb446749308f7eae77af8b6e7a937e4ad94 Mon Sep 17 00:00:00 2001 From: Julius Date: Mon, 25 Feb 2013 10:13:44 +0100 Subject: [PATCH 4/6] Revert 396a47b6afa5478c96dd8990a80a5437b784c0da --- app/controllers/stockit_controller.rb | 3 +- .../stockit_selections_controller.rb | 29 ++++++++----------- .../stock_article_selections_helper.rb | 16 ---------- app/models/stock_article.rb | 5 ---- app/models/stock_article_selection.rb | 12 -------- app/views/stockit/index.html.haml | 7 ++--- .../stockit_selections/_overview.html.haml | 13 ++------- app/views/stockit_selections/index.html.haml | 28 ++++-------------- app/views/stockit_selections/show.html.haml | 17 ++++------- config/routes.rb | 4 --- 10 files changed, 30 insertions(+), 104 deletions(-) diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index 36254f10..53bf0e25 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -1,7 +1,8 @@ class StockitController < ApplicationController def index - @stock_articles = StockArticle.elements_for_index + @stock_articles = StockArticle.includes(:supplier, :article_category). + order('suppliers.name, article_categories.name, articles.name') @stock_article_selection = StockArticleSelection.new end diff --git a/app/controllers/stockit_selections_controller.rb b/app/controllers/stockit_selections_controller.rb index 6e89b5dc..ae498c40 100644 --- a/app/controllers/stockit_selections_controller.rb +++ b/app/controllers/stockit_selections_controller.rb @@ -2,7 +2,7 @@ class StockitSelectionsController < ApplicationController def index - @stock_article_selections = StockArticleSelection.find(:all, :order => 'created_at DESC') + @stock_article_selections = StockArticleSelection.all end def show @@ -14,48 +14,43 @@ 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 erstellt.') + redirect_to(@stock_article_selection, :notice => 'Löschvorschlag für gewählte Artikel wurde erstellt.') else - @stock_articles = StockArticle.elements_for_index + @stock_articles = StockArticle.includes(:supplier, :article_category). + order('suppliers.name, article_categories.name, articles.name') render 'stockit/index' end end - def destroy # destroy selection without deleting articles + def destroy # destroy (open or finished) selection without deleting articles stock_article_selection = StockArticleSelection.find(params[:id]) stock_article_selection.destroy - redirect_to stock_article_selections_path, :notice => 'Löschvorschlag verworfen.' + redirect_to stock_article_selections_path, :notice => 'Löschvorschlag wurde verworfen.' end - def articles # destroy articles + def articles # destroy articles, finish selection 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.destroy # article.delete would save some effort, but validations are important 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 - flash[:notice] = "#{destroyed_articles_count} gewählte Artikel gelöscht." + 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." flash[:error] = "#{failed_articles_count} Artikel konnten nicht gelöscht werden." unless 0==failed_articles_count else - flash[:error] = 'Löschvorgang fehlgeschlagen. Keine Artikel gelöscht.' + flash[:error] = "Löschvorgang fehlgeschlagen. Es wurden 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 diff --git a/app/helpers/stock_article_selections_helper.rb b/app/helpers/stock_article_selections_helper.rb index 5d9d7737..9d7090f5 100644 --- a/app/helpers/stock_article_selections_helper.rb +++ b/app/helpers/stock_article_selections_helper.rb @@ -1,18 +1,2 @@ -# encoding: utf-8 module StockArticleSelectionsHelper - def article_deletion_classes(article) - className = "label label-success" # usual deletable case, maybe modified below - className = "label label-important" if article.quantity_available > 0 - className = "label" if article.deleted? - - className - end - - def article_deletion_title(article) - myTitle = "Löschbar" # usual deletable case, maybe modified below - myTitle = "Nicht löschbar, da im Lager vorhanden" if article.quantity_available > 0 - myTitle = "Bereits gelöscht" if article.deleted? - - myTitle - end end diff --git a/app/models/stock_article.rb b/app/models/stock_article.rb index 478e6c78..6050c8c4 100644 --- a/app/models/stock_article.rb +++ b/app/models/stock_article.rb @@ -22,11 +22,6 @@ class StockArticle < Article def self.stock_value available.collect { |a| a.quantity * a.gross_price }.sum end - - def self.elements_for_index - StockArticle.includes(:supplier, :article_category). - order('suppliers.name, article_categories.name, articles.name') - end protected diff --git a/app/models/stock_article_selection.rb b/app/models/stock_article_selection.rb index a66c61f2..20aafd7a 100644 --- a/app/models/stock_article_selection.rb +++ b/app/models/stock_article_selection.rb @@ -13,18 +13,6 @@ class StockArticleSelection < ActiveRecord::Base all_articles = stock_article_ids end - def deletable_count - stock_articles.select { |a| a.quantity_available<=0 }.length - end - - def nondeletable_count - stock_articles.select { |a| a.quantity_available>0 }.length - end - - def deleted_count - stock_articles.only_deleted.count - end - protected def include_stock_articles diff --git a/app/views/stockit/index.html.haml b/app/views/stockit/index.html.haml index b84b4f4f..5cd138d6 100644 --- a/app/views/stockit/index.html.haml +++ b/app/views/stockit/index.html.haml @@ -2,7 +2,7 @@ - content_for :javascript do :javascript $(function() { - $('tr.unavailable,input.unavailable,div.unavailable').hide(); + $('tr.unavailable,input.unavailable').hide(); }) .well.well-small @@ -12,7 +12,7 @@ Ansichtsoptionen %span.caret %ul.dropdown-menu - %li= link_to "Nicht verfügbare Artikel zeigen/verstecken", "#", 'data-toggle-this' => 'tr.unavailable,input.unavailable,div.unavailable', tabindex: -1 + %li= link_to "Nicht verfügbare Artikel zeigen/verstecken", "#", 'data-toggle-this' => 'tr.unavailable,input.unavailable', tabindex: -1 .btn-group = link_to_if @current_user.role_orders?, "Lagerbestellung online stellen", new_order_path(supplier_id: 0), @@ -65,8 +65,7 @@ %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', :remote => true - .form-actions.unavailable - = submit_tag "Artikel zum Löschen vormerken", { :class => 'unavailable btn' } + %p= submit_tag "Artikel zum Löschen vormerken", { :class => 'unavailable btn' } %p Aktueller Lagerwert: = number_to_currency StockArticle.stock_value diff --git a/app/views/stockit_selections/_overview.html.haml b/app/views/stockit_selections/_overview.html.haml index 5663fb4c..15f9b7f9 100644 --- a/app/views/stockit_selections/_overview.html.haml +++ b/app/views/stockit_selections/_overview.html.haml @@ -2,22 +2,15 @@ %table.table.table-hover %tr %th Artikel - %th Zusammenfassung %th Erstellt am %th Erstellt von %th Optionen - stock_article_selections.each do |stock_article_selection| %tr - %td - - for article in stock_article_selection.stock_articles.with_deleted - %span{:class => article_deletion_classes(article), :title => article_deletion_title(article)}= article.name - %td - %span{:class => 'label label-success'}= "#{stock_article_selection.deletable_count} Löschbar" - %span{:class => 'label'}= "#{stock_article_selection.deleted_count} Gelöscht" - %span{:class => 'label label-important'}= "#{stock_article_selection.nondeletable_count} Nicht löschbar" - %td= format_date(stock_article_selection.created_at) - %td= link_to_user_message_if_valid stock_article_selection.created_by + %td=h truncate stock_article_selection.stock_articles.map{ |article| article.name}.join(', ') + %td=h stock_article_selection.created_at + %td=h link_to_user_message_if_valid stock_article_selection.created_by %td = link_to 'Anzeigen', stock_article_selection, class: 'btn btn-small' = link_to "Artikel löschen", articles_stock_article_selection_path(stock_article_selection), :method => :delete, diff --git a/app/views/stockit_selections/index.html.haml b/app/views/stockit_selections/index.html.haml index f189f2ca..99cac93f 100644 --- a/app/views/stockit_selections/index.html.haml +++ b/app/views/stockit_selections/index.html.haml @@ -1,25 +1,7 @@ - title "Löschvorschläge für Lagerartikel" - -.well.well-small - .btn-toolbar - .btn-group - = link_to "Lager anzeigen", stock_articles_path, class: 'btn' - = link_to "Aufräumen", finished_stock_article_selections_path, :method => 'delete', - :confirm => 'Wirklich alle erledigten Löschvorschläge entfernen?', class: 'btn' - -.well - %h2 Ausstehende Löschvorschläge - - open_selections = @stock_article_selections.select { |sel| sel.deletable_count + sel.nondeletable_count > 0 } - - if open_selections.length == 0 - %p Es gibt keine ausstehenden Löschvorschläge. - %ul - %li= link_to "Löschvorschlag erstellen", stock_articles_path - - else - = render :partial => 'overview', :locals => {:stock_article_selections => open_selections} - -%h2 Erledigte Löschvorschläge -- finished_selections = @stock_article_selections.select { |sel| sel.deletable_count + sel.nondeletable_count <= 0 } -- if finished_selections.length == 0 - %p Es gibt keine erledigten Löschvorschläge. +- if @stock_article_selections.empty? + %p Es gibt keine ausstehenden Löschvorschläge. + %ul + %li= link_to "Löschvorschlag erstellen", stock_articles_path - else - = render :partial => 'overview', :locals => {:stock_article_selections => finished_selections} + = render :partial => 'overview', :locals => {:stock_article_selections => @stock_article_selections} diff --git a/app/views/stockit_selections/show.html.haml b/app/views/stockit_selections/show.html.haml index 9434421b..63ea5dfa 100644 --- a/app/views/stockit_selections/show.html.haml +++ b/app/views/stockit_selections/show.html.haml @@ -1,20 +1,13 @@ - title "Löschvorschlag für #{@stock_article_selection.stock_articles.count} Lagerartikel" -.well.well-small - .btn-toolbar - .btn-group - = link_to "Lager anzeigen", stock_articles_path, class: 'btn' - = link_to 'Alle Löschvorschläge anzeigen', stock_article_selections_path, class: 'btn' - %dl %dt Löschvorschlag vom: - %dd= format_time(@stock_article_selection.created_at) + %dd=h @stock_article_selection.created_at %dt Erstellt durch: - %dd= link_to_user_message_if_valid(@stock_article_selection.created_by) + %dd=h link_to_user_message_if_valid(@stock_article_selection.created_by) %dt Zu löschende Artikel - - for article in @stock_article_selection.stock_articles.with_deleted - %dd - %span{:class => article_deletion_classes(article), :title => article_deletion_title(article)}= article.name + - for article in @stock_article_selection.stock_articles + %dd=h article.name %p @@ -22,4 +15,4 @@ :confirm => 'Diesen Löschvorschlag wirklich ausführen und markierte Artikel löschen?', class: 'btn btn-danger' = link_to 'Verwerfen', @stock_article_selection, :method => :delete, :confirm => 'Diesen Löschvorschlag wirklich verwerfen?', class: 'btn' - = link_to 'Alle Löschvorschläge anzeigen', stock_article_selections_path, class: 'btn' + = link_to 'Alle Löschvorschläge zeigen', stock_article_selections_path, class: 'btn' diff --git a/config/routes.rb b/config/routes.rb index 6e108cce..9c27bbe7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -95,10 +95,6 @@ Foodsoft::Application.routes.draw do member do delete 'articles' end - - collection do - delete 'finished' - end end From 5dc3fc30adf5fb4b70c45a0663eb475e5652ec8d Mon Sep 17 00:00:00 2001 From: Julius Date: Mon, 25 Feb 2013 10:29:47 +0100 Subject: [PATCH 5/6] Revert 099e2b9b06bca02b205d15dd00c1872620c5b39f --- app/controllers/stockit_controller.rb | 1 - .../stockit_selections_controller.rb | 56 --------------- .../stock_article_selections_helper.rb | 2 - app/helpers/stockit_helper.rb | 11 --- app/models/stock_article_selection.rb | 24 ------- app/views/stockit/index.html.haml | 71 +++++++++---------- .../stockit_selections/_overview.html.haml | 19 ----- app/views/stockit_selections/index.html.haml | 7 -- app/views/stockit_selections/show.html.haml | 18 ----- config/routes.rb | 8 --- ...2121840_create_stock_article_selections.rb | 18 ----- db/schema.rb | 13 +--- 12 files changed, 33 insertions(+), 215 deletions(-) delete mode 100644 app/controllers/stockit_selections_controller.rb delete mode 100644 app/helpers/stock_article_selections_helper.rb delete mode 100644 app/models/stock_article_selection.rb delete mode 100644 app/views/stockit_selections/_overview.html.haml delete mode 100644 app/views/stockit_selections/index.html.haml delete mode 100644 app/views/stockit_selections/show.html.haml delete mode 100644 db/migrate/20130112121840_create_stock_article_selections.rb diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index 53bf0e25..f70bec27 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -3,7 +3,6 @@ class StockitController < ApplicationController def index @stock_articles = StockArticle.includes(:supplier, :article_category). order('suppliers.name, article_categories.name, articles.name') - @stock_article_selection = StockArticleSelection.new end def new diff --git a/app/controllers/stockit_selections_controller.rb b/app/controllers/stockit_selections_controller.rb deleted file mode 100644 index ae498c40..00000000 --- a/app/controllers/stockit_selections_controller.rb +++ /dev/null @@ -1,56 +0,0 @@ -# encoding: utf-8 -class StockitSelectionsController < ApplicationController - - def index - @stock_article_selections = StockArticleSelection.all - end - - def show - @stock_article_selection = StockArticleSelection.find(params[:id]) - end - - def create - @stock_article_selection = StockArticleSelection.new(params[:stock_article_selection]) - @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.') - else - @stock_articles = StockArticle.includes(:supplier, :article_category). - order('suppliers.name, article_categories.name, articles.name') - render 'stockit/index' - end - end - - def destroy # destroy (open or finished) 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.' - end - - def articles # destroy articles, finish selection - 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 - 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." - 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." - end - - redirect_to stock_articles_path - end -end diff --git a/app/helpers/stock_article_selections_helper.rb b/app/helpers/stock_article_selections_helper.rb deleted file mode 100644 index 9d7090f5..00000000 --- a/app/helpers/stock_article_selections_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module StockArticleSelectionsHelper -end diff --git a/app/helpers/stockit_helper.rb b/app/helpers/stockit_helper.rb index c2665a96..2888603c 100644 --- a/app/helpers/stockit_helper.rb +++ b/app/helpers/stockit_helper.rb @@ -1,18 +1,7 @@ -# encoding: utf-8 module StockitHelper def stock_article_classes(article) class_names = [] class_names << "unavailable" if article.quantity_available <= 0 class_names.join(" ") end - - def stock_article_delete_checkbox(article) - if article.quantity_available <= 0 - check_box_tag "stock_article_selection[stock_article_ids][]", article.id, false, - { :id => "checkbox_#{article.id}", :title => 'Zum löschen markieren' } - else - check_box_tag 'checkall', '1', false, - { :disabled => true, :title => 'Verfügbare Artikel können nicht gelöscht werden.', :class => 'unavailable' } - end - end end diff --git a/app/models/stock_article_selection.rb b/app/models/stock_article_selection.rb deleted file mode 100644 index 20aafd7a..00000000 --- a/app/models/stock_article_selection.rb +++ /dev/null @@ -1,24 +0,0 @@ -# encoding: utf-8 -class StockArticleSelection < ActiveRecord::Base - - # Associations - has_and_belongs_to_many :stock_articles - belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_user_id' - belongs_to :finished_by, :class_name => 'User', :foreign_key => 'finished_by_user_id' - - # Validations - validate :include_stock_articles - - def all_articles - all_articles = stock_article_ids - end - - protected - - def include_stock_articles - errors.add(:stock_articles, "Es muss mindestens ein Lagerartikel ausgewählt sein.") if stock_articles.empty? - end - - private - -end diff --git a/app/views/stockit/index.html.haml b/app/views/stockit/index.html.haml index 5cd138d6..6ceb0e91 100644 --- a/app/views/stockit/index.html.haml +++ b/app/views/stockit/index.html.haml @@ -2,7 +2,7 @@ - content_for :javascript do :javascript $(function() { - $('tr.unavailable,input.unavailable').hide(); + $('tr.unavailable').hide(); }) .well.well-small @@ -12,7 +12,7 @@ Ansichtsoptionen %span.caret %ul.dropdown-menu - %li= link_to "Nicht verfügbare Artikel zeigen/verstecken", "#", 'data-toggle-this' => 'tr.unavailable,input.unavailable', tabindex: -1 + %li= link_to "Nicht verfügbare Artikel zeigen/verstecken", "#", 'data-toggle-this' => 'tr.unavailable', tabindex: -1 .btn-group = link_to_if @current_user.role_orders?, "Lagerbestellung online stellen", new_order_path(supplier_id: 0), @@ -20,7 +20,6 @@ = link_to "Neuen Lagerartikel anlegen", new_stock_article_path, class: 'btn' = link_to "Inventur anlegen", new_stock_taking_path, class: 'btn' = link_to "Inventurübersicht", stock_takings_path, class: 'btn' - = link_to 'Löschvorschläge', stock_article_selections_path, class: 'btn' .btn-group = link_to '#', data: {toggle: 'dropdown'}, class: 'btn dropdown-toggle' do @@ -30,42 +29,36 @@ - Supplier.all.each do |supplier| %li= link_to supplier.name, new_supplier_delivery_path(supplier), tabindex: -1 -= form_for @stock_article_selection do |form| - - if @stock_article_selection.errors.has_key?(:stock_articles) - .alert.alert-error - = @stock_article_selection.errors.get(:stock_articles).join(" ") - %table.table.table-hover#articles - %thead - %tr - %th= check_box_tag 'checkall', '1', false, - { 'data-check-all' => 'table#articles tr.unavailable', :title => 'Alle löschbaren Artikel', :class => 'unavailable' } - %th Artikel - %th im Lager - %th davon bestellt - %th verfügbar - %th Einheit - %th Preis - %th MwSt - %th Lieferantin - %th Kategorie - %th - %tbody - - for article in @stock_articles - %tr{:class => stock_article_classes(article)} - %td= stock_article_delete_checkbox(article) - %td=h article.name - %td= article.quantity - %td= article.quantity - article.quantity_available - %th= article.quantity_available - %td= article.unit - %td= article.price - %td= number_to_percentage article.tax - %td= link_to article.supplier.name, article.supplier - %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', :remote => true - %p= submit_tag "Artikel zum Löschen vormerken", { :class => 'unavailable btn' } +%table.table.table-hover#articles + %thead + %tr + %th Artikel + %th im Lager + %th davon bestellt + %th verfügbar + %th Einheit + %th Preis + %th MwSt + %th Lieferantin + %th Kategorie + %th + %tbody + - for article in @stock_articles + %tr{:class => stock_article_classes(article)} + %td=h article.name + %td= article.quantity + %td= article.quantity - article.quantity_available + %th= article.quantity_available + %td= article.unit + %td= article.price + %td= number_to_percentage article.tax + %td= link_to article.supplier.name, article.supplier + %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', :remote => true + %p Aktueller Lagerwert: = number_to_currency StockArticle.stock_value diff --git a/app/views/stockit_selections/_overview.html.haml b/app/views/stockit_selections/_overview.html.haml deleted file mode 100644 index 15f9b7f9..00000000 --- a/app/views/stockit_selections/_overview.html.haml +++ /dev/null @@ -1,19 +0,0 @@ - -%table.table.table-hover - %tr - %th Artikel - %th Erstellt am - %th Erstellt von - %th Optionen - - - stock_article_selections.each do |stock_article_selection| - %tr - %td=h truncate stock_article_selection.stock_articles.map{ |article| article.name}.join(', ') - %td=h stock_article_selection.created_at - %td=h link_to_user_message_if_valid stock_article_selection.created_by - %td - = link_to 'Anzeigen', stock_article_selection, class: 'btn btn-small' - = link_to "Artikel löschen", articles_stock_article_selection_path(stock_article_selection), :method => :delete, - :confirm => 'Diesen Löschvorschlag wirklich ausführen und markierte Artikel löschen?', class: 'btn btn-small btn-danger' - = link_to "Verwerfen", stock_article_selection, :method => :delete, - :confirm => 'Diesen Löschvorschlag wirklich verwerfen?', class: 'btn btn-small' diff --git a/app/views/stockit_selections/index.html.haml b/app/views/stockit_selections/index.html.haml deleted file mode 100644 index 99cac93f..00000000 --- a/app/views/stockit_selections/index.html.haml +++ /dev/null @@ -1,7 +0,0 @@ -- title "Löschvorschläge für Lagerartikel" -- if @stock_article_selections.empty? - %p Es gibt keine ausstehenden Löschvorschläge. - %ul - %li= link_to "Löschvorschlag erstellen", stock_articles_path -- else - = render :partial => 'overview', :locals => {:stock_article_selections => @stock_article_selections} diff --git a/app/views/stockit_selections/show.html.haml b/app/views/stockit_selections/show.html.haml deleted file mode 100644 index 63ea5dfa..00000000 --- a/app/views/stockit_selections/show.html.haml +++ /dev/null @@ -1,18 +0,0 @@ -- title "Löschvorschlag für #{@stock_article_selection.stock_articles.count} Lagerartikel" - -%dl - %dt Löschvorschlag vom: - %dd=h @stock_article_selection.created_at - %dt Erstellt durch: - %dd=h link_to_user_message_if_valid(@stock_article_selection.created_by) - %dt Zu löschende Artikel - - for article in @stock_article_selection.stock_articles - %dd=h article.name - - -%p - = link_to 'Artikel löschen', articles_stock_article_selection_path(@stock_article_selection), :method => :delete, - :confirm => 'Diesen Löschvorschlag wirklich ausführen und markierte Artikel löschen?', class: 'btn btn-danger' - = link_to 'Verwerfen', @stock_article_selection, :method => :delete, - :confirm => 'Diesen Löschvorschlag wirklich verwerfen?', class: 'btn' - = link_to 'Alle Löschvorschläge zeigen', stock_article_selections_path, class: 'btn' diff --git a/config/routes.rb b/config/routes.rb index 9c27bbe7..aa8e0e29 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -89,14 +89,6 @@ Foodsoft::Application.routes.draw do post :add_stock_article end end - - resources :stock_article_selections, :controller => 'stockit_selections', - :only => [ :create, :show, :index, :destroy ], :path => "/stock_articles/selections" do - member do - delete 'articles' - end - end - resources :stock_articles, :to => 'stockit' do collection do diff --git a/db/migrate/20130112121840_create_stock_article_selections.rb b/db/migrate/20130112121840_create_stock_article_selections.rb deleted file mode 100644 index 2ead9008..00000000 --- a/db/migrate/20130112121840_create_stock_article_selections.rb +++ /dev/null @@ -1,18 +0,0 @@ -class CreateStockArticleSelections < ActiveRecord::Migration - def up - create_table :stock_article_selections do |t| - t.integer :created_by_user_id - t.timestamps - end - - create_table :stock_article_selections_stock_articles, :id => false do |t| - t.integer :stock_article_id - t.integer :stock_article_selection_id - end - end - - def down - drop_table :stock_article_selections - drop_table :stock_article_selections_stock_articles - end -end diff --git a/db/schema.rb b/db/schema.rb index bec2b950..188ea83b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130112121840) do +ActiveRecord::Schema.define(:version => 20121230142516) do create_table "article_categories", :force => true do |t| t.string "name", :default => "", :null => false @@ -268,17 +268,6 @@ ActiveRecord::Schema.define(:version => 20130112121840) do add_index "pages", ["permalink"], :name => "index_pages_on_permalink" add_index "pages", ["title"], :name => "index_pages_on_title" - create_table "stock_article_selections", :force => true do |t| - t.integer "created_by_user_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false - end - - create_table "stock_article_selections_stock_articles", :id => false, :force => true do |t| - t.integer "stock_article_id" - t.integer "stock_article_selection_id" - end - create_table "stock_changes", :force => true do |t| t.integer "delivery_id" t.integer "order_id" From 0e1bd5c75e0ed1d0f71adf94f63649b266a9c9b7 Mon Sep 17 00:00:00 2001 From: Julius Date: Mon, 25 Feb 2013 11:12:28 +0100 Subject: [PATCH 6/6] Improve (AJAX) deletion of StockArticles --- app/controllers/stockit_controller.rb | 6 +++--- app/views/stockit/_destroy_fail.js.haml | 8 +++----- app/views/stockit/destroy.js.haml | 15 ++++++++++++++- app/views/stockit/index.html.haml | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index f70bec27..2a232a31 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -32,9 +32,9 @@ class StockitController < ApplicationController end def destroy - StockArticle.find(params[:id]).destroy - render :layout => false, - :locals => { :destroyed_article_id => params[:id] } + @article = StockArticle.find(params[:id]) + @article.destroy + render :layout => false rescue => error render :partial => "destroy_fail", :layout => false, :locals => { :fail_msg => "Ein Fehler ist aufgetreten: " + error.message } diff --git a/app/views/stockit/_destroy_fail.js.haml b/app/views/stockit/_destroy_fail.js.haml index 60b03d87..a9601900 100644 --- a/app/views/stockit/_destroy_fail.js.haml +++ b/app/views/stockit/_destroy_fail.js.haml @@ -1,7 +1,5 @@ --# please polish the following line if you know how -var errorDiv = $(''); - --# next line, createTextNode called by .text(textString) helps escaping for html, but what about ' signs? -errorDiv.text('#{fail_msg}'); +-# please polish the following line if you know how, same in view destroy +var errorDiv = $(''); +errorDiv.append(document.createTextNode('#{j(fail_msg)}')); $('div.container-fluid').prepend(errorDiv); diff --git a/app/views/stockit/destroy.js.haml b/app/views/stockit/destroy.js.haml index 39602dfb..66d6d70d 100644 --- a/app/views/stockit/destroy.js.haml +++ b/app/views/stockit/destroy.js.haml @@ -1 +1,14 @@ -console.log('#{destroyed_article_id}'); +-# please polish the following line if you know how, same in partial _destroy_fail +var successDiv = $(''); + +successDiv.append(document.createTextNode('Artikel #{j(@article.name)} gelöscht.')); +$('div.container-fluid').prepend(successDiv); + +-# WARNING: If you try to use the escape j(...) here, an error occurs: +-# Ein Fehler ist aufgetreten: undefined method `gsub' for 50:Fixnum +-# However, it should work without without escaping. +-# Note that article names which are purely numeric, e.g. 12345, are escaped correctly (see above). + +$('#stockArticle-#{@article.id}').remove(); + +-# WARNING: Do not use a simple .fadeOut() above, because it conflicts with the show/hide function of unavailable articles. diff --git a/app/views/stockit/index.html.haml b/app/views/stockit/index.html.haml index 6ceb0e91..c69c1689 100644 --- a/app/views/stockit/index.html.haml +++ b/app/views/stockit/index.html.haml @@ -44,7 +44,7 @@ %th %tbody - for article in @stock_articles - %tr{:class => stock_article_classes(article)} + %tr{:class => stock_article_classes(article), :id => "stockArticle-#{article.id}"} %td=h article.name %td= article.quantity %td= article.quantity - article.quantity_available