Show usefull error message for uniqueness_of_name with acts_as_paranoid.

Fixed #88
This commit is contained in:
Benjamin Meichsner 2013-02-24 23:26:16 +01:00
parent 8b2b70f8bf
commit 82178161fa
5 changed files with 28 additions and 6 deletions

View file

@ -4,7 +4,7 @@ class Group < ActiveRecord::Base
has_many :memberships, :dependent => :destroy has_many :memberships, :dependent => :destroy
has_many :users, :through => :memberships 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 attr_reader :user_tokens

View file

@ -16,7 +16,7 @@ class Ordergroup < Group
has_many :orders, :through => :group_orders has_many :orders, :through => :group_orders
validates_numericality_of :account_balance, :message => 'ist keine gültige Zahl' 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! after_create :update_stats!
@ -107,5 +107,15 @@ class Ordergroup < Group
end end
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 end

View file

@ -12,15 +12,13 @@ class Supplier < ActiveRecord::Base
attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number, attr_accessible :name, :address, :phone, :phone2, :fax, :email, :url, :contact_person, :customer_number,
:delivery_days, :order_howto, :note, :shared_supplier_id, :min_order_quantity :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 :phone, :presence => true, :length => { :in => 8..20 }
validates :address, :presence => true, :length => { :in => 8..50 } validates :address, :presence => true, :length => { :in => 8..50 }
validates_length_of :order_howto, :note, maximum: 250 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 :phone, :in => 8..20
validates_length_of :address, :in => 8..50 validates_length_of :address, :in => 8..50
validate :uniqueness_of_name
# sync all articles with the external database # sync all articles with the external database
# returns an array with articles(and prices), which should be updated (to use in a form) # 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 end
return [updated_articles, outlisted_articles] return [updated_articles, outlisted_articles]
end 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 end

View file

@ -5,6 +5,7 @@ class Workgroup < Group
# returns all non-finished tasks # returns all non-finished tasks
has_many :open_tasks, :class_name => 'Task', :conditions => ['done = ?', false], :order => 'due_date ASC' 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, validates_presence_of :task_name, :weekday, :task_required_users, :next_weekly_tasks_number,
:if => :weekly_task :if => :weekly_task
validates_numericality_of :next_weekly_tasks_number, :greater_than => 0, :less_than => 21, :only_integer => true, validates_numericality_of :next_weekly_tasks_number, :greater_than => 0, :less_than => 21, :only_integer => true,

View file

@ -143,6 +143,7 @@ de:
odd: muss ungerade sein odd: muss ungerade sein
record_invalid: ! 'Gültigkeitsprüfung ist fehlgeschlagen: %{errors}' record_invalid: ! 'Gültigkeitsprüfung ist fehlgeschlagen: %{errors}'
taken: ist bereits vergeben 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_long: ist zu lang (nicht mehr als %{count} Zeichen)
too_short: ist zu kurz (nicht weniger 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) wrong_length: hat die falsche Länge (muss genau %{count} Zeichen haben)