diff --git a/app/controllers/stock_takings_controller.rb b/app/controllers/stock_takings_controller.rb index 8d45e0fd..bdf1dc77 100644 --- a/app/controllers/stock_takings_controller.rb +++ b/app/controllers/stock_takings_controller.rb @@ -10,6 +10,13 @@ class StockTakingsController < ApplicationController StockArticle.undeleted.each { |a| @stock_taking.stock_changes.build(:stock_article => a) } end + def new_on_stock_article_create # See publish/subscribe design pattern in /doc. + stock_article = StockArticle.find(params[:stock_article_id]) + @stock_change = StockChange.new(:stock_article => stock_article) + + render :layout => false + end + def create create!(:notice => I18n.t('stock_takings.create.notice')) end @@ -17,39 +24,4 @@ class StockTakingsController < ApplicationController def update update!(:notice => I18n.t('stock_takings.update.notice')) end - - def fill_new_stock_article_form - article = Article.find(params[:article_id]) - supplier = article.supplier - stock_article = supplier.stock_articles.build( - article.attributes.reject { |attr| attr == ('id' || 'type')} - ) - - render :partial => 'stock_article_form', :locals => {:stock_article => stock_article} - end - - def add_stock_article - article = StockArticle.new(params[:stock_article]) - render :update do |page| - if article.save - page.insert_html :top, 'stock_changes', :partial => 'stock_change', - :locals => {:stock_change => article.stock_changes.build} - - page.replace_html 'new_stock_article', :partial => 'stock_article_form', - :locals => {:stock_article => StockArticle.new} - else - page.replace_html 'new_stock_article', :partial => 'stock_article_form', - :locals => {:stock_article => article} - end - end - end - - def drop_stock_change - stock_change = StockChange.find(params[:stock_change_id]) - stock_change.destroy - - render :update do |page| - page.visual_effect :DropOut, "stock_change_#{stock_change.id}" - end - end end diff --git a/app/controllers/stockit_controller.rb b/app/controllers/stockit_controller.rb index b9a82b65..d04d866b 100644 --- a/app/controllers/stockit_controller.rb +++ b/app/controllers/stockit_controller.rb @@ -86,14 +86,4 @@ class StockitController < ApplicationController @articles = Article.not_in_stock.limit(8).where('name LIKE ?', "%#{params[:term]}%") render :json => @articles.map(&:name) end - - def fill_new_stock_article_form - article = Article.find(params[:article_id]) - @supplier = article.supplier - stock_article = @supplier.stock_articles.build( - article.attributes.reject { |attr| attr == ('id' || 'type')} - ) - - render :partial => 'form', :locals => {:stock_article => stock_article} - end end diff --git a/app/views/stock_takings/_stock_article_form.html.haml b/app/views/stock_takings/_stock_article_form.html.haml deleted file mode 100644 index 9e7ec14b..00000000 --- a/app/views/stock_takings/_stock_article_form.html.haml +++ /dev/null @@ -1,13 +0,0 @@ -- simple_form_for stock_article, url: add_stock_article_stock_takings_path, remote: true do |f| - = f.association :supplier - = f.input :name - = f.input :unit - = f.input :note - = f.input :price - = f.input :tax, :wrapper => :append do - = f.input_field :tax - %span.add-on % - -# untested, because this view is currently not included (?) - = f.input :deposit - = f.association :article_category - = f.submit diff --git a/app/views/stock_takings/edit.html.haml b/app/views/stock_takings/edit.html.haml index 66f5752d..ec8554fc 100644 --- a/app/views/stock_takings/edit.html.haml +++ b/app/views/stock_takings/edit.html.haml @@ -1,6 +1,6 @@ - title t('.title') -- simple_form_for(@stock_taking) do |f| += simple_form_for(@stock_taking) do |f| = f.input :date = f.input :note = f.submit diff --git a/app/views/stock_takings/new.html.haml b/app/views/stock_takings/new.html.haml index 481ffdc6..692609b6 100644 --- a/app/views/stock_takings/new.html.haml +++ b/app/views/stock_takings/new.html.haml @@ -1,9 +1,23 @@ - title t('.title') +- content_for :javascript do + :javascript + $(function() { + // Subscribe to database changes. + // See publish/subscribe design pattern in /doc. + $(document).on('StockArticle#create', function(e) { + $.ajax({ + url: '#{new_on_stock_article_create_stock_takings_path}', + type: 'get', + data: {stock_article_id: e.stock_article_id}, + contentType: 'application/json; charset=UTF-8' + }); + }); + }); - content_for :sidebar do %p %i= t('.text_deviations', inv_link: link_to(t('.temp_inventory'), stock_articles_path)).html_safe - %p= t('.text_need_articles', create_link: link_to(t('.create'), new_stock_article_path)).html_safe + %p= t('.text_need_articles', create_link: link_to(t('.create'), new_stock_article_path, :remote => true)).html_safe = simple_form_for(@stock_taking) do |f| = f.input :date, as: :date_picker diff --git a/app/views/stock_takings/new_on_stock_article_create.js.erb b/app/views/stock_takings/new_on_stock_article_create.js.erb new file mode 100644 index 00000000..8584356a --- /dev/null +++ b/app/views/stock_takings/new_on_stock_article_create.js.erb @@ -0,0 +1,14 @@ +// Handle more advanced DOM update after AJAX database manipulation. +// See publish/subscribe design pattern in /doc. +(function() { + $('#stock_changes p').removeClass('alert alert-success'); + + var stock_change = $('<%= j(render( + :partial => 'stock_change', + :locals => { + :stock_change => @stock_change + } + )) %>').addClass('alert alert-success'); + + $('#stock_changes').prepend(stock_change); +})(); diff --git a/config/routes.rb b/config/routes.rb index 9e3cad02..b718b636 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -96,8 +96,7 @@ Foodsoft::Application.routes.draw do resources :stock_takings do collection do - get :fill_new_stock_article_form - post :add_stock_article + get :new_on_stock_article_create end end