From 65f6b4b43e2c5ad137bc1ad621111fe81751b5ce Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Tue, 8 Mar 2016 15:06:53 +0200 Subject: [PATCH 1/8] 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/8] 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 From 3615f4dd8bd8131cbc455e635767353b95c4fdc5 Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Tue, 8 Mar 2016 15:48:26 +0200 Subject: [PATCH 3/8] Story#114871365 - add rake task to update contacts country code (typo) --- lib/tasks/convert.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/convert.rake b/lib/tasks/convert.rake index 849450c7c..d55778ad9 100644 --- a/lib/tasks/convert.rake +++ b/lib/tasks/convert.rake @@ -35,7 +35,7 @@ namespace :convert do desc 'Contact Address Country Code Upcase' - task country_code_ucase: :environment do + task country_code_upcase: :environment do count = 0 Contact.find_each do |c| if c.country_code.present? && c.country_code != c.country_code.upcase From 2af2a2ab1cc7aace3a539759541492d1fde2b253 Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Fri, 11 Mar 2016 15:13:40 +0200 Subject: [PATCH 4/8] 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 From 4ab4ba6f975a04c1bb4724a34f0cc273dc7d7445 Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Wed, 23 Mar 2016 18:55:04 +0200 Subject: [PATCH 5/8] Story#114871365 - add to country_code updater functionality to update it in log table --- lib/tasks/convert.rake | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/tasks/convert.rake b/lib/tasks/convert.rake index d55778ad9..c3976f2a0 100644 --- a/lib/tasks/convert.rake +++ b/lib/tasks/convert.rake @@ -41,12 +41,36 @@ namespace :convert do 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 + puts "Contacts change has been finished. Starting ContactVersions" + + count = 0 + ContactVersion.find_each do |c| + if if_object = (c.object && c.object["country_code"].present? && c.object["country_code"] != c.object["country_code"].upcase) || + if_changes = (c.object_changes && c.object_changes["country_code"].present? && c.object_changes["country_code"] != c.object_changes["country_code"].map{|e|e.try(:upcase)}) + + if if_object + h = c.object + h["country_code"] = h["country_code"].upcase + c.object = h + end + + if if_changes + h = c.object_changes + h["country_code"] = h["country_code"].map{|e|e.try(:upcase)} + c.object_changes = h + binding.pry + end + c.update_columns(object: c.object, object_changes: c.object_changes) + + count +=1 + puts "#{count} contact histories has been changed" if count % 1000 == 0 + end + end end end From 2a7de9fbd6232d55a261ae4b7b6af199e335ff14 Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Thu, 24 Mar 2016 15:32:44 +0200 Subject: [PATCH 6/8] Story#114871365 - fix imported old fred history --- lib/tasks/convert.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/convert.rake b/lib/tasks/convert.rake index c3976f2a0..7b5adb9a5 100644 --- a/lib/tasks/convert.rake +++ b/lib/tasks/convert.rake @@ -55,7 +55,7 @@ namespace :convert do if if_object h = c.object - h["country_code"] = h["country_code"].upcase + h["country_code"] = h["country_code"].try(:upcase) c.object = h end From b682deeb9e1ce151960f35208ba0790d93b6110f Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Mon, 28 Mar 2016 11:40:34 +0300 Subject: [PATCH 7/8] story#114871365 - revert prefix code name generation --- app/models/contact.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/contact.rb b/app/models/contact.rb index baa51c19e..f06008495 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -41,7 +41,7 @@ class Contact < ActiveRecord::Base end before_validation :to_upcase_country_code - before_validation :val_prefix_code + before_validation :prefix_code before_create :generate_auth_info before_update :manage_emails @@ -299,7 +299,7 @@ class Contact < ActiveRecord::Base end # rubocop:disable Metrics/CyclomaticComplexity - def val_prefix_code + def prefix_code return nil unless new_record? return nil if registrar.blank? code = self[:code] From 0f33a0fb70da1fb2aa602a6f7e02374181db6774 Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Tue, 29 Mar 2016 17:44:01 +0300 Subject: [PATCH 8/8] story#114871365 - differintiate which value we're setting in if --- lib/tasks/convert.rake | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/tasks/convert.rake b/lib/tasks/convert.rake index 7b5adb9a5..35e68046c 100644 --- a/lib/tasks/convert.rake +++ b/lib/tasks/convert.rake @@ -50,8 +50,8 @@ namespace :convert do count = 0 ContactVersion.find_each do |c| - if if_object = (c.object && c.object["country_code"].present? && c.object["country_code"] != c.object["country_code"].upcase) || - if_changes = (c.object_changes && c.object_changes["country_code"].present? && c.object_changes["country_code"] != c.object_changes["country_code"].map{|e|e.try(:upcase)}) + if (if_object = (c.object && c.object["country_code"].present? && c.object["country_code"] != c.object["country_code"].upcase)) || + (if_changes = (c.object_changes && c.object_changes["country_code"].present? && c.object_changes["country_code"] != c.object_changes["country_code"].map{|e|e.try(:upcase)})) if if_object h = c.object @@ -63,7 +63,6 @@ namespace :convert do h = c.object_changes h["country_code"] = h["country_code"].map{|e|e.try(:upcase)} c.object_changes = h - binding.pry end c.update_columns(object: c.object, object_changes: c.object_changes)