diff --git a/app/models/group.rb b/app/models/group.rb index c1976830..68cb015c 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -4,7 +4,7 @@ class Group < ActiveRecord::Base has_many :memberships, :dependent => :destroy has_many :users, :through => :memberships - validates :name, :presence => true, :length => {:in => 1..25}, :uniqueness => true + validates :name, :presence => true, :length => {:in => 1..25} attr_reader :user_tokens diff --git a/app/models/ordergroup.rb b/app/models/ordergroup.rb index 3d863834..dbfe3171 100644 --- a/app/models/ordergroup.rb +++ b/app/models/ordergroup.rb @@ -16,7 +16,7 @@ class Ordergroup < Group has_many :orders, :through => :group_orders validates_numericality_of :account_balance, :message => 'ist keine gültige Zahl' - validate :uniqueness_of_members + validate :uniqueness_of_name, :uniqueness_of_members after_create :update_stats! @@ -106,6 +106,16 @@ class Ordergroup < Group errors.add :user_tokens, "#{user.nick} ist schon in einer anderen Bestellgruppe" if user.groups.where(:type => 'Ordergroup').size > 1 end end + + # Make sure, the name is uniq, add usefull message if uniq group is already deleted + def uniqueness_of_name + id = new_record? ? '' : self.id + group = Ordergroup.with_deleted.where('groups.id != ? AND groups.name = ?', id, name).first + if group.present? + message = group.deleted? ? :taken_with_deleted : :taken + errors.add :name, message + end + end end diff --git a/app/models/supplier.rb b/app/models/supplier.rb index 05dbeb6c..384c5973 100644 --- a/app/models/supplier.rb +++ b/app/models/supplier.rb @@ -12,15 +12,13 @@ class Supplier < ActiveRecord::Base attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number, :delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity - validates :name, :presence => true, :length => { :in => 4..30 }, :uniqueness => true + validates :name, :presence => true, :length => { :in => 4..30 } validates :phone, :presence => true, :length => { :in => 8..20 } validates :address, :presence => true, :length => { :in => 8..50 } validates_length_of :order_howto, :note, maximum: 250 -# validates_length_of :name, :in => 4..30 -# validates_uniqueness_of :name - validates_length_of :phone, :in => 8..20 validates_length_of :address, :in => 8..50 + validate :uniqueness_of_name # sync all articles with the external database # returns an array with articles(and prices), which should be updated (to use in a form) @@ -66,5 +64,17 @@ class Supplier < ActiveRecord::Base end return [updated_articles, outlisted_articles] end + + protected + + # Make sure, the name is uniq, add usefull message if uniq group is already deleted + def uniqueness_of_name + id = new_record? ? '' : self.id + supplier = Supplier.with_deleted.where('suppliers.id != ? AND suppliers.name = ?', id, name).first + if supplier.present? + message = supplier.deleted? ? :taken_with_deleted : :taken + errors.add :name, message + end + end end diff --git a/app/models/workgroup.rb b/app/models/workgroup.rb index 5b201932..20448839 100644 --- a/app/models/workgroup.rb +++ b/app/models/workgroup.rb @@ -5,6 +5,7 @@ class Workgroup < Group # returns all non-finished tasks has_many :open_tasks, :class_name => 'Task', :conditions => ['done = ?', false], :order => 'due_date ASC' + validates_uniqueness_of :name validates_presence_of :task_name, :weekday, :task_required_users, :next_weekly_tasks_number, :if => :weekly_task validates_numericality_of :next_weekly_tasks_number, :greater_than => 0, :less_than => 21, :only_integer => true, diff --git a/config/locales/de.yml b/config/locales/de.yml index 213c3c97..6f6d108a 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -143,6 +143,7 @@ de: odd: muss ungerade sein record_invalid: ! 'Gültigkeitsprüfung ist fehlgeschlagen: %{errors}' taken: ist bereits vergeben + taken_with_deleted: ist bereits vergeben (eine gelöschte Gruppe) too_long: ist zu lang (nicht mehr als %{count} Zeichen) too_short: ist zu kurz (nicht weniger als %{count} Zeichen) wrong_length: hat die falsche Länge (muss genau %{count} Zeichen haben)