From 2af2a2ab1cc7aace3a539759541492d1fde2b253 Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Fri, 11 Mar 2016 15:13:40 +0200 Subject: [PATCH] Story#114871365 - Better error messages for country codes in contact --- app/models/contact.rb | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/app/models/contact.rb b/app/models/contact.rb index 4f2e7105e..baa51c19e 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -32,6 +32,7 @@ class Contact < ActiveRecord::Base validate :val_ident_valid_format? validate :uniq_statuses? validate :validate_html + validate :val_country_code after_initialize do self.statuses = [] if statuses.nil? @@ -39,7 +40,7 @@ class Contact < ActiveRecord::Base self.ident_updated_at = Time.zone.now if new_record? && ident_updated_at.blank? end - before_validation :val_upcase_country_code + before_validation :to_upcase_country_code before_validation :val_prefix_code before_create :generate_auth_info @@ -338,18 +339,14 @@ class Contact < ActiveRecord::Base destroy end - def val_upcase_country_code - if code = Country.new(ident_country_code) - self.ident_country_code = code.alpha2 - else - errors.add(:ident, :invalid_country_code) - end if ident_country_code_changed? && ident_country_code.present? + def to_upcase_country_code + self.ident_country_code = ident_country_code.upcase if ident_country_code + self.country_code = country_code.upcase if country_code + end - if code = Country.new(country_code) - self.country_code = code.alpha2 - else - errors.add(:country_code, :invalid_country_code) - end + def val_country_code + errors.add(:ident, :invalid_country_code) unless Country.new(ident_country_code) + errors.add(:ident, :invalid_country_code) unless Country.new(country_code) end def related_domain_descriptions