From 3fb5c313ee30d563b445512b451ed0b72575f5ed Mon Sep 17 00:00:00 2001 From: Benjamin Meichsner Date: Sun, 24 Feb 2013 23:26:16 +0100 Subject: [PATCH] Show usefull error message for uniqueness_of_name with acts_as_paranoid. Fixed #88 Conflicts: app/models/ordergroup.rb --- app/models/group.rb | 2 +- app/models/ordergroup.rb | 12 +++++++++++- app/models/supplier.rb | 18 ++++++++++++++---- app/models/workgroup.rb | 1 + config/locales/de/de.defaults.yml | 1 + config/locales/en/en.defaults.yml | 1 + 6 files changed, 29 insertions(+), 6 deletions(-) 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 5378cc35..06baa614 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 => I18n.t('ordergroups.model.invalid_balance') - 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, I18n.t('ordergroups.model.error_single_group', :user => user.nick) 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 d154fe3e..21023a43 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/de.defaults.yml b/config/locales/de/de.defaults.yml index 8768d3d0..4e9565e5 100644 --- a/config/locales/de/de.defaults.yml +++ b/config/locales/de/de.defaults.yml @@ -142,6 +142,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) diff --git a/config/locales/en/en.defaults.yml b/config/locales/en/en.defaults.yml index 3d421ad1..a65188e8 100644 --- a/config/locales/en/en.defaults.yml +++ b/config/locales/en/en.defaults.yml @@ -142,6 +142,7 @@ en: odd: must be odd record_invalid: ! 'validation failed: %{errors}' taken: is already taken + taken_with_deleted: is already taken (deleted group) too_long: is too long (no more than %{count} characters) too_short: is too short (use more than %{count} characters) wrong_length: is the wrong length (has to have exactly %{count} characters)