From 65f6b4b43e2c5ad137bc1ad621111fe81751b5ce Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Tue, 8 Mar 2016 15:06:53 +0200 Subject: [PATCH 1/2] Story#114871365 - add address country_code validation --- app/models/contact.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/models/contact.rb b/app/models/contact.rb index aa3819850..4f2e7105e 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -39,8 +39,8 @@ class Contact < ActiveRecord::Base self.ident_updated_at = Time.zone.now if new_record? && ident_updated_at.blank? end - before_validation :set_ident_country_code - before_validation :prefix_code + before_validation :val_upcase_country_code + before_validation :val_prefix_code before_create :generate_auth_info before_update :manage_emails @@ -298,7 +298,7 @@ class Contact < ActiveRecord::Base end # rubocop:disable Metrics/CyclomaticComplexity - def prefix_code + def val_prefix_code return nil unless new_record? return nil if registrar.blank? code = self[:code] @@ -338,13 +338,17 @@ class Contact < ActiveRecord::Base destroy end - def set_ident_country_code - return true unless ident_country_code_changed? && ident_country_code.present? - code = Country.new(ident_country_code) - if code + 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? + + if code = Country.new(country_code) + self.country_code = code.alpha2 + else + errors.add(:country_code, :invalid_country_code) end end From d18fb5e6be46feb9c01920732bff374ffc36e299 Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Tue, 8 Mar 2016 15:46:48 +0200 Subject: [PATCH 2/2] Story#114871365 - add rake task to update contacts country code --- lib/tasks/convert.rake | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/tasks/convert.rake b/lib/tasks/convert.rake index ba0e547dc..849450c7c 100644 --- a/lib/tasks/convert.rake +++ b/lib/tasks/convert.rake @@ -32,5 +32,21 @@ namespace :convert do d.save! end end + + + desc 'Contact Address Country Code Upcase' + task country_code_ucase: :environment do + count = 0 + Contact.find_each do |c| + if c.country_code.present? && c.country_code != c.country_code.upcase + c.country_code = c.country_code.upcase + c.update_columns(country_code: c.country_code.upcase) + # c.send(:record_update) + + count +=1 + puts "#{count} contacts has been changed" if count % 1000 == 0 + end + end + end end