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
|
|
@ -10,6 +10,7 @@ class Delivery < ActiveRecord::Base
|
|||
scope :recent, :order => 'created_at DESC', :limit => 10
|
||||
|
||||
validates_presence_of :supplier_id, :delivered_on
|
||||
validate :stock_articles_must_be_unique
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue