From 209a05fb3e7409993a17bebdea6a75e1933761ff Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 3 Oct 2013 14:48:26 +0200 Subject: [PATCH 01/11] Move StockArticle actions to StockArticle controller; introduce publish/subscribe pattern for AJAX updates --- app/controllers/deliveries_controller.rb | 51 +++------------- app/controllers/stockit_controller.rb | 58 ++++++++++++++++--- app/helpers/deliveries_helper.rb | 9 --- app/helpers/stockit_helper.rb | 9 +++ app/views/deliveries/_form.html.haml | 24 +++++++- .../_stock_article_for_adding.html.haml | 4 +- .../deliveries/_stock_article_form.html.haml | 23 -------- .../deliveries/copy_stock_article.js.erb | 5 -- .../deliveries/create_stock_article.js.erb | 17 ------ .../deliveries/derive_stock_article.js.erb | 5 -- .../deliveries/edit_stock_article.js.erb | 5 -- app/views/deliveries/new_stock_article.js.erb | 5 -- .../deliveries/on_stock_article_create.js.erb | 14 +++++ ....js.erb => on_stock_article_update.js.erb} | 16 +++-- app/views/shared/_alert_error.haml | 5 ++ app/views/stockit/_destroy_fail.js.erb | 6 ++ app/views/stockit/_destroy_fail.js.haml | 5 -- app/views/stockit/_form.html.haml | 39 +++++++------ app/views/stockit/_stock_article.html.haml | 15 +++++ app/views/stockit/copy.js.erb | 9 +++ app/views/stockit/create.js.erb | 13 +++++ app/views/stockit/destroy.js.erb | 11 ++++ app/views/stockit/destroy.js.haml | 8 --- app/views/stockit/edit.html.haml | 3 - app/views/stockit/edit.js.erb | 9 +++ app/views/stockit/index.html.haml | 44 ++++++++------ app/views/stockit/new.html.haml | 22 ------- app/views/stockit/new.js.erb | 9 +++ .../stockit/on_stock_article_create.js.erb | 10 ++++ .../stockit/on_stock_article_update.js.erb | 10 ++++ app/views/stockit/update.js.erb | 13 +++++ config/locales/de.yml | 25 ++++---- config/routes.rb | 20 ++++--- 33 files changed, 293 insertions(+), 228 deletions(-) delete mode 100644 app/views/deliveries/_stock_article_form.html.haml delete mode 100644 app/views/deliveries/copy_stock_article.js.erb delete mode 100644 app/views/deliveries/create_stock_article.js.erb delete mode 100644 app/views/deliveries/derive_stock_article.js.erb delete mode 100644 app/views/deliveries/edit_stock_article.js.erb delete mode 100644 app/views/deliveries/new_stock_article.js.erb create mode 100644 app/views/deliveries/on_stock_article_create.js.erb rename app/views/deliveries/{update_stock_article.js.erb => on_stock_article_update.js.erb} (66%) create mode 100644 app/views/shared/_alert_error.haml create mode 100644 app/views/stockit/_destroy_fail.js.erb delete mode 100644 app/views/stockit/_destroy_fail.js.haml create mode 100644 app/views/stockit/_stock_article.html.haml create mode 100644 app/views/stockit/copy.js.erb create mode 100644 app/views/stockit/create.js.erb create mode 100644 app/views/stockit/destroy.js.erb delete mode 100644 app/views/stockit/destroy.js.haml delete mode 100644 app/views/stockit/edit.html.haml create mode 100644 app/views/stockit/edit.js.erb delete mode 100644 app/views/stockit/new.html.haml create mode 100644 app/views/stockit/new.js.erb create mode 100644 app/views/stockit/on_stock_article_create.js.erb create mode 100644 app/views/stockit/on_stock_article_update.js.erb create mode 100644 app/views/stockit/update.js.erb diff --git a/app/controllers/deliveries_controller.rb b/app/controllers/deliveries_controller.rb index 86eedc147..0016a29fd 100644 --- a/app/controllers/deliveries_controller.rb +++ b/app/controllers/deliveries_controller.rb @@ -1,7 +1,7 @@ # encoding: utf-8 class DeliveriesController < ApplicationController - before_filter :find_supplier, :exclude => :fill_new_stock_article_form + before_filter :find_supplier def index @deliveries = @supplier.deliveries.all :order => 'delivered_on DESC' @@ -49,57 +49,22 @@ def destroy flash[:notice] = I18n.t('deliveries.destroy.notice') redirect_to supplier_deliveries_url(@supplier) end - - # three possibilites to fill a new_stock_article form - # (1) start from blank or use params - def new_stock_article - @stock_article = @supplier.stock_articles.build(params[:stock_article]) - render :layout => false - end - - # (2) StockArticle as template - def copy_stock_article - @stock_article = StockArticle.find(params[:old_stock_article_id]).dup - - render :layout => false - end - - # (3) non-stock Article as template - def derive_stock_article - @stock_article = Article.find(params[:old_article_id]).becomes(StockArticle).dup - + def add_stock_change + @stock_change = StockChange.new + @stock_change.stock_article = StockArticle.find(params[:stock_article_id]) render :layout => false end - def create_stock_article - @stock_article = StockArticle.new(params[:stock_article]) + def on_stock_article_create + @stock_article = StockArticle.find(params[:id]) - if @stock_article.valid? and @stock_article.save - render :layout => false - else - render :action => 'new_stock_article', :layout => false - end - end - - def edit_stock_article - @stock_article = StockArticle.find(params[:stock_article_id]) render :layout => false end - def update_stock_article - @stock_article = StockArticle.find(params[:stock_article][:id]) + def on_stock_article_update + @stock_article = StockArticle.find(params[:id]) - if @stock_article.update_attributes(params[:stock_article]) - render :layout => false - else - render :action => 'edit_stock_article', :layout => false - end - end - - def add_stock_change - @stock_change = StockChange.new - @stock_change.stock_article = StockArticle.find(params[:stock_article_id]) render :layout => false end diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index 3e3b43b2e..24434e5f5 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -5,35 +5,55 @@ def index order('suppliers.name, article_categories.name, articles.name') end + # three possibilites to fill a new_stock_article form + # (1) start from blank or use params def new - @stock_article = StockArticle.new + @stock_article = StockArticle.new(params[:stock_article]) + + render :layout => false + end + + # (2) StockArticle as template + def copy + @stock_article = StockArticle.find(params[:stock_article_id]).dup + + render :layout => false + end + + # (3) non-stock Article as template + def derive + @stock_article = Article.find(params[:old_article_id]).becomes(StockArticle).dup + + render :layout => false end def create @stock_article = StockArticle.new(params[:stock_article]) - if @stock_article.save - redirect_to stock_articles_path, :notice => I18n.t('stockit.stock_create.notice') + if @stock_article.valid? and @stock_article.save + render :layout => false else - render :action => 'new' + render :action => 'new', :layout => false end end def edit @stock_article = StockArticle.find(params[:id]) + + render :layout => false end def update @stock_article = StockArticle.find(params[:id]) if @stock_article.update_attributes(params[:stock_article]) - redirect_to stock_articles_path, :notice => I18n.t('stockit.stock_update.notice') + render :layout => false else - render :action => 'edit' + render :action => 'edit', :layout => false end end def destroy - @article = StockArticle.find(params[:id]) - @article.mark_as_deleted + @stock_article = StockArticle.find(params[:id]) + @stock_article.mark_as_deleted render :layout => false rescue => error render :partial => "destroy_fail", :layout => false, @@ -60,4 +80,26 @@ def history @stock_article = StockArticle.undeleted.find(params[:stock_article_id]) @stock_changes = @stock_article.stock_changes.order('stock_changes.created_at DESC').each {|s| s.readonly!} end + + def create_stock_article + @stock_article = StockArticle.new(params[:stock_article]) + + if @stock_article.valid? and @stock_article.save + render :layout => false + else + render :action => 'new_stock_article', :layout => false + end + end + + def on_stock_article_create + @stock_article = StockArticle.find(params[:id]) + + render :layout => false + end + + def on_stock_article_update + @stock_article = StockArticle.find(params[:id]) + + render :layout => false + end end diff --git a/app/helpers/deliveries_helper.rb b/app/helpers/deliveries_helper.rb index b23107cc9..2ef5d14b4 100644 --- a/app/helpers/deliveries_helper.rb +++ b/app/helpers/deliveries_helper.rb @@ -25,13 +25,4 @@ def stock_change_remove_link(stock_change_form) return output.html_safe end - def stock_article_price_hint(stock_article) - t('simple_form.hints.stock_article.edit_stock_article.price', - :stock_article_copy_link => link_to(t('.copy_stock_article'), - copy_stock_article_supplier_deliveries_path(@supplier, :old_stock_article_id => stock_article.id), - :remote => true - ) - ) - end - end diff --git a/app/helpers/stockit_helper.rb b/app/helpers/stockit_helper.rb index f6a0a1fce..2841309c7 100644 --- a/app/helpers/stockit_helper.rb +++ b/app/helpers/stockit_helper.rb @@ -14,4 +14,13 @@ def link_to_stock_change_reason(stock_change) link_to t('.stock_taking'), stock_taking_path(stock_change.stock_taking) end end + + def stock_article_price_hint(stock_article) + t('simple_form.hints.stock_article.edit.price', + :stock_article_copy_link => link_to(t('.copy_stock_article'), + stock_article_copy_path(stock_article), + :remote => true + ) + ) + end end diff --git a/app/views/deliveries/_form.html.haml b/app/views/deliveries/_form.html.haml index c17027083..ad60b6e45 100644 --- a/app/views/deliveries/_form.html.haml +++ b/app/views/deliveries/_form.html.haml @@ -42,7 +42,7 @@ } if('new' == selectedArticle.id) { $.ajax({ - url: '#{new_stock_article_supplier_deliveries_path(@supplier)}', + url: '#{new_stock_article_path}', type: 'get', data: {stock_article: {name: selectedArticle.text}}, contentType: 'application/json; charset=UTF-8' @@ -52,7 +52,7 @@ } if('' != selectedArticle.id) { $.ajax({ - url: '#{derive_stock_article_supplier_deliveries_path(@supplier)}', + url: '#{derive_stock_articles_path}', type: 'get', data: {old_article_id: selectedArticle.id}, contentType: 'application/json; charset=UTF-8' @@ -63,6 +63,24 @@ }); enablePriceTooltips(); + + $('body').on('StockArticle#create', function(e) { + $.ajax({ + url: '#{on_stock_article_create_supplier_deliveries_path(@supplier)}', + type: 'get', + data: {id: e.stock_article_id}, + contentType: 'application/json; charset=UTF-8' + }); + }); + + $('body').on('StockArticle#update', function(e) { + $.ajax({ + url: '#{on_stock_article_update_supplier_deliveries_path(@supplier)}', + type: 'get', + data: {id: e.stock_article_id}, + contentType: 'application/json; charset=UTF-8' + }); + }); }); function mark_article_for_delivery(stock_article_id) { @@ -106,7 +124,7 @@ %tr %th{:colspan => 5} - if articles_for_select2(@supplier).empty? - = link_to t('.create_stock_article'), new_stock_article_supplier_deliveries_path(@supplier), :remote => true, :class => 'btn' + = link_to t('.create_stock_article'), new_stock_article_path(:params => {:stock_article => {:supplier_id => @supplier.id}}), :remote => true, :class => 'btn' - else %input#new_stock_article{:style => 'width: 500px;'} %tbody diff --git a/app/views/deliveries/_stock_article_for_adding.html.haml b/app/views/deliveries/_stock_article_for_adding.html.haml index b43df7828..956d8379b 100644 --- a/app/views/deliveries/_stock_article_for_adding.html.haml +++ b/app/views/deliveries/_stock_article_for_adding.html.haml @@ -5,7 +5,7 @@ %td= article.unit %td= article.article_category.name %td - = link_to t('.action_edit'), edit_stock_article_supplier_deliveries_path(@supplier, :stock_article_id => article.id), remote: true, class: 'btn btn-mini' - = link_to t('.action_other_price'), copy_stock_article_supplier_deliveries_path(@supplier, :old_stock_article_id => article.id), remote: true, class: 'btn btn-mini' + = link_to t('.action_edit'), edit_stock_article_path(article), remote: true, class: 'btn btn-mini' + = link_to t('.action_other_price'), stock_article_copy_path(article), remote: true, class: 'btn btn-mini' - deliver_button_disabled = ( @delivery and @delivery.includes_article? article ) ? ( 'disabled' ) : ( false ) = link_to t('.action_add_to_delivery'), add_stock_change_supplier_deliveries_path(@supplier, :stock_article_id => article.id), :method => :post, remote: true, class: 'button-add-stock-change btn btn-mini btn-primary', disabled: deliver_button_disabled diff --git a/app/views/deliveries/_stock_article_form.html.haml b/app/views/deliveries/_stock_article_form.html.haml deleted file mode 100644 index 21c5b216b..000000000 --- a/app/views/deliveries/_stock_article_form.html.haml +++ /dev/null @@ -1,23 +0,0 @@ -- url = ( stock_article.new_record? ) ? ( create_stock_article_supplier_deliveries_path(@supplier) ) : ( update_stock_article_supplier_deliveries_path(@supplier) ) -= simple_form_for stock_article, url: url, remote: true, validate: true do |f| - = f.association :supplier, :as => :hidden - = f.hidden_field :id unless stock_article.new_record? - .modal-header - = link_to t('ui.marks.close').html_safe, '#', class: 'close', data: {dismiss: 'modal'} - %h3= t 'activerecord.models.stock_article' - .modal-body - = f.input :name - = f.input :unit - = f.input :note - - if stock_article.new_record? - = f.input :price - = f.input :tax, :wrapper => :append do - = f.input_field :tax - %span.add-on % - = f.input :deposit - - else - = f.input :price, :input_html => {:disabled => 'disabled'}, :hint => stock_article_price_hint(stock_article) - = f.association :article_category - .modal-footer - = link_to t('ui.close'), '#', class: 'btn', data: {dismiss: 'modal'} - = f.submit :class => 'btn btn-primary', 'data-disable-with' => t('ui.please_wait') diff --git a/app/views/deliveries/copy_stock_article.js.erb b/app/views/deliveries/copy_stock_article.js.erb deleted file mode 100644 index de5d260e8..000000000 --- a/app/views/deliveries/copy_stock_article.js.erb +++ /dev/null @@ -1,5 +0,0 @@ -$('#modalContainer').html( - '<%= j(render(:partial => "stock_article_form", :locals => {:stock_article => @stock_article})) %>' -); - -$('#modalContainer').modal(); diff --git a/app/views/deliveries/create_stock_article.js.erb b/app/views/deliveries/create_stock_article.js.erb deleted file mode 100644 index 5e2893a92..000000000 --- a/app/views/deliveries/create_stock_article.js.erb +++ /dev/null @@ -1,17 +0,0 @@ -$('div.container-fluid').prepend( - '<%= j(render(:partial => 'shared/alert_success', :locals => {:alert_message => t('.notice', :name => @stock_article.name)})) %>' -); - -(function() { - $('#stock_articles_for_adding tr').removeClass('success'); - - var stock_article_for_adding = $( - '<%= j(render(:partial => 'stock_article_for_adding', :locals => {:article => @stock_article})) %>' - ).addClass('success'); - enablePriceTooltips(stock_article_for_adding); - - $('#stock_articles_for_adding tbody').append(stock_article_for_adding); - updateSort('#stock_articles_for_adding'); -})(); - -$('#modalContainer').modal('hide'); diff --git a/app/views/deliveries/derive_stock_article.js.erb b/app/views/deliveries/derive_stock_article.js.erb deleted file mode 100644 index de5d260e8..000000000 --- a/app/views/deliveries/derive_stock_article.js.erb +++ /dev/null @@ -1,5 +0,0 @@ -$('#modalContainer').html( - '<%= j(render(:partial => "stock_article_form", :locals => {:stock_article => @stock_article})) %>' -); - -$('#modalContainer').modal(); diff --git a/app/views/deliveries/edit_stock_article.js.erb b/app/views/deliveries/edit_stock_article.js.erb deleted file mode 100644 index de5d260e8..000000000 --- a/app/views/deliveries/edit_stock_article.js.erb +++ /dev/null @@ -1,5 +0,0 @@ -$('#modalContainer').html( - '<%= j(render(:partial => "stock_article_form", :locals => {:stock_article => @stock_article})) %>' -); - -$('#modalContainer').modal(); diff --git a/app/views/deliveries/new_stock_article.js.erb b/app/views/deliveries/new_stock_article.js.erb deleted file mode 100644 index de5d260e8..000000000 --- a/app/views/deliveries/new_stock_article.js.erb +++ /dev/null @@ -1,5 +0,0 @@ -$('#modalContainer').html( - '<%= j(render(:partial => "stock_article_form", :locals => {:stock_article => @stock_article})) %>' -); - -$('#modalContainer').modal(); diff --git a/app/views/deliveries/on_stock_article_create.js.erb b/app/views/deliveries/on_stock_article_create.js.erb new file mode 100644 index 000000000..b46583a49 --- /dev/null +++ b/app/views/deliveries/on_stock_article_create.js.erb @@ -0,0 +1,14 @@ +(function() { + $('#stock_articles_for_adding tr').removeClass('success'); + + var stock_article_for_adding = $('<%= j(render( + :partial => 'stock_article_for_adding', + :locals => { + :article => @stock_article + } + )) %>').addClass('success'); + enablePriceTooltips(stock_article_for_adding); + + $('#stock_articles_for_adding tbody').append(stock_article_for_adding); + updateSort('#stock_articles_for_adding'); +})(); diff --git a/app/views/deliveries/update_stock_article.js.erb b/app/views/deliveries/on_stock_article_update.js.erb similarity index 66% rename from app/views/deliveries/update_stock_article.js.erb rename to app/views/deliveries/on_stock_article_update.js.erb index ef46636db..6ffdfc5ac 100644 --- a/app/views/deliveries/update_stock_article.js.erb +++ b/app/views/deliveries/on_stock_article_update.js.erb @@ -1,15 +1,15 @@ -$('div.container-fluid').prepend( - '<%= j(render(:partial => 'shared/alert_success', :locals => {:alert_message => t('.notice', :name => @stock_article.name)})) %>' -); - (function() { // update entry in stock_article table $('#stock_articles_for_adding tr').removeClass('success'); - var stock_article_for_adding = $( - '<%= j(render(:partial => 'stock_article_for_adding', :locals => {:article => @stock_article, :delivery => @delivery})) %>' - ).addClass('success'); + var stock_article_for_adding = $('<%= j(render( + :partial => 'stock_article_for_adding', + :locals => { + :article => @stock_article, + :delivery => @delivery + } + )) %>').addClass('success'); enablePriceTooltips(stock_article_for_adding); $('#stock_article_<%= @stock_article.id %>').replaceWith(stock_article_for_adding); @@ -29,5 +29,3 @@ $('div.container-fluid').prepend( updateSort('#stock_changes'); })(); - -$('#modalContainer').modal('hide'); diff --git a/app/views/shared/_alert_error.haml b/app/views/shared/_alert_error.haml new file mode 100644 index 000000000..069dc61df --- /dev/null +++ b/app/views/shared/_alert_error.haml @@ -0,0 +1,5 @@ +.alert.fade.in.alert-error + %a.close{:href => '#', :data => {:dismiss => 'alert'}} + = t('ui.marks.close').html_safe + = t('ui.marks.error').html_safe + = alert_message diff --git a/app/views/stockit/_destroy_fail.js.erb b/app/views/stockit/_destroy_fail.js.erb new file mode 100644 index 000000000..2ccffa44b --- /dev/null +++ b/app/views/stockit/_destroy_fail.js.erb @@ -0,0 +1,6 @@ +$('div.container-fluid').prepend('<%= j(render( + :partial => 'shared/alert_error', + :locals => { + :alert_message => fail_msg + } +)) %>'); diff --git a/app/views/stockit/_destroy_fail.js.haml b/app/views/stockit/_destroy_fail.js.haml deleted file mode 100644 index ca85c67c8..000000000 --- a/app/views/stockit/_destroy_fail.js.haml +++ /dev/null @@ -1,5 +0,0 @@ --# please polish the following line if you know how, same in view destroy -var errorDiv = $('
#{t('ui.marks.close').html_safe}
'); - -errorDiv.append(document.createTextNode('#{j(fail_msg)}')); -$('div.container-fluid').prepend(errorDiv); diff --git a/app/views/stockit/_form.html.haml b/app/views/stockit/_form.html.haml index 8b96aea8f..4b4b0b16c 100644 --- a/app/views/stockit/_form.html.haml +++ b/app/views/stockit/_form.html.haml @@ -1,18 +1,21 @@ -= simple_form_for stock_article, :validate => true do |f| - = f.association :supplier - = f.input :name - = f.input :unit - = f.input :note - - - if stock_article.new_record? - = f.input :price - = f.input :tax, :wrapper => :append do - = f.input_field :tax - %span.add-on % - = f.input :deposit - - else - = f.input :price, :input_html => {:disabled => 'disabled'}, :hint => t('.form.price_hint') - = f.association :article_category - .form-actions - = f.submit class: 'btn' - = link_to t('ui.or_cancel'), stock_articles_path += simple_form_for stock_article, remote: true, :validate => true do |f| + .modal-header + = link_to t('ui.marks.close').html_safe, '#', class: 'close', data: {dismiss: 'modal'} + %h3= title + .modal-body + = f.association :supplier + = f.input :name + = f.input :unit + = f.input :note + - if stock_article.new_record? + = f.input :price + = f.input :tax, :wrapper => :append do + = f.input_field :tax + %span.add-on % + = f.input :deposit + - else + = f.input :price, :input_html => {:disabled => 'disabled'}, :hint => stock_article_price_hint(stock_article) + = f.association :article_category + .modal-footer + = link_to t('ui.close'), '#', class: 'btn', data: {dismiss: 'modal'} + = f.submit :class => 'btn btn-primary', 'data-disable-with' => t('ui.please_wait') diff --git a/app/views/stockit/_stock_article.html.haml b/app/views/stockit/_stock_article.html.haml new file mode 100644 index 000000000..53cb304fa --- /dev/null +++ b/app/views/stockit/_stock_article.html.haml @@ -0,0 +1,15 @@ +%tr{:class => stock_article_classes(stock_article), :id => "stockArticle-#{stock_article.id}"} + %td= stock_article.name + %td= stock_article.quantity + %td= stock_article.quantity - stock_article.quantity_available + %th= stock_article.quantity_available + %td= stock_article.unit + %td= stock_article.price + %td= number_to_percentage stock_article.tax + %td= link_to stock_article.supplier.name, stock_article.supplier + %td= stock_article.article_category.name + %td + = link_to t('ui.edit'), edit_stock_article_path(stock_article), remote: true, class: 'btn btn-mini' + = link_to t('ui.history'), stock_article_history_path(stock_article), class: 'btn btn-mini' + = link_to t('ui.delete'), stock_article, :method => :delete, :confirm => t('.confirm_delete', :name => stock_article.name), + class: 'btn btn-mini btn-danger', :remote => true diff --git a/app/views/stockit/copy.js.erb b/app/views/stockit/copy.js.erb new file mode 100644 index 000000000..e3bc33217 --- /dev/null +++ b/app/views/stockit/copy.js.erb @@ -0,0 +1,9 @@ +$('#modalContainer').html('<%= j(render( + :partial => "form", + :locals => { + :title => t('.title'), + :stock_article => @stock_article + } +)) %>'); + +$('#modalContainer').modal(); diff --git a/app/views/stockit/create.js.erb b/app/views/stockit/create.js.erb new file mode 100644 index 000000000..c6defcf81 --- /dev/null +++ b/app/views/stockit/create.js.erb @@ -0,0 +1,13 @@ +$('div.container-fluid').prepend('<%= j(render( + :partial => 'shared/alert_success', + :locals => { + :alert_message => t('.notice', :name => @stock_article.name) + } +)) %>'); + +$('body').trigger({ + type: 'StockArticle#create', + stock_article_id: <%= @stock_article.id %> +}); + +$('#modalContainer').modal('hide'); diff --git a/app/views/stockit/destroy.js.erb b/app/views/stockit/destroy.js.erb new file mode 100644 index 000000000..fa44b47ac --- /dev/null +++ b/app/views/stockit/destroy.js.erb @@ -0,0 +1,11 @@ +$('div.container-fluid').prepend('<%= j(render( + :partial => 'shared/alert_success', + :locals => { + :alert_message => t('.notice', :name => @stock_article.name) + } +)) %>'); + +$('body').trigger({ + type: 'StockArticle#destroy', + stock_article_id: <%= @stock_article.id %> +}); diff --git a/app/views/stockit/destroy.js.haml b/app/views/stockit/destroy.js.haml deleted file mode 100644 index 8bbde9edf..000000000 --- a/app/views/stockit/destroy.js.haml +++ /dev/null @@ -1,8 +0,0 @@ --# please polish the following line if you know how, same in partial _destroy_fail -var successDiv = $('
#{escape_javascript(t('ui.marks.close').html_safe)}
'); - -successDiv.append(document.createTextNode('#{escape_javascript(t('.notice', name: @article.name))}')); -$('div.container-fluid').prepend(successDiv); - -$('#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/edit.html.haml b/app/views/stockit/edit.html.haml deleted file mode 100644 index bdf498c61..000000000 --- a/app/views/stockit/edit.html.haml +++ /dev/null @@ -1,3 +0,0 @@ -- title t('.title') - -= render :partial => 'form', :locals => {:stock_article => @stock_article} diff --git a/app/views/stockit/edit.js.erb b/app/views/stockit/edit.js.erb new file mode 100644 index 000000000..e3bc33217 --- /dev/null +++ b/app/views/stockit/edit.js.erb @@ -0,0 +1,9 @@ +$('#modalContainer').html('<%= j(render( + :partial => "form", + :locals => { + :title => t('.title'), + :stock_article => @stock_article + } +)) %>'); + +$('#modalContainer').modal(); diff --git a/app/views/stockit/index.html.haml b/app/views/stockit/index.html.haml index 477e5816f..a1f49c8d0 100644 --- a/app/views/stockit/index.html.haml +++ b/app/views/stockit/index.html.haml @@ -3,6 +3,28 @@ :javascript $(function() { $('tr.unavailable').hide(); + + $('body').on('StockArticle#create', function(e) { + $.ajax({ + url: '#{on_stock_article_create_stock_articles_path}', + type: 'get', + data: {id: e.stock_article_id}, + contentType: 'application/json; charset=UTF-8' + }); + }); + + $('body').on('StockArticle#destroy', function(e) { + $('#stockArticle-' + e.stock_article_id).remove(); + }); + + $('body').on('StockArticle#update', function(e) { + $.ajax({ + url: '#{on_stock_article_update_stock_articles_path}', + type: 'get', + data: {id: e.stock_article_id}, + contentType: 'application/json; charset=UTF-8' + }); + }); }) .well.well-small @@ -17,7 +39,7 @@ .btn-group = link_to_if @current_user.role_orders?, t('.order_online'), new_order_path(supplier_id: 0), class: 'btn', class: 'btn btn-primary' - = link_to t('.new_stock_article'), new_stock_article_path, class: 'btn' + = link_to t('.new_stock_article'), new_stock_article_path, class: 'btn', remote: true = link_to t('.new_stock_taking'), new_stock_taking_path, class: 'btn' = link_to t('.show_stock_takings'), stock_takings_path, class: 'btn' @@ -42,23 +64,9 @@ %th= t '.article.supplier' %th= t '.article.category' %th - %tbody - - for article in @stock_articles - %tr{:class => stock_article_classes(article), :id => "stockArticle-#{article.id}"} - %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 t('ui.edit'), edit_stock_article_path(article), class: 'btn btn-mini' - = link_to t('ui.history'), stock_article_history_path(article), class: 'btn btn-mini' - = link_to t('ui.delete'), article, :method => :delete, :confirm => t('.confirm_delete'), - class: 'btn btn-mini btn-danger', :remote => true + %tbody#articles-tbody + - for stock_article in @stock_articles + = render :partial => 'stock_article', :locals => {:stock_article => stock_article} %p = t '.stock_worth' = number_to_currency StockArticle.stock_value diff --git a/app/views/stockit/new.html.haml b/app/views/stockit/new.html.haml deleted file mode 100644 index 05609a84f..000000000 --- a/app/views/stockit/new.html.haml +++ /dev/null @@ -1,22 +0,0 @@ -- title t('.title') - -- content_for :head do - :javascript - $(function() { - $('#article_search').autocomplete({ - source: '#{articles_search_stock_articles_path}', - select: function(e, ui) { - alert(ui.item.value); - //location.href = '#{nil}' + ui.item.value; - } - }); - }) - - -/ - TODO: Fix this - %p - = t '.search_text' - = text_field_tag 'article_search' -#stock_article_form - = render :partial => 'form', :locals => {:stock_article => @stock_article} diff --git a/app/views/stockit/new.js.erb b/app/views/stockit/new.js.erb new file mode 100644 index 000000000..e3bc33217 --- /dev/null +++ b/app/views/stockit/new.js.erb @@ -0,0 +1,9 @@ +$('#modalContainer').html('<%= j(render( + :partial => "form", + :locals => { + :title => t('.title'), + :stock_article => @stock_article + } +)) %>'); + +$('#modalContainer').modal(); diff --git a/app/views/stockit/on_stock_article_create.js.erb b/app/views/stockit/on_stock_article_create.js.erb new file mode 100644 index 000000000..284a4f85e --- /dev/null +++ b/app/views/stockit/on_stock_article_create.js.erb @@ -0,0 +1,10 @@ +(function() { + var stock_article_row = $('<%= j(render( + :partial => 'stock_article', + :locals => { + :stock_article => @stock_article + } + )) %>'); + + $('#articles-tbody').prepend(stock_article_row); +})(); diff --git a/app/views/stockit/on_stock_article_update.js.erb b/app/views/stockit/on_stock_article_update.js.erb new file mode 100644 index 000000000..06a71552a --- /dev/null +++ b/app/views/stockit/on_stock_article_update.js.erb @@ -0,0 +1,10 @@ +(function() { + var stock_article_row = $('<%= j(render( + :partial => 'stock_article', + :locals => { + :stock_article => @stock_article + } + )) %>'); + + $('#stockArticle-<%= @stock_article.id %>').replaceWith(stock_article_row); +})(); diff --git a/app/views/stockit/update.js.erb b/app/views/stockit/update.js.erb new file mode 100644 index 000000000..3b32b19a6 --- /dev/null +++ b/app/views/stockit/update.js.erb @@ -0,0 +1,13 @@ +$('div.container-fluid').prepend('<%= j(render( + :partial => 'shared/alert_success', + :locals => { + :alert_message => t('.notice', :name => @stock_article.name) + } +)) %>'); + +$('body').trigger({ + type: 'StockArticle#update', + stock_article_id: <%= @stock_article.id %> +}); + +$('#modalContainer').modal('hide'); diff --git a/config/locales/de.yml b/config/locales/de.yml index a0a0e3a79..ff977fc23 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -431,9 +431,7 @@ de: add_stock_change: how_many_units: Wie viele Einheiten (%{unit}) des Artikels »%{name}« liefern? create: - notice: Lieferung wurde erstellt. Bitte nicht vergessen die Rechnung anzulegen! - create_stock_article: - notice: Neuer Lagerartikel »%{name}« gespeichert. + notice: Lieferung wurde erstellt. Bitte nicht vergessen die Rechnung anzulegen!stocki destroy: notice: Lieferung wurde gelöscht. edit: @@ -473,15 +471,11 @@ de: action_add_to_delivery: Liefern action_edit: Bearbeiten action_other_price: Kopieren - stock_article_form: - copy_stock_article: Lagerartikel kopieren stock_change_fields: remove_article: Artikel aus Lieferung entfernen suppliers_overview: Lieferantenübersicht update: notice: Lieferung wurde aktualisiert. - update_stock_article: - notice: Lagerartikel »%{name}« aktualisiert. documents: order_by_articles: filename: Bestellung %{name}-%{date} - Artikelsortierung @@ -1541,9 +1535,9 @@ de: units_to_order: Wenn Du die Gesamtanzahl gelieferter Gebinde änderst, musst Du auch die individuelle Anzahl der einzelnen Bestellgruppen anpassen, indem Du auf den Artikelnamen klickst. Sie werden nicht automatisch neuberechnet und andernfalls werden den Bestellgruppen Artikel in Rechnung gestellt, die nicht geliefert wurden! update_current_price: Ändert auch den Preis für aktuelle Bestellungen stock_article: - copy_stock_article: + copy: name: Bitte ändern - edit_stock_article: + edit: price: supplier: supplier: @@ -1725,11 +1719,16 @@ de: stockit: check: not_empty: ! '%{name} kann nicht gelöscht werden. Der Lagerbestand ist nicht null.' + copy: + title: 'Lagerartikel kopieren' + create: + notice: Neuer Lagerartikel »%{name}« gespeichert. destroy: notice: Artikel %{name} gelöscht. edit: title: Lagerartikel bearbeiten form: + copy_stock_article: Lagerartikel kopieren price_hint: Um Chaos zu vermeiden können bis auf weiteres die Preise von angelegten Lagerartikeln nicht mehr verändert werden. history: change_quantity: Veränderung @@ -1751,7 +1750,6 @@ de: supplier: Lieferantin unit: Einheit vat: MwSt - confirm_delete: Bist Du sicher? new_delivery: Neue Lieferung .. new_stock_article: Neuen Lagerartikel anlegen new_stock_taking: Inventur anlegen @@ -1764,11 +1762,15 @@ de: view_options: Ansichtsoptionen new: search_text: ! 'Suche nache Artikeln aus allen Katalogen:' - title: Neuen Lagerartikel anlegen + title: Lagerartikel anlegen + stock_article: + confirm_delete: Willst Du wirklich den Lagerartikel »%{name}« löschen? stock_create: notice: Lagerartikel wurde gespeichert. stock_update: notice: Lagerartikel wurde gespeichert. + update: + notice: Lagerartikel »%{name}« aktualisiert. suppliers: create: notice: Lieferant wurde erstellt @@ -1892,6 +1894,7 @@ de: history: Verlauf anzeigen marks: close: ! '×' + error: success: or_cancel: oder abbrechen please_wait: Bitte warten... diff --git a/config/routes.rb b/config/routes.rb index 0f96d7fa4..a1dfdde2f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -93,9 +93,15 @@ end resources :stock_articles, :to => 'stockit' do + get :copy collection do get :articles_search get :fill_new_stock_article_form + + get :derive + + get :on_stock_article_create + get :on_stock_article_update end get :history @@ -105,15 +111,11 @@ get :shared_suppliers, :on => :collection resources :deliveries do - post :add_stock_change, :on => :collection - - get :new_stock_article, :on => :collection - get :copy_stock_article, :on => :collection - get :derive_stock_article, :on => :collection - post :create_stock_article, :on => :collection - - get :edit_stock_article, :on => :collection - put :update_stock_article, :on => :collection + collection do + post :add_stock_change + get :on_stock_article_create + get :on_stock_article_update + end end resources :articles do From 1e552be26a432caa0bfdd835dfdb34b1e827d66e Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 3 Oct 2013 14:55:46 +0200 Subject: [PATCH 02/11] Fix creation of StockArticles from Articles --- app/views/stockit/derive.js.erb | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 app/views/stockit/derive.js.erb diff --git a/app/views/stockit/derive.js.erb b/app/views/stockit/derive.js.erb new file mode 100644 index 000000000..e3bc33217 --- /dev/null +++ b/app/views/stockit/derive.js.erb @@ -0,0 +1,9 @@ +$('#modalContainer').html('<%= j(render( + :partial => "form", + :locals => { + :title => t('.title'), + :stock_article => @stock_article + } +)) %>'); + +$('#modalContainer').modal(); From fb861bd1f2323cdee485724dd43a6c29075b96b8 Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 3 Oct 2013 14:56:39 +0200 Subject: [PATCH 03/11] Remove redundant StockArticles#create_stock_article action --- app/controllers/stockit_controller.rb | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index 24434e5f5..5286fa5a3 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -81,16 +81,6 @@ def history @stock_changes = @stock_article.stock_changes.order('stock_changes.created_at DESC').each {|s| s.readonly!} end - def create_stock_article - @stock_article = StockArticle.new(params[:stock_article]) - - if @stock_article.valid? and @stock_article.save - render :layout => false - else - render :action => 'new_stock_article', :layout => false - end - end - def on_stock_article_create @stock_article = StockArticle.find(params[:id]) From 21489513c7cec90693c8125a5ce599bd84b05a2c Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 3 Oct 2013 15:00:35 +0200 Subject: [PATCH 04/11] Revert "Fix migrate problem caused by output formating(?)" This reverts commit 53f24cd885169d4bb0adf2253257fdaabc3dd054. --- db/migrate/20130702113610_update_group_order_totals.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/migrate/20130702113610_update_group_order_totals.rb b/db/migrate/20130702113610_update_group_order_totals.rb index f0052ee5a..ca9aab672 100644 --- a/db/migrate/20130702113610_update_group_order_totals.rb +++ b/db/migrate/20130702113610_update_group_order_totals.rb @@ -7,7 +7,7 @@ def self.up say "If you do want to update the ordergroup totals, open the rails console " + "(by running `rails c`), and enter:" - say "GroupOrder.all.each { |go| go.order.closed? and go.update_price! }" + say "GroupOrder.all.each { |go| go.order.closed? and go.update_price! }", subitem: true say "You may want to check first that no undesired accounting issues are introduced. " + "It may be wise to discuss this with those responsible for the ordering finances." From 756cf466d65bf05a3287821626c9b946abb4318d Mon Sep 17 00:00:00 2001 From: Julius Date: Tue, 8 Oct 2013 19:19:54 +0200 Subject: [PATCH 05/11] AJAX edit of StockArticles in stockit/show view --- app/controllers/stockit_controller.rb | 6 +++ .../stockit/_stock_article_details.html.haml | 26 +++++++++++++ app/views/stockit/show.html.haml | 38 +++++++------------ .../show_on_stock_article_update.js.erb | 11 ++++++ config/routes.rb | 2 + 5 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 app/views/stockit/_stock_article_details.html.haml create mode 100644 app/views/stockit/show_on_stock_article_update.js.erb diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index 264ae86aa..a8faaebab 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -56,6 +56,12 @@ def show @stock_changes = @stock_article.stock_changes.order('stock_changes.created_at DESC') end + def show_on_stock_article_update + @stock_article = StockArticle.find(params[:id]) + + render :layout => false + end + def destroy @stock_article = StockArticle.find(params[:id]) @stock_article.mark_as_deleted diff --git a/app/views/stockit/_stock_article_details.html.haml b/app/views/stockit/_stock_article_details.html.haml new file mode 100644 index 000000000..63dd07b89 --- /dev/null +++ b/app/views/stockit/_stock_article_details.html.haml @@ -0,0 +1,26 @@ +#stockArticleDetails + %dl.dl-horizontal + %dt= StockArticle.human_attribute_name 'supplier' + %dd= link_to stock_article.supplier.name, stock_article.supplier + %dt= StockArticle.human_attribute_name 'name' + %dd= stock_article.name + %dt= StockArticle.human_attribute_name 'unit' + %dd= stock_article.unit + %dt= StockArticle.human_attribute_name 'price' + %dd= number_to_currency stock_article.price + %dt= StockArticle.human_attribute_name 'tax' + %dd= number_to_percentage stock_article.tax + %dt= StockArticle.human_attribute_name 'deposit' + %dd= number_to_currency stock_article.deposit + %dt= StockArticle.human_attribute_name 'fc_price' + %dd= number_to_currency stock_article.fc_price + %dt= StockArticle.human_attribute_name 'article_category' + %dd= stock_article.article_category.name + %dt= StockArticle.human_attribute_name 'note' + %dd= stock_article.note + %dt= StockArticle.human_attribute_name 'quantity' + %dd= stock_article.quantity + %dt= StockArticle.human_attribute_name 'quantity_available' + %dd= stock_article.quantity_available + .form-actions + = link_to t('ui.edit'), edit_stock_article_path(stock_article), remote: true, class: 'btn' diff --git a/app/views/stockit/show.html.haml b/app/views/stockit/show.html.haml index 5f416bf93..936405283 100644 --- a/app/views/stockit/show.html.haml +++ b/app/views/stockit/show.html.haml @@ -1,32 +1,20 @@ - title @stock_article.name +- content_for :javascript do + :javascript + $(function() { + $('body').on('StockArticle#update', function(e) { + $.ajax({ + url: '#{show_on_stock_article_update_stock_articles_path}', + type: 'get', + data: {id: e.stock_article_id}, + contentType: 'application/json; charset=UTF-8' + }); + }); + }); .row-fluid .span6 - %dl.dl-horizontal - %dt= StockArticle.human_attribute_name 'supplier' - %dd= link_to @stock_article.supplier.name, @stock_article.supplier - %dt= StockArticle.human_attribute_name 'name' - %dd= @stock_article.name - %dt= StockArticle.human_attribute_name 'unit' - %dd= @stock_article.unit - %dt= StockArticle.human_attribute_name 'price' - %dd= number_to_currency @stock_article.price - %dt= StockArticle.human_attribute_name 'tax' - %dd= number_to_percentage @stock_article.tax - %dt= StockArticle.human_attribute_name 'deposit' - %dd= number_to_currency @stock_article.deposit - %dt= StockArticle.human_attribute_name 'fc_price' - %dd= number_to_currency @stock_article.fc_price - %dt= StockArticle.human_attribute_name 'article_category' - %dd= @stock_article.article_category.name - %dt= StockArticle.human_attribute_name 'note' - %dd= @stock_article.note - %dt= StockArticle.human_attribute_name 'quantity' - %dd= @stock_article.quantity - %dt= StockArticle.human_attribute_name 'quantity_available' - %dd= @stock_article.quantity_available - .form-actions - = link_to t('ui.edit'), edit_stock_article_path(@stock_article), class: 'btn' + = render :partial => 'stock_article_details', :locals => {:stock_article => @stock_article} .span6 %h2= t('.stock_changes') diff --git a/app/views/stockit/show_on_stock_article_update.js.erb b/app/views/stockit/show_on_stock_article_update.js.erb new file mode 100644 index 000000000..6ae801c9f --- /dev/null +++ b/app/views/stockit/show_on_stock_article_update.js.erb @@ -0,0 +1,11 @@ +(function() { + var stock_article_details = $('<%= j(render( + :partial => 'stock_article_details', + :locals => { + :stock_article => @stock_article + } + )) %>'); + + $('#stockArticleDetails').replaceWith(stock_article_details); + $('h1').first().text('<%= j(@stock_article.name) %>'); +})(); diff --git a/config/routes.rb b/config/routes.rb index 9746a271b..7ef1a3428 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -102,6 +102,8 @@ get :on_stock_article_create get :on_stock_article_update + + get :show_on_stock_article_update end end From f59a930b9cc8f33cd3a15debebd469871c0d5296 Mon Sep 17 00:00:00 2001 From: Julius Date: Tue, 8 Oct 2013 19:24:40 +0200 Subject: [PATCH 06/11] Use consistent names for new '..._on_stock_article_...' routes --- app/controllers/deliveries_controller.rb | 4 ++-- app/controllers/stockit_controller.rb | 24 +++++++++---------- app/views/deliveries/_form.html.haml | 4 ++-- ...rb => form_on_stock_article_create.js.erb} | 0 ...rb => form_on_stock_article_update.js.erb} | 0 app/views/stockit/index.html.haml | 4 ++-- ...b => index_on_stock_article_create.js.erb} | 0 ...b => index_on_stock_article_update.js.erb} | 0 config/routes.rb | 8 +++---- 9 files changed, 22 insertions(+), 22 deletions(-) rename app/views/deliveries/{on_stock_article_create.js.erb => form_on_stock_article_create.js.erb} (100%) rename app/views/deliveries/{on_stock_article_update.js.erb => form_on_stock_article_update.js.erb} (100%) rename app/views/stockit/{on_stock_article_create.js.erb => index_on_stock_article_create.js.erb} (100%) rename app/views/stockit/{on_stock_article_update.js.erb => index_on_stock_article_update.js.erb} (100%) diff --git a/app/controllers/deliveries_controller.rb b/app/controllers/deliveries_controller.rb index 0016a29fd..f4335bc7e 100644 --- a/app/controllers/deliveries_controller.rb +++ b/app/controllers/deliveries_controller.rb @@ -56,13 +56,13 @@ def add_stock_change render :layout => false end - def on_stock_article_create + def form_on_stock_article_create @stock_article = StockArticle.find(params[:id]) render :layout => false end - def on_stock_article_update + def form_on_stock_article_update @stock_article = StockArticle.find(params[:id]) render :layout => false diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index a8faaebab..7ce6796a2 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -5,6 +5,18 @@ def index order('suppliers.name, article_categories.name, articles.name') end + def index_on_stock_article_create + @stock_article = StockArticle.find(params[:id]) + + render :layout => false + end + + def index_on_stock_article_update + @stock_article = StockArticle.find(params[:id]) + + render :layout => false + end + # three possibilites to fill a new_stock_article form # (1) start from blank or use params def new @@ -86,16 +98,4 @@ def fill_new_stock_article_form render :partial => 'form', :locals => {:stock_article => stock_article} end - - def on_stock_article_create - @stock_article = StockArticle.find(params[:id]) - - render :layout => false - end - - def on_stock_article_update - @stock_article = StockArticle.find(params[:id]) - - render :layout => false - end end diff --git a/app/views/deliveries/_form.html.haml b/app/views/deliveries/_form.html.haml index ad60b6e45..ad681208a 100644 --- a/app/views/deliveries/_form.html.haml +++ b/app/views/deliveries/_form.html.haml @@ -66,7 +66,7 @@ $('body').on('StockArticle#create', function(e) { $.ajax({ - url: '#{on_stock_article_create_supplier_deliveries_path(@supplier)}', + url: '#{form_on_stock_article_create_supplier_deliveries_path(@supplier)}', type: 'get', data: {id: e.stock_article_id}, contentType: 'application/json; charset=UTF-8' @@ -75,7 +75,7 @@ $('body').on('StockArticle#update', function(e) { $.ajax({ - url: '#{on_stock_article_update_supplier_deliveries_path(@supplier)}', + url: '#{form_on_stock_article_update_supplier_deliveries_path(@supplier)}', type: 'get', data: {id: e.stock_article_id}, contentType: 'application/json; charset=UTF-8' diff --git a/app/views/deliveries/on_stock_article_create.js.erb b/app/views/deliveries/form_on_stock_article_create.js.erb similarity index 100% rename from app/views/deliveries/on_stock_article_create.js.erb rename to app/views/deliveries/form_on_stock_article_create.js.erb diff --git a/app/views/deliveries/on_stock_article_update.js.erb b/app/views/deliveries/form_on_stock_article_update.js.erb similarity index 100% rename from app/views/deliveries/on_stock_article_update.js.erb rename to app/views/deliveries/form_on_stock_article_update.js.erb diff --git a/app/views/stockit/index.html.haml b/app/views/stockit/index.html.haml index a1f49c8d0..2470f5c1f 100644 --- a/app/views/stockit/index.html.haml +++ b/app/views/stockit/index.html.haml @@ -6,7 +6,7 @@ $('body').on('StockArticle#create', function(e) { $.ajax({ - url: '#{on_stock_article_create_stock_articles_path}', + url: '#{index_on_stock_article_create_stock_articles_path}', type: 'get', data: {id: e.stock_article_id}, contentType: 'application/json; charset=UTF-8' @@ -19,7 +19,7 @@ $('body').on('StockArticle#update', function(e) { $.ajax({ - url: '#{on_stock_article_update_stock_articles_path}', + url: '#{index_on_stock_article_update_stock_articles_path}', type: 'get', data: {id: e.stock_article_id}, contentType: 'application/json; charset=UTF-8' diff --git a/app/views/stockit/on_stock_article_create.js.erb b/app/views/stockit/index_on_stock_article_create.js.erb similarity index 100% rename from app/views/stockit/on_stock_article_create.js.erb rename to app/views/stockit/index_on_stock_article_create.js.erb diff --git a/app/views/stockit/on_stock_article_update.js.erb b/app/views/stockit/index_on_stock_article_update.js.erb similarity index 100% rename from app/views/stockit/on_stock_article_update.js.erb rename to app/views/stockit/index_on_stock_article_update.js.erb diff --git a/config/routes.rb b/config/routes.rb index 7ef1a3428..2cfd56682 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -100,8 +100,8 @@ get :derive - get :on_stock_article_create - get :on_stock_article_update + get :index_on_stock_article_create + get :index_on_stock_article_update get :show_on_stock_article_update end @@ -113,8 +113,8 @@ resources :deliveries do collection do post :add_stock_change - get :on_stock_article_create - get :on_stock_article_update + get :form_on_stock_article_create + get :form_on_stock_article_update end end From 2e8e3dda3963ff6b4a66d50d0fc4161cef60462c Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 31 Oct 2013 21:34:13 +0100 Subject: [PATCH 07/11] Optimize merge result --- app/views/deliveries/form_on_stock_article_create.js.erb | 1 - app/views/shared/_article_price_info.html.haml | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/views/deliveries/form_on_stock_article_create.js.erb b/app/views/deliveries/form_on_stock_article_create.js.erb index b46583a49..44c56df9d 100644 --- a/app/views/deliveries/form_on_stock_article_create.js.erb +++ b/app/views/deliveries/form_on_stock_article_create.js.erb @@ -7,7 +7,6 @@ :article => @stock_article } )) %>').addClass('success'); - enablePriceTooltips(stock_article_for_adding); $('#stock_articles_for_adding tbody').append(stock_article_for_adding); updateSort('#stock_articles_for_adding'); diff --git a/app/views/shared/_article_price_info.html.haml b/app/views/shared/_article_price_info.html.haml index 00e25a3a5..f6c38f2e7 100644 --- a/app/views/shared/_article_price_info.html.haml +++ b/app/views/shared/_article_price_info.html.haml @@ -10,7 +10,7 @@ %td.numeric= number_to_percentage article.tax - unless article.fc_price == article.gross_price %tr - %th= t 'activerecord.attributes.article.fc_share' + %th= t 'activerecord.attributes.article.fc_share_short' %td.numeric= number_to_currency(article.fc_price-article.gross_price) %tr %th= t 'activerecord.attributes.article.fc_price' From f59a7b58c66e47c15090d2d404fde9bcc645fc65 Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 31 Oct 2013 21:46:54 +0100 Subject: [PATCH 08/11] Use $(document) instead of $('body') for publish/subscribe messages --- app/views/deliveries/_form.html.haml | 4 ++-- app/views/stockit/create.js.erb | 2 +- app/views/stockit/destroy.js.erb | 2 +- app/views/stockit/index.html.haml | 6 +++--- app/views/stockit/show.html.haml | 2 +- app/views/stockit/update.js.erb | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/views/deliveries/_form.html.haml b/app/views/deliveries/_form.html.haml index 50e6bb77a..4268fef72 100644 --- a/app/views/deliveries/_form.html.haml +++ b/app/views/deliveries/_form.html.haml @@ -62,7 +62,7 @@ } }); - $('body').on('StockArticle#create', function(e) { + $(document).on('StockArticle#create', function(e) { $.ajax({ url: '#{form_on_stock_article_create_supplier_deliveries_path(@supplier)}', type: 'get', @@ -71,7 +71,7 @@ }); }); - $('body').on('StockArticle#update', function(e) { + $(document).on('StockArticle#update', function(e) { $.ajax({ url: '#{form_on_stock_article_update_supplier_deliveries_path(@supplier)}', type: 'get', diff --git a/app/views/stockit/create.js.erb b/app/views/stockit/create.js.erb index c6defcf81..8b9f3d731 100644 --- a/app/views/stockit/create.js.erb +++ b/app/views/stockit/create.js.erb @@ -5,7 +5,7 @@ $('div.container-fluid').prepend('<%= j(render( } )) %>'); -$('body').trigger({ +$(document).trigger({ type: 'StockArticle#create', stock_article_id: <%= @stock_article.id %> }); diff --git a/app/views/stockit/destroy.js.erb b/app/views/stockit/destroy.js.erb index fa44b47ac..3bd2174d4 100644 --- a/app/views/stockit/destroy.js.erb +++ b/app/views/stockit/destroy.js.erb @@ -5,7 +5,7 @@ $('div.container-fluid').prepend('<%= j(render( } )) %>'); -$('body').trigger({ +$(document).trigger({ type: 'StockArticle#destroy', stock_article_id: <%= @stock_article.id %> }); diff --git a/app/views/stockit/index.html.haml b/app/views/stockit/index.html.haml index 2470f5c1f..fb52cd599 100644 --- a/app/views/stockit/index.html.haml +++ b/app/views/stockit/index.html.haml @@ -4,7 +4,7 @@ $(function() { $('tr.unavailable').hide(); - $('body').on('StockArticle#create', function(e) { + $(document).on('StockArticle#create', function(e) { $.ajax({ url: '#{index_on_stock_article_create_stock_articles_path}', type: 'get', @@ -13,11 +13,11 @@ }); }); - $('body').on('StockArticle#destroy', function(e) { + $(document).on('StockArticle#destroy', function(e) { $('#stockArticle-' + e.stock_article_id).remove(); }); - $('body').on('StockArticle#update', function(e) { + $(document).on('StockArticle#update', function(e) { $.ajax({ url: '#{index_on_stock_article_update_stock_articles_path}', type: 'get', diff --git a/app/views/stockit/show.html.haml b/app/views/stockit/show.html.haml index 936405283..d075c682b 100644 --- a/app/views/stockit/show.html.haml +++ b/app/views/stockit/show.html.haml @@ -2,7 +2,7 @@ - content_for :javascript do :javascript $(function() { - $('body').on('StockArticle#update', function(e) { + $(document).on('StockArticle#update', function(e) { $.ajax({ url: '#{show_on_stock_article_update_stock_articles_path}', type: 'get', diff --git a/app/views/stockit/update.js.erb b/app/views/stockit/update.js.erb index 3b32b19a6..3a2214875 100644 --- a/app/views/stockit/update.js.erb +++ b/app/views/stockit/update.js.erb @@ -5,7 +5,7 @@ $('div.container-fluid').prepend('<%= j(render( } )) %>'); -$('body').trigger({ +$(document).trigger({ type: 'StockArticle#update', stock_article_id: <%= @stock_article.id %> }); From 2704f4e614948df9c194a17aa7f650d4d7bbc932 Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 31 Oct 2013 22:57:33 +0100 Subject: [PATCH 09/11] Comment on publish/subscribe pattern in source code --- app/views/deliveries/_form.html.haml | 10 ++++++++++ app/views/stockit/create.js.erb | 13 +++++++++++++ app/views/stockit/destroy.js.erb | 13 +++++++++++++ app/views/stockit/index.html.haml | 15 +++++++++++++++ app/views/stockit/show.html.haml | 16 ++++++++++++++++ app/views/stockit/update.js.erb | 13 +++++++++++++ 6 files changed, 80 insertions(+) diff --git a/app/views/deliveries/_form.html.haml b/app/views/deliveries/_form.html.haml index 4268fef72..ae0343617 100644 --- a/app/views/deliveries/_form.html.haml +++ b/app/views/deliveries/_form.html.haml @@ -62,6 +62,16 @@ } }); + #{########################################################################## + # Publish/subscribe pattern for loose coupling of + # (1) AJAX database manipulation and + # (2) DOM updates for the particular view + ############################################################################ + # * THIS STARTS STEP (2) + # * subscribe to document's events which are published/triggered by step (1) + # * possibly launch a second AJAX request to retreive the source code of the + # new DOM elements + } $(document).on('StockArticle#create', function(e) { $.ajax({ url: '#{form_on_stock_article_create_supplier_deliveries_path(@supplier)}', diff --git a/app/views/stockit/create.js.erb b/app/views/stockit/create.js.erb index 8b9f3d731..d86b0d4ff 100644 --- a/app/views/stockit/create.js.erb +++ b/app/views/stockit/create.js.erb @@ -5,6 +5,19 @@ $('div.container-fluid').prepend('<%= j(render( } )) %>'); + <%########################################################################## + # Publish/subscribe pattern for loose coupling of + # (1) AJAX database manipulation and + # (2) DOM updates for the particular view + ############################################################################ + # * THIS FINISHES STEP (2) + # * publish/trigger event(s) which reflect(s) the database manipulation + # * attach enough information to the event to allow for a proper response of + # the views in step (2) + # + # WARNING: remember not to attach secret information (e.g. full JSON of + # database entries) - these events are dispatched in the user's browser! + %> $(document).trigger({ type: 'StockArticle#create', stock_article_id: <%= @stock_article.id %> diff --git a/app/views/stockit/destroy.js.erb b/app/views/stockit/destroy.js.erb index 3bd2174d4..0203bc83a 100644 --- a/app/views/stockit/destroy.js.erb +++ b/app/views/stockit/destroy.js.erb @@ -5,6 +5,19 @@ $('div.container-fluid').prepend('<%= j(render( } )) %>'); + <%########################################################################## + # Publish/subscribe pattern for loose coupling of + # (1) AJAX database manipulation and + # (2) DOM updates for the particular view + ############################################################################ + # * THIS FINISHES STEP (2) + # * publish/trigger event(s) which reflect(s) the database manipulation + # * attach enough information to the event to allow for a proper response of + # the views in step (2) + # + # WARNING: remember not to attach secret information (e.g. full JSON of + # database entries) - these events are dispatched in the user's browser! + %> $(document).trigger({ type: 'StockArticle#destroy', stock_article_id: <%= @stock_article.id %> diff --git a/app/views/stockit/index.html.haml b/app/views/stockit/index.html.haml index fb52cd599..4efb45deb 100644 --- a/app/views/stockit/index.html.haml +++ b/app/views/stockit/index.html.haml @@ -4,6 +4,21 @@ $(function() { $('tr.unavailable').hide(); + #{########################################################################## + # Publish/subscribe pattern for loose coupling of + # (1) AJAX database manipulation and + # (2) DOM updates for the particular view + ############################################################################ + # * THIS STARTS STEP (2) + # * subscribe to document's events which are published/triggered by step (1) + # * possibly launch a second AJAX request to retreive the source code of the + # new DOM elements + # + # NOTE: This is a special case because the same controller (stockit) is + # responsible for steps (1) and (2). However, there are other views which + # employ the functionality of step (1) in this controller and provide their + # own step (2) for DOM updates (by listening on the corresponding events). + } $(document).on('StockArticle#create', function(e) { $.ajax({ url: '#{index_on_stock_article_create_stock_articles_path}', diff --git a/app/views/stockit/show.html.haml b/app/views/stockit/show.html.haml index d075c682b..a1b34c356 100644 --- a/app/views/stockit/show.html.haml +++ b/app/views/stockit/show.html.haml @@ -2,6 +2,22 @@ - content_for :javascript do :javascript $(function() { + + #{########################################################################## + # Publish/subscribe pattern for loose coupling of + # (1) AJAX database manipulation and + # (2) DOM updates for the particular view + ############################################################################ + # * THIS STARTS STEP (2) + # * subscribe to document's events which are published/triggered by step (1) + # * possibly launch a second AJAX request to retreive the source code of the + # new DOM elements + # + # NOTE: This is a special case because the same controller (stockit) is + # responsible for steps (1) and (2). However, there are other views which + # employ the functionality of step (1) in this controller and provide their + # own step (2) for DOM updates (by listening on the corresponding events). + } $(document).on('StockArticle#update', function(e) { $.ajax({ url: '#{show_on_stock_article_update_stock_articles_path}', diff --git a/app/views/stockit/update.js.erb b/app/views/stockit/update.js.erb index 3a2214875..c69c2d676 100644 --- a/app/views/stockit/update.js.erb +++ b/app/views/stockit/update.js.erb @@ -5,6 +5,19 @@ $('div.container-fluid').prepend('<%= j(render( } )) %>'); + <%########################################################################## + # Publish/subscribe pattern for loose coupling of + # (1) AJAX database manipulation and + # (2) DOM updates for the particular view + ############################################################################ + # * THIS FINISHES STEP (2) + # * publish/trigger event(s) which reflect(s) the database manipulation + # * attach enough information to the event to allow for a proper response of + # the views in step (2) + # + # WARNING: remember not to attach secret information (e.g. full JSON of + # database entries) - these events are dispatched in the user's browser! + %> $(document).trigger({ type: 'StockArticle#update', stock_article_id: <%= @stock_article.id %> From 08be821c81d7b56a8170fb025fc4fd6e14e0e4a2 Mon Sep 17 00:00:00 2001 From: Julius Date: Thu, 31 Oct 2013 23:03:35 +0100 Subject: [PATCH 10/11] Fix typo in comment --- app/views/stockit/create.js.erb | 2 +- app/views/stockit/destroy.js.erb | 2 +- app/views/stockit/update.js.erb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/stockit/create.js.erb b/app/views/stockit/create.js.erb index d86b0d4ff..4268140fd 100644 --- a/app/views/stockit/create.js.erb +++ b/app/views/stockit/create.js.erb @@ -10,7 +10,7 @@ $('div.container-fluid').prepend('<%= j(render( # (1) AJAX database manipulation and # (2) DOM updates for the particular view ############################################################################ - # * THIS FINISHES STEP (2) + # * THIS FINISHES STEP (1) # * publish/trigger event(s) which reflect(s) the database manipulation # * attach enough information to the event to allow for a proper response of # the views in step (2) diff --git a/app/views/stockit/destroy.js.erb b/app/views/stockit/destroy.js.erb index 0203bc83a..804c70c20 100644 --- a/app/views/stockit/destroy.js.erb +++ b/app/views/stockit/destroy.js.erb @@ -10,7 +10,7 @@ $('div.container-fluid').prepend('<%= j(render( # (1) AJAX database manipulation and # (2) DOM updates for the particular view ############################################################################ - # * THIS FINISHES STEP (2) + # * THIS FINISHES STEP (1) # * publish/trigger event(s) which reflect(s) the database manipulation # * attach enough information to the event to allow for a proper response of # the views in step (2) diff --git a/app/views/stockit/update.js.erb b/app/views/stockit/update.js.erb index c69c2d676..7e4ee738c 100644 --- a/app/views/stockit/update.js.erb +++ b/app/views/stockit/update.js.erb @@ -10,7 +10,7 @@ $('div.container-fluid').prepend('<%= j(render( # (1) AJAX database manipulation and # (2) DOM updates for the particular view ############################################################################ - # * THIS FINISHES STEP (2) + # * THIS FINISHES STEP (1) # * publish/trigger event(s) which reflect(s) the database manipulation # * attach enough information to the event to allow for a proper response of # the views in step (2) From e56dd0533e119268345d5a45c83ff3048b14726f Mon Sep 17 00:00:00 2001 From: Julius Date: Fri, 1 Nov 2013 10:18:20 +0100 Subject: [PATCH 11/11] Use heading_helper for price info and StockArticle#show --- .../shared/_article_price_info.html.haml | 10 ++++----- .../stockit/_stock_article_details.html.haml | 22 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/views/shared/_article_price_info.html.haml b/app/views/shared/_article_price_info.html.haml index f6c38f2e7..67afc4d8e 100644 --- a/app/views/shared/_article_price_info.html.haml +++ b/app/views/shared/_article_price_info.html.haml @@ -1,17 +1,17 @@ %table.table.table-condensed %tr - %th= t 'activerecord.attributes.article.price' + %th= heading_helper(Article, :price) %td.numeric= number_to_currency article.price %tr - %th= t 'activerecord.attributes.article.deposit' + %th= heading_helper(Article, :deposit) %td.numeric= number_to_currency article.deposit %tr - %th= t 'activerecord.attributes.article.tax' + %th= heading_helper(Article, :tax) %td.numeric= number_to_percentage article.tax - unless article.fc_price == article.gross_price %tr - %th= t 'activerecord.attributes.article.fc_share_short' + %th= heading_helper(Article, :fc_share, {:short => true}) %td.numeric= number_to_currency(article.fc_price-article.gross_price) %tr - %th= t 'activerecord.attributes.article.fc_price' + %th= heading_helper(Article, :fc_price) %td.numeric= number_to_currency article.fc_price diff --git a/app/views/stockit/_stock_article_details.html.haml b/app/views/stockit/_stock_article_details.html.haml index 63dd07b89..617799e7f 100644 --- a/app/views/stockit/_stock_article_details.html.haml +++ b/app/views/stockit/_stock_article_details.html.haml @@ -1,26 +1,26 @@ #stockArticleDetails %dl.dl-horizontal - %dt= StockArticle.human_attribute_name 'supplier' + %dt= heading_helper(StockArticle, :supplier) %dd= link_to stock_article.supplier.name, stock_article.supplier - %dt= StockArticle.human_attribute_name 'name' + %dt= heading_helper(StockArticle, :name) %dd= stock_article.name - %dt= StockArticle.human_attribute_name 'unit' + %dt= heading_helper(StockArticle, :unit) %dd= stock_article.unit - %dt= StockArticle.human_attribute_name 'price' + %dt= heading_helper(StockArticle, :price) %dd= number_to_currency stock_article.price - %dt= StockArticle.human_attribute_name 'tax' + %dt= heading_helper(StockArticle, :tax) %dd= number_to_percentage stock_article.tax - %dt= StockArticle.human_attribute_name 'deposit' + %dt= heading_helper(StockArticle, :deposit) %dd= number_to_currency stock_article.deposit - %dt= StockArticle.human_attribute_name 'fc_price' + %dt= heading_helper(StockArticle, :fc_price) %dd= number_to_currency stock_article.fc_price - %dt= StockArticle.human_attribute_name 'article_category' + %dt= heading_helper(StockArticle, :article_category) %dd= stock_article.article_category.name - %dt= StockArticle.human_attribute_name 'note' + %dt= heading_helper(StockArticle, :note) %dd= stock_article.note - %dt= StockArticle.human_attribute_name 'quantity' + %dt= heading_helper(StockArticle, :quantity) %dd= stock_article.quantity - %dt= StockArticle.human_attribute_name 'quantity_available' + %dt= heading_helper(StockArticle, :quantity_available) %dd= stock_article.quantity_available .form-actions = link_to t('ui.edit'), edit_stock_article_path(stock_article), remote: true, class: 'btn'