Use OrderArticle#create in receive form instead of Order#add_article

This commit is contained in:
Julius 2013-12-31 13:46:25 +01:00 committed by wvengen
parent 59c118a171
commit 033aa90698
6 changed files with 36 additions and 55 deletions

View file

@ -16,7 +16,7 @@ class OrderArticlesController < ApplicationController
# given mentioning that the article already exists, which is desired. # given mentioning that the article already exists, which is desired.
@order_article = @order.order_articles.where(:article_id => params[:order_article][:article_id]).first @order_article = @order.order_articles.where(:article_id => params[:order_article][:article_id]).first
if @order_article and @order_article.units_to_order == 0 if @order_article and @order_article.units_to_order == 0
@order_article.units_to_order = 1 @order_article.units_to_order = 1 # FIXME: this is ugly if used in the receive form
else else
@order_article = OrderArticle.new(params[:order_article]) @order_article = OrderArticle.new(params[:order_article])
end end

View file

@ -106,17 +106,6 @@ class OrdersController < ApplicationController
redirect_to orders_url, alert: I18n.t('errors.general_msg', :msg => error.message) redirect_to orders_url, alert: I18n.t('errors.general_msg', :msg => error.message)
end end
# ajax add article
def add_article
@order = Order.find(params[:id])
@order_article = @order.order_articles.where(:article_id => params[:article_id]).includes(:article).first
# we need to create the order article if it's not part of the current order
if @order_article.nil?
@order_article = @order.order_articles.build({order: @order, article_id: params[:article_id]})
@order_article.save!
end
end
def receive def receive
@order = Order.find(params[:id]) @order = Order.find(params[:id])
unless request.post? unless request.post?
@ -127,6 +116,12 @@ class OrdersController < ApplicationController
end end
end end
def receive_on_order_article_create # See publish/subscribe design pattern in /doc.
@order_article = OrderArticle.find(params[:order_article_id])
render :layout => false
end
def receive_on_order_article_update # See publish/subscribe design pattern in /doc. def receive_on_order_article_update # See publish/subscribe design pattern in /doc.
@order_article = OrderArticle.find(params[:order_article_id]) @order_article = OrderArticle.find(params[:order_article_id])

View file

@ -1,10 +1,6 @@
- new_articles = (@order.supplier.articles rescue @order.articles)
- new_article_data = articles_for_select2(new_articles, @order_articles.map(&:article_id)) {|a| "#{a.name} (#{a.unit_quantity}#{a.unit})" }
- content_for :javascript do - content_for :javascript do
:javascript :javascript
var new_article_data = #{new_article_data.to_json};
function update_delta(input) { function update_delta(input) {
var units = $(input).val(); var units = $(input).val();
var expected = $(input).data('units-expected'); var expected = $(input).data('units-expected');
@ -35,45 +31,13 @@
$('input[data-units-expected]').each(function() { $('input[data-units-expected]').each(function() {
update_delta(this); update_delta(this);
}); });
init_add_article('#add_article');
}); });
function init_add_article(sel) { %table#order_articles.ordered-articles.table.table-striped.stupidtable
$(sel).removeAttr('disabled').select2({
placeholder: '#{t 'orders.add_article.title'}',
data: new_article_data,
// TODO implement adding a new article, like in deliveries
}).on('change', function(e) {
var selectedArticle = $(e.currentTarget).select2('data');
if(!selectedArticle) {
return false;
}
$.ajax({
url: '#{add_article_order_path(@order)}',
type: 'get',
data: {article_id: selectedArticle.id},
contentType: 'application/json; charset=UTF-8'
});
// clear selection
$('#add_article').select2('data', null);
// remove newly added item from list of items
new_article_data = $.grep(new_article_data, function(el, i) {
return el.id != selectedArticle.id;
});
init_add_article(sel)
return true;
});
}
%table.ordered-articles.table.table-striped.stupidtable
%thead %thead
%tr %tr
%th.sort{:data => {:sort => 'string'}}= heading_helper Article, :order_number, short: true %th.sort{:data => {:sort => 'string'}}= heading_helper Article, :order_number, short: true
%th.sort{:data => {:sort => 'string'}}= heading_helper Article, :name %th.default-sort.sort{:data => {:sort => 'string'}}= heading_helper Article, :name
%th= heading_helper Article, :unit %th= heading_helper Article, :unit
%th Members %th Members
%th Ordered %th Ordered
@ -83,11 +47,11 @@
%th= heading_helper ArticlePrice, :price %th= heading_helper ArticlePrice, :price
%th %th
%th= t 'ui.actions' %th= t 'ui.actions'
%tfoot
%tr
%th{:colspan => 10}
= link_to t('.add_article'), new_order_order_article_path(@order), remote: true, class: 'btn btn-small'
%tbody#result_table %tbody#result_table
- @order_articles.each do |order_article| - @order_articles.each do |order_article|
= render :partial => 'edit_amount', :locals => {:order_article => order_article} = render :partial => 'edit_amount', :locals => {:order_article => order_article}
%tfoot
%tr
%th{:colspan => 8}
%input#add_article{:style => 'width: 500px;'}

View file

@ -11,6 +11,15 @@
contentType: 'application/json; charset=UTF-8' contentType: 'application/json; charset=UTF-8'
}); });
}); });
$(document).on('OrderArticle#create', function(e) {
$.ajax({
url: '#{receive_on_order_article_create_order_path(@order)}',
type: 'get',
data: {order_article_id: e.order_article_id},
contentType: 'application/json; charset=UTF-8'
});
});
}); });
- title "Receiving #{@order.name}" - title "Receiving #{@order.name}"

View file

@ -0,0 +1,13 @@
// Handle more advanced DOM update after AJAX database manipulation.
// See publish/subscribe design pattern in /doc.
(function(w) {
$('#order_articles tr').removeClass('success');
var order_article_entry = $(
'<%= j render(partial: 'edit_amount', locals: {order_article: @order_article}) %>'
).addClass('success');
$('#order_articles tbody').append(order_article_entry);
updateSort('#order_articles');
})(window);

View file

@ -41,8 +41,8 @@ Foodsoft::Application.routes.draw do
get :receive get :receive
post :receive post :receive
get :add_article
get :receive_on_order_article_create
get :receive_on_order_article_update get :receive_on_order_article_update
end end