Apply publish/subscribe pattern to stock_article manipulation in delivery form

This commit is contained in:
Julius 2013-12-07 16:42:25 +01:00
parent 13bb4e0a8b
commit bceabfec5c
13 changed files with 57 additions and 127 deletions

View file

@ -50,57 +50,22 @@ class DeliveriesController < ApplicationController
redirect_to supplier_deliveries_url(@supplier) redirect_to supplier_deliveries_url(@supplier)
end 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
render :layout => false
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 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])
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 def add_stock_change
@stock_change = StockChange.new @stock_change = StockChange.new
@stock_change.stock_article = StockArticle.find(params[:stock_article_id]) @stock_change.stock_article = StockArticle.find(params[:stock_article_id])
render :layout => false render :layout => false
end end
def form_on_stock_article_create # See publish/subscribe design pattern in /doc.
@stock_article = StockArticle.find(params[:id])
render :layout => false
end
def form_on_stock_article_update # See publish/subscribe design pattern in /doc.
@stock_article = StockArticle.find(params[:id])
render :layout => false
end
end end

View file

@ -25,13 +25,4 @@ module DeliveriesHelper
return output.html_safe return output.html_safe
end 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 end

View file

@ -42,7 +42,7 @@
} }
if('new' == selectedArticle.id) { if('new' == selectedArticle.id) {
$.ajax({ $.ajax({
url: '#{new_stock_article_supplier_deliveries_path(@supplier)}', url: '#{new_stock_article_path}',
type: 'get', type: 'get',
data: {stock_article: {name: selectedArticle.text}}, data: {stock_article: {name: selectedArticle.text}},
contentType: 'application/json; charset=UTF-8' contentType: 'application/json; charset=UTF-8'
@ -52,7 +52,7 @@
} }
if('' != selectedArticle.id) { if('' != selectedArticle.id) {
$.ajax({ $.ajax({
url: '#{derive_stock_article_supplier_deliveries_path(@supplier)}', url: '#{derive_stock_articles_path}',
type: 'get', type: 'get',
data: {old_article_id: selectedArticle.id}, data: {old_article_id: selectedArticle.id},
contentType: 'application/json; charset=UTF-8' contentType: 'application/json; charset=UTF-8'
@ -61,6 +61,26 @@
return true; return true;
} }
}); });
// Subscribe to database changes.
// See publish/subscribe design pattern in /doc.
$(document).on('StockArticle#create', function(e) {
$.ajax({
url: '#{form_on_stock_article_create_supplier_deliveries_path(@supplier)}',
type: 'get',
data: {id: e.stock_article_id},
contentType: 'application/json; charset=UTF-8'
});
});
$(document).on('StockArticle#update', function(e) {
$.ajax({
url: '#{form_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) { function mark_article_for_delivery(stock_article_id) {
@ -96,7 +116,7 @@
%tr %tr
%th{:colspan => 5} %th{:colspan => 5}
- if articles_for_select2(@supplier).empty? - 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, :remote => true, :class => 'btn'
- else - else
%input#new_stock_article{:style => 'width: 500px;'} %input#new_stock_article{:style => 'width: 500px;'}
%tbody %tbody

View file

@ -5,7 +5,7 @@
%td= article.unit %td= article.unit
%td= article.article_category.name %td= article.article_category.name
%td %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_edit'), edit_stock_article_path(article), 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_other_price'), stock_article_copy_path(article), remote: true, class: 'btn btn-mini'
- deliver_button_disabled = ( @delivery and @delivery.includes_article? article ) ? ( 'disabled' ) : ( false ) - 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 = 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

View file

@ -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).html_safe
= 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')

View file

@ -1,5 +0,0 @@
$('#modalContainer').html(
'<%= j(render(:partial => "stock_article_form", :locals => {:stock_article => @stock_article})) %>'
);
$('#modalContainer').modal();

View file

@ -1,5 +0,0 @@
$('#modalContainer').html(
'<%= j(render(:partial => "stock_article_form", :locals => {:stock_article => @stock_article})) %>'
);
$('#modalContainer').modal();

View file

@ -1,5 +0,0 @@
$('#modalContainer').html(
'<%= j(render(:partial => "stock_article_form", :locals => {:stock_article => @stock_article})) %>'
);
$('#modalContainer').modal();

View file

@ -1,7 +1,5 @@
$('div.container-fluid').prepend( // Handle more advanced DOM update after AJAX database manipulation.
'<%= j(render(:partial => 'shared/alert_success', :locals => {:alert_message => t('.notice', :name => @stock_article.name)})) %>' // See publish/subscribe design pattern in /doc.
);
(function() { (function() {
$('#stock_articles_for_adding tr').removeClass('success'); $('#stock_articles_for_adding tr').removeClass('success');
@ -12,5 +10,3 @@ $('div.container-fluid').prepend(
$('#stock_articles_for_adding tbody').append(stock_article_for_adding); $('#stock_articles_for_adding tbody').append(stock_article_for_adding);
updateSort('#stock_articles_for_adding'); updateSort('#stock_articles_for_adding');
})(); })();
$('#modalContainer').modal('hide');

View file

@ -1,7 +1,5 @@
$('div.container-fluid').prepend( // Handle more advanced DOM update after AJAX database manipulation.
'<%= j(render(:partial => 'shared/alert_success', :locals => {:alert_message => t('.notice', :name => @stock_article.name)})) %>' // See publish/subscribe design pattern in /doc.
);
(function() { (function() {
// update entry in stock_article table // update entry in stock_article table
@ -28,5 +26,3 @@ $('div.container-fluid').prepend(
updateSort('#stock_changes'); updateSort('#stock_changes');
})(); })();
$('#modalContainer').modal('hide');

View file

@ -1,5 +0,0 @@
$('#modalContainer').html(
'<%= j(render(:partial => "stock_article_form", :locals => {:stock_article => @stock_article})) %>'
);
$('#modalContainer').modal();

View file

@ -0,0 +1,9 @@
$('#modalContainer').html('<%= j(render(
:partial => "form",
:locals => {
:title => t('.title'),
:stock_article => @stock_article
}
)) %>');
$('#modalContainer').modal();

View file

@ -88,8 +88,7 @@ Foodsoft::Application.routes.draw do
resources :stock_articles, :to => 'stockit' do resources :stock_articles, :to => 'stockit' do
get :copy get :copy
collection do collection do
get :articles_search get :derive
get :fill_new_stock_article_form
get :index_on_stock_article_create get :index_on_stock_article_create
get :index_on_stock_article_update get :index_on_stock_article_update
@ -102,15 +101,12 @@ Foodsoft::Application.routes.draw do
get :shared_suppliers, :on => :collection get :shared_suppliers, :on => :collection
resources :deliveries do resources :deliveries do
post :add_stock_change, :on => :collection collection do
post :add_stock_change
get :new_stock_article, :on => :collection
get :copy_stock_article, :on => :collection get :form_on_stock_article_create
get :derive_stock_article, :on => :collection get :form_on_stock_article_update
post :create_stock_article, :on => :collection end
get :edit_stock_article, :on => :collection
put :update_stock_article, :on => :collection
end end
resources :articles do resources :articles do