Improve delivery validation: StockArticles must not be associated more than once
This commit is contained in:
parent
5df4c8f8a6
commit
2ee7f716ae
7 changed files with 55 additions and 25 deletions
|
@ -159,4 +159,12 @@ module ApplicationHelper
|
||||||
flash_messages.join("\n").html_safe
|
flash_messages.join("\n").html_safe
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# render base errors in a form after failed validation
|
||||||
|
# http://railsapps.github.io/twitter-bootstrap-rails.html
|
||||||
|
def base_errors resource
|
||||||
|
return '' if (resource.errors.empty?) or (resource.errors[:base].empty?)
|
||||||
|
messages = resource.errors[:base].map { |msg| content_tag(:li, msg) }.join
|
||||||
|
render :partial => 'shared/base_errors', :locals => {:error_messages => messages}
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,6 +10,7 @@ class Delivery < ActiveRecord::Base
|
||||||
scope :recent, :order => 'created_at DESC', :limit => 10
|
scope :recent, :order => 'created_at DESC', :limit => 10
|
||||||
|
|
||||||
validates_presence_of :supplier_id, :delivered_on
|
validates_presence_of :supplier_id, :delivered_on
|
||||||
|
validate :stock_articles_must_be_unique
|
||||||
|
|
||||||
accepts_nested_attributes_for :stock_changes, :allow_destroy => :true
|
accepts_nested_attributes_for :stock_changes, :allow_destroy => :true
|
||||||
|
|
||||||
|
@ -23,6 +24,14 @@ class Delivery < ActiveRecord::Base
|
||||||
self.stock_changes.map{|stock_change| stock_change.stock_article.id}.include? article.id
|
self.stock_changes.map{|stock_change| stock_change.stock_article.id}.include? article.id
|
||||||
end
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def stock_articles_must_be_unique
|
||||||
|
unless stock_changes.reject{|sc| sc.marked_for_destruction?}.map {|sc| sc.stock_article.id}.uniq!.nil?
|
||||||
|
errors.add(:base, I18n.t('model.delivery.each_stock_article_must_be_unique'))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6,14 +6,15 @@
|
||||||
|
|
||||||
var stock_change = $(this).closest('tr');
|
var stock_change = $(this).closest('tr');
|
||||||
stock_change.hide(); // do not remove (to ensure destruction)
|
stock_change.hide(); // do not remove (to ensure destruction)
|
||||||
unmark_article_unavailable_for_delivery( stock_change.data('id') );
|
stock_change.removeAttr('id'); // remove id to allow re-adding
|
||||||
|
mark_article_for_delivery( stock_change.data('id') );
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
$('.remove_new_stock_change').live('click', function() {
|
$('.remove_new_stock_change').live('click', function() {
|
||||||
var stock_change = $(this).closest('tr');
|
var stock_change = $(this).closest('tr');
|
||||||
stock_change.remove();
|
stock_change.remove();
|
||||||
unmark_article_unavailable_for_delivery( stock_change.data('id') );
|
mark_article_for_delivery( stock_change.data('id') );
|
||||||
return false;
|
return false;
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -62,23 +63,26 @@
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
function mark_article_unavailable_for_delivery(stock_article_id) {
|
function mark_article_for_delivery(stock_article_id) {
|
||||||
var articleTr = $('#stock_article_' + stock_article_id);
|
var articleTr = $('#stock_article_' + stock_article_id);
|
||||||
articleTr.addClass('unavailable');
|
if( is_article_available_for_delivery(stock_article_id) ) {
|
||||||
$('.button-add-stock-change', articleTr).attr('disabled', 'disabled');
|
articleTr.removeClass('unavailable');
|
||||||
|
$('.button-add-stock-change', articleTr).removeAttr('disabled');
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
articleTr.addClass('unavailable');
|
||||||
|
$('.button-add-stock-change', articleTr).attr('disabled', 'disabled');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
function unmark_article_unavailable_for_delivery(stock_article_id) {
|
function is_article_available_for_delivery(stock_article_id) {
|
||||||
var articleTr = $('#stock_article_' + stock_article_id);
|
return ( 0 == $('#stock_change_stock_article_' + stock_article_id).length );
|
||||||
articleTr.removeClass('unavailable');
|
|
||||||
$('.button-add-stock-change', articleTr).removeAttr('disabled');
|
|
||||||
}
|
|
||||||
function is_article_unavailable_for_delivery(stock_article_id) {
|
|
||||||
var articleTr = $('#stock_article_' + stock_article_id);
|
|
||||||
return articleTr.hasClass('unavailable');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
= simple_form_for [@supplier, @delivery], validate: true do |f|
|
= simple_form_for [@supplier, @delivery], validate: true do |f|
|
||||||
= f.hidden_field :supplier_id
|
= f.error_notification
|
||||||
|
= base_errors f.object
|
||||||
|
= f.association :supplier, :as => :hidden
|
||||||
|
|
||||||
%h2= t '.title_select_stock_articles'
|
%h2= t '.title_select_stock_articles'
|
||||||
.well.well-small
|
.well.well-small
|
||||||
.btn-toolbar
|
.btn-toolbar
|
||||||
|
|
|
@ -1,13 +1,5 @@
|
||||||
(function(w) {
|
(function(w) {
|
||||||
if(is_article_unavailable_for_delivery(<%= @stock_change.stock_article.id %>)) {
|
if(!is_article_available_for_delivery(<%= @stock_change.stock_article.id %>)) {
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
mark_article_unavailable_for_delivery(<%= @stock_change.stock_article.id %>);
|
|
||||||
|
|
||||||
var quantity = w.prompt('<%= j(t('.how_many_units', :unit => @stock_change.stock_article.unit, :name => @stock_change.stock_article.name)) %>'); <%# how to properly escape here? %>
|
|
||||||
if(null === quantity) {
|
|
||||||
unmark_article_unavailable_for_delivery(<%= @stock_change.stock_article.id %>); // think wisely before changing this: What about double clicks on "deliver" button?
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,8 +9,16 @@
|
||||||
'<%= j(render(:partial => 'stock_change', :locals => {:stock_change => @stock_change})) %>'
|
'<%= j(render(:partial => 'stock_change', :locals => {:stock_change => @stock_change})) %>'
|
||||||
).addClass('success');
|
).addClass('success');
|
||||||
|
|
||||||
|
$('#stock_changes').append(stock_change);
|
||||||
|
mark_article_for_delivery(<%= @stock_change.stock_article.id %>);
|
||||||
|
updateSort('#stock_changes');
|
||||||
|
|
||||||
|
var quantity = w.prompt('<%= j(t('.how_many_units', :unit => @stock_change.stock_article.unit, :name => @stock_change.stock_article.name)) %>'); <%# how to properly escape here? %>
|
||||||
|
if(null === quantity) {
|
||||||
|
stock_change.remove();
|
||||||
|
mark_article_for_delivery(<%= @stock_change.stock_article.id %>);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
$('input.stock-change-quantity', stock_change).val(quantity);
|
$('input.stock-change-quantity', stock_change).val(quantity);
|
||||||
|
|
||||||
$('#stock_changes').append(stock_change);
|
|
||||||
updateSort('#stock_changes');
|
|
||||||
})(window);
|
})(window);
|
||||||
|
|
5
app/views/shared/_base_errors.haml
Normal file
5
app/views/shared/_base_errors.haml
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
.alert.alert-error.alert-block
|
||||||
|
%a.close{:href => '#', :data => {:dismiss => 'alert'}}
|
||||||
|
= t('ui.marks.close').html_safe
|
||||||
|
%ul
|
||||||
|
= error_messages.html_safe
|
|
@ -1163,6 +1163,8 @@ de:
|
||||||
subject: ! 'Betreff:'
|
subject: ! 'Betreff:'
|
||||||
title: Nachricht anzeigen
|
title: Nachricht anzeigen
|
||||||
model:
|
model:
|
||||||
|
delivery:
|
||||||
|
each_stock_article_must_be_unique: Lieferung darf jeden Lagerartikel höchstens einmal auflisten.
|
||||||
membership:
|
membership:
|
||||||
no_admin_delete: Mitgliedschaft kann nicht beendet werden. Du bist die letzte Administratorin
|
no_admin_delete: Mitgliedschaft kann nicht beendet werden. Du bist die letzte Administratorin
|
||||||
order_article:
|
order_article:
|
||||||
|
|
|
@ -1166,6 +1166,8 @@ en:
|
||||||
subject: ! 'Subject:'
|
subject: ! 'Subject:'
|
||||||
title: Show message
|
title: Show message
|
||||||
model:
|
model:
|
||||||
|
delivery:
|
||||||
|
each_stock_article_must_be_unique: Each stock article must not be listed more than once.
|
||||||
membership:
|
membership:
|
||||||
no_admin_delete: Membership can not be withdrawn as you are the last administrator.
|
no_admin_delete: Membership can not be withdrawn as you are the last administrator.
|
||||||
order_article:
|
order_article:
|
||||||
|
|
Loading…
Reference in a new issue