From ddd91d464afbeca4b9a4c83a95874225735de568 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Wed, 10 Dec 2014 11:35:48 +0200 Subject: [PATCH] Cache contact code for epp eager errors --- app/models/domain.rb | 22 +++++++++++++++---- app/models/domain_contact.rb | 2 +- app/models/epp/epp_domain.rb | 6 ----- config/locales/en.yml | 2 ++ ...85432_add_code_cache_for_domain_contact.rb | 10 +++++++++ db/schema.rb | 3 ++- spec/epp/domain_spec.rb | 2 +- spec/fabricators/domain_contact_fabricator.rb | 7 ++++++ spec/fabricators/domain_fabricator.rb | 2 +- 9 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20141210085432_add_code_cache_for_domain_contact.rb create mode 100644 spec/fabricators/domain_contact_fabricator.rb diff --git a/app/models/domain.rb b/app/models/domain.rb index 00aea6d17..a0a4e8ed6 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -43,7 +43,7 @@ class Domain < ActiveRecord::Base before_create :generate_auth_info before_create :set_validity_dates - after_create :attach_default_contacts + before_create :attach_default_contacts after_save :manage_automatic_statuses validates :name_dirty, domain_name: true, uniqueness: true @@ -186,7 +186,7 @@ class Domain < ActiveRecord::Base next unless existing.length > 1 validated << dc errors.add(:domain_contacts, :invalid) if errors[:domain_contacts].blank? - dc.errors.add(:contact, :taken) + dc.errors.add(:contact_code_cache, :taken) end end @@ -280,8 +280,22 @@ class Domain < ActiveRecord::Base # rubocop:enable Lint/Loop def attach_default_contacts - tech_contacts << owner_contact if tech_contacts_count.zero? - admin_contacts << owner_contact if admin_contacts_count.zero? && owner_contact.citizen? + if tech_contacts_count.zero? + attach_contact(DomainContact::TECH, owner_contact) + end + + return unless admin_contacts_count.zero? && owner_contact.citizen? + attach_contact(DomainContact::ADMIN, owner_contact) + end + + def attach_contact(type, contact) + domain_contacts.build( + contact: contact, contact_type: DomainContact::TECH, contact_code_cache: contact.code + ) if type.to_sym == :tech + + domain_contacts.build( + contact: contact, contact_type: DomainContact::ADMIN, contact_code_cache: contact.code + ) if type.to_sym == :admin end def set_validity_dates diff --git a/app/models/domain_contact.rb b/app/models/domain_contact.rb index 82332c0dc..a9cb203b7 100644 --- a/app/models/domain_contact.rb +++ b/app/models/domain_contact.rb @@ -12,7 +12,7 @@ class DomainContact < ActiveRecord::Base def epp_code_map { '2302' => [ - [:contact, :taken, { value: { obj: 'contact', val: contact.code } }] + [:contact_code_cache, :taken, { value: { obj: 'contact', val: contact_code_cache } }] ] } end diff --git a/app/models/epp/epp_domain.rb b/app/models/epp/epp_domain.rb index 139700f98..18011672f 100644 --- a/app/models/epp/epp_domain.rb +++ b/app/models/epp/epp_domain.rb @@ -121,11 +121,6 @@ class Epp::EppDomain < Domain attach_contact(DomainContact::ADMIN, owner_contact) if admin_contacts_count.zero? && owner_contact.citizen? end - def attach_contact(type, contact) - domain_contacts.build(contact: contact, contact_type: DomainContact::TECH) if type.to_sym == :tech - domain_contacts.build(contact: contact, contact_type: DomainContact::ADMIN) if type.to_sym == :admin - end - def attach_nameservers(ns_list) ns_list.each do |ns_attrs| nameservers.build(ns_attrs) @@ -302,7 +297,6 @@ class Epp::EppDomain < Domain end return pt if pt - if Setting.transfer_wait_time > 0 dt = domain_transfers.create( status: DomainTransfer::PENDING, diff --git a/config/locales/en.yml b/config/locales/en.yml index 48b1a75e0..fd503b4a3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -139,6 +139,8 @@ en: contact: blank: 'Contact was not found' taken: 'Contact already exists on this domain!' + contact_code_cache: + taken: 'Contact already exists on this domain!' domain_status: attributes: diff --git a/db/migrate/20141210085432_add_code_cache_for_domain_contact.rb b/db/migrate/20141210085432_add_code_cache_for_domain_contact.rb new file mode 100644 index 000000000..d77aa11ab --- /dev/null +++ b/db/migrate/20141210085432_add_code_cache_for_domain_contact.rb @@ -0,0 +1,10 @@ +class AddCodeCacheForDomainContact < ActiveRecord::Migration + def change + add_column :domain_contacts, :contact_code_cache, :string + + DomainContact.all.each do |x| + x.contact_code_cache = x.contact.code + x.save + end + end +end diff --git a/db/schema.rb b/db/schema.rb index fd2f36dda..19714e730 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141203090115) do +ActiveRecord::Schema.define(version: 20141210085432) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -139,6 +139,7 @@ ActiveRecord::Schema.define(version: 20141203090115) do t.string "contact_type" t.datetime "created_at" t.datetime "updated_at" + t.string "contact_code_cache" end create_table "domain_status_versions", force: true do |t| diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index 9b75fb238..1bf170002 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -892,7 +892,7 @@ describe 'EPP Domain', epp: true do expect(response[:results][0][:msg]).to eq('Domain not found') end - it 'updates domain and adds objects', pending: true do + it 'updates domain and adds objects' do xml = domain_update_xml({ add: [ { diff --git a/spec/fabricators/domain_contact_fabricator.rb b/spec/fabricators/domain_contact_fabricator.rb new file mode 100644 index 000000000..54f0598fa --- /dev/null +++ b/spec/fabricators/domain_contact_fabricator.rb @@ -0,0 +1,7 @@ +Fabricator(:domain_contact) do + contact { Fabricate(:contact) } + contact_type 'admin' + after_build do |x| + x.contact_code_cache = x.contact.code + end +end diff --git a/spec/fabricators/domain_fabricator.rb b/spec/fabricators/domain_fabricator.rb index d1063a096..dbf1bc985 100644 --- a/spec/fabricators/domain_fabricator.rb +++ b/spec/fabricators/domain_fabricator.rb @@ -5,7 +5,7 @@ Fabricator(:domain) do period_unit 'y' owner_contact(fabricator: :contact) nameservers(count: 3) - admin_contacts(count: 1) { Fabricate(:contact) } + domain_contacts(count: 1) { Fabricate(:domain_contact, contact_type: 'admin') } registrar auth_info '98oiewslkfkd' dnskeys(count: 1)