From 292daa66e04649733d199b8523bbcd529d033be6 Mon Sep 17 00:00:00 2001 From: Martin Lensment Date: Thu, 31 Jul 2014 10:35:32 +0300 Subject: [PATCH] Refactor contact type parsing to dynamic --- app/helpers/epp/domains_helper.rb | 13 ++++++------- app/models/domain.rb | 19 +++++++------------ 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/app/helpers/epp/domains_helper.rb b/app/helpers/epp/domains_helper.rb index f5fa25fc0..eab7284f2 100644 --- a/app/helpers/epp/domains_helper.rb +++ b/app/helpers/epp/domains_helper.rb @@ -36,13 +36,12 @@ module Epp::DomainsHelper def domain_contacts parsed_frame = Nokogiri::XML(params[:frame]).remove_namespaces! - res = {tech: [], admin: []} - parsed_frame.css('contact[type="tech"]').each do |x| - res[:tech] << Hash.from_xml(x.to_s).with_indifferent_access - end - - parsed_frame.css('contact[type="admin"]').each do |x| - res[:admin] << Hash.from_xml(x.to_s).with_indifferent_access + res = {} + Contact::CONTACT_TYPES.each do |ct| + res[ct.to_sym] ||= [] + parsed_frame.css("contact[type='#{ct}']").each do |x| + res[ct.to_sym] << Hash.from_xml(x.to_s).with_indifferent_access + end end res diff --git a/app/models/domain.rb b/app/models/domain.rb index 6bc1a3b42..0fe7fe033 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -31,18 +31,13 @@ class Domain < ActiveRecord::Base end def attach_contacts(contacts) - contacts[:tech].each do |x| - domain_contacts.create( - contact: Contact.find_by(code: x[:contact]), - contact_type: Contact::CONTACT_TYPE_TECH - ) - end - - contacts[:admin].each do |x| - domain_contacts.create( - contact: Contact.find_by(code: x[:contact]), - contact_type: Contact::CONTACT_TYPE_ADMIN - ) + contacts.each do |k, v| + v.each do |x| + domain_contacts.create( + contact: Contact.find_by(code: x[:contact]), + contact_type: k + ) + end end end