From 81dfe8110c1812fa7865365e48d3ed3a5400c93a Mon Sep 17 00:00:00 2001 From: Julius Date: Mon, 30 Dec 2013 13:09:49 +0100 Subject: [PATCH] Apply publish/subscribe for OrderArticle#update --- app/controllers/finance/balancing_controller.rb | 6 ++++++ app/views/finance/balancing/new.html.haml | 15 +++++++++++++++ .../balancing/new_on_order_article_update.js.erb | 14 ++++++++++++++ app/views/finance/order_articles/update.js.erb | 9 +++++++++ app/views/finance/order_articles/update.js.haml | 4 ---- config/routes.rb | 2 ++ 6 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 app/views/finance/balancing/new_on_order_article_update.js.erb create mode 100644 app/views/finance/order_articles/update.js.erb delete mode 100644 app/views/finance/order_articles/update.js.haml diff --git a/app/controllers/finance/balancing_controller.rb b/app/controllers/finance/balancing_controller.rb index 8de1444c..a0f354b2 100644 --- a/app/controllers/finance/balancing_controller.rb +++ b/app/controllers/finance/balancing_controller.rb @@ -29,6 +29,12 @@ class Finance::BalancingController < Finance::BaseController render layout: false if request.xhr? end + + def new_on_order_article_update # See publish/subscribe design pattern in /doc. + @order_article = OrderArticle.find(params[:order_article_id]) + + render :layout => false + end def update_summary @order = Order.find(params[:id]) diff --git a/app/views/finance/balancing/new.html.haml b/app/views/finance/balancing/new.html.haml index b8f08129..52828947 100644 --- a/app/views/finance/balancing/new.html.haml +++ b/app/views/finance/balancing/new.html.haml @@ -1,3 +1,18 @@ +- content_for :javascript do + :javascript + $(function() { + // Subscribe to database changes. + // See publish/subscribe design pattern in /doc. + $(document).on('OrderArticle#update', function(e) { + $.ajax({ + url: '#{new_on_order_article_update_finance_order_path(@order)}', + type: 'get', + data: {order_article_id: e.order_article_id}, + contentType: 'application/json; charset=UTF-8' + }); + }); + }); + - title t('.title', name: @order.name) - content_for :sidebar do diff --git a/app/views/finance/balancing/new_on_order_article_update.js.erb b/app/views/finance/balancing/new_on_order_article_update.js.erb new file mode 100644 index 00000000..a3fe6c27 --- /dev/null +++ b/app/views/finance/balancing/new_on_order_article_update.js.erb @@ -0,0 +1,14 @@ +// Handle more advanced DOM update after AJAX database manipulation. +// See publish/subscribe design pattern in /doc. +(function(w) { + $('#order_article_<%= @order_article.id %>').html( + '<%= j render('finance/balancing/order_article', order_article: @order_article) %>' + ); + + $('#group_order_articles_<%= @order_article.id %>').html( + '<%= j render('finance/balancing/group_order_articles', order_article: @order_article) %>' + ); + + $('#summaryChangedWarning').show(); +})(window); + diff --git a/app/views/finance/order_articles/update.js.erb b/app/views/finance/order_articles/update.js.erb new file mode 100644 index 00000000..4c7b7a68 --- /dev/null +++ b/app/views/finance/order_articles/update.js.erb @@ -0,0 +1,9 @@ +// Publish database changes. +// See publish/subscribe design pattern in /doc. +$(document).trigger({ + type: 'OrderArticle#update', + order_article_id: <%= @order_article.id %> +}); + +$('#modalContainer').modal('hide'); + diff --git a/app/views/finance/order_articles/update.js.haml b/app/views/finance/order_articles/update.js.haml deleted file mode 100644 index 36e66ccd..00000000 --- a/app/views/finance/order_articles/update.js.haml +++ /dev/null @@ -1,4 +0,0 @@ -$('#modalContainer').modal('hide'); -$('#order_article_#{@order_article.id}').html('#{j(render('finance/balancing/order_article', order_article: @order_article))}'); -$('#group_order_articles_#{@order_article.id}').html('#{j(render('finance/balancing/group_order_articles', order_article: @order_article))}'); -$('#summaryChangedWarning').show(); \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 9d9d508c..2249c654 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -145,6 +145,8 @@ Foodsoft::Application.routes.draw do get :confirm put :close put :close_direct + + get :new_on_order_article_update end resources :order_articles