Replaced address with local and international addr

This commit is contained in:
Andres Keskküla 2014-08-25 17:05:57 +03:00
parent fe54f327d9
commit 2655da4555
16 changed files with 208 additions and 57 deletions

View file

@ -57,6 +57,15 @@ module Epp::Common
epp_errors.empty?
end
def xml_nested_attrs_present?(array_ph, attributes )
[array_ph].flatten.each do |ph|
attributes.each do |x|
epp_errors << {code: '2003', msg: I18n.t('errors.messages.required_parameter_missing', key: x.last)} unless has_attribute(ph, x)
end
end
epp_errors.empty?
end
def has_attribute(ph, path)
path.inject(ph) do |location, key|
location.respond_to?(:keys) ? location[key] : nil

View file

@ -48,9 +48,14 @@ module Epp::ContactsHelper
@ph = params_hash['epp']['command']['create']['create']
xml_attrs_present?(@ph, [['id'],
['authInfo', 'pw'],
['postalInfo', 'name'],
['postalInfo', 'addr', 'city'],
['postalInfo', 'addr', 'cc']])
['postalInfo']])
# ['postalInfo', 'addr', 'city'],
# ['postalInfo', 'addr', 'cc']])
if @ph['postalInfo'].is_a?(Hash) || @ph['postalInfo'].is_a?(Array)
xml_nested_attrs_present?( @ph['postalInfo'], [['name'],
['addr', 'city'],
['addr', 'cc']] )
end
end
## UPDATE
@ -101,12 +106,17 @@ module Epp::ContactsHelper
case type
when :update
contact_hash = Contact.extract_attributes(@ph[:chg], type)
contact_hash[:address_attributes] =
Address.extract_attributes(( @ph.try(:[], :chg).try(:[], :postalInfo).try(:[], :addr) || [] ), type)
#contact_hash[:address_attributes] =
contact_hash = contact_hash.merge(
Address.extract_attributes(( @ph.try(:[], :chg).try(:[], :postalInfo) || [] ), type)
)
else
contact_hash = Contact.extract_attributes(@ph, type)
contact_hash[:address_attributes] =
Address.extract_attributes(( @ph.try(:[], :postalInfo).try(:[], :addr) || [] ), type)
#contact_hash[:address_attributes] =
# Address.extract_attributes(( @ph.try(:[], :postalInfo) || [] ), type)
contact_hash = contact_hash.merge(
Address.extract_attributes(( @ph.try(:[], :postalInfo) || [] ), type)
)
end
contact_hash[:ident_type] = ident_type unless ident_type.nil?
contact_hash

View file

@ -1,20 +1,47 @@
class Address < ActiveRecord::Base
LOCAL_TYPE_SHORT = 'loc'
INTERNATIONAL_TYPE_SHORT = 'int'
LOCAL_TYPE = 'LocalAddress'
TYPES = [
LOCAL_TYPE_SHORT,
INTERNATIONAL_TYPE_SHORT
]
belongs_to :contact
belongs_to :country
#validates_inclusion_of :type, in: TYPES
class << self
def extract_attributes ah, type=:create
address_hash = {}
address_hash = ({
country_id: Country.find_by(iso: ah[:cc]).try(:id),
city: ah[:city],
street: ah[:street][0],
street2: ah[:street][1],
street3: ah[:street][2],
zip: ah[:pc]
}) if ah.is_a?(Hash)
address_hash.delete_if { |k, v| v.nil? }
[ah].flatten.each do |pi|
address_hash[local?(pi)] = addr_hash_from_params(pi)
end
address_hash
end
private
def local?(postal_info)
return :local_address_attributes if postal_info[:type] == LOCAL_TYPE_SHORT
:international_address_attributes
end
def addr_hash_from_params(addr)
return {} unless addr[:addr].is_a?(Hash)
{
name: addr[:name],
org_name: addr[:org],
country_id: Country.find_by(iso: addr[:addr][:cc]).try(:id),
city: addr[:addr][:city],
street: addr[:addr][:street][0],
street2: addr[:addr][:street][1],
street3: addr[:addr][:street][2],
zip: addr[:addr][:pc]
}.delete_if { |k, v| v.nil? }
end
end
end

View file

@ -6,16 +6,18 @@ class Contact < ActiveRecord::Base
EPP_ATTR_MAP = {}
has_one :address
has_one :local_address#, class_name: 'Address'#, foreign_key: 'local_address_id'
has_one :international_address
has_many :domain_contacts
has_many :domains, through: :domain_contacts
belongs_to :created_by, class_name: 'EppUser', foreign_key: :created_by_id
belongs_to :updated_by, class_name: 'EppUser', foreign_key: :updated_by_id
accepts_nested_attributes_for :address
accepts_nested_attributes_for :local_address, :international_address
validates_presence_of :code, :name, :phone, :email, :ident
validates_presence_of :code, :phone, :email, :ident
validate :ident_must_be_valid
@ -36,6 +38,16 @@ class Contact < ActiveRecord::Base
CONTACT_TYPE_ADMIN = 'admin'
CONTACT_TYPES = [CONTACT_TYPE_TECH, CONTACT_TYPE_ADMIN]
#TEMP METHODS for transaction to STI
def name
international_address.name
end
def address
international_address
end
##
def ident_must_be_valid
#TODO Ident can also be passport number or company registry code.
#so have to make changes to validations (and doc/schema) accordingly
@ -114,11 +126,6 @@ class Contact < ActiveRecord::Base
email: ph[:email]
}
contact_hash = contact_hash.merge({
name: ph[:postalInfo][:name],
org_name: ph[:postalInfo][:org]
}) if ph[:postalInfo].is_a? Hash
contact_hash[:code] = ph[:id] if type == :create
contact_hash.delete_if { |k, v| v.nil? }

View file

@ -0,0 +1,3 @@
class InternationalAddress < Address
end

View file

@ -0,0 +1,3 @@
class LocalAddress < Address
end

View file

@ -0,0 +1,27 @@
if @contact.international_address
address = @contact.international_address
xml.tag!('contact:postalInfo', type: 'int') do # TODO instance method of defining type
xml.tag!('contact:name', address.name)
xml.tag!('contact:org', address.org_name)
xml.tag!('contact:addr') do
xml.tag!('contact:street', address.street) if address.street
xml.tag!('contact:street', address.street2) if address.street2
xml.tag!('contact:street', address.street3) if address.street3
xml.tag!('contact:cc', address.try(:country).try(:iso)) unless address.try(:country).nil?
end
end
end
if @contact.local_address
address = @contact.local_address
xml.tag!('contact:postalInfo', type: 'loc') do
xml.tag!('contact:name', address.name)
xml.tag!('contact:org', address.org_name)
xml.tag!('contact:addr') do
xml.tag!('contact:street', address.street) if address.street
xml.tag!('contact:street', address.street2) if address.street2
xml.tag!('contact:street', address.street3) if address.street3
xml.tag!('contact:cc', address.try(:country).try(:iso)) unless address.try(:country).nil?
end
end
end

View file

@ -6,15 +6,7 @@ xml.epp_head do
xml.resData do
xml.tag!('contact:chkData', 'xmlns:contact' => 'urn:ietf:params:xml:ns:contact-1.0') do
xml.tag!('contact:name', @contact.name)
xml.tag!('contact:org', @contact.org_name)
xml.tag!('contact:addr') do
address = @contact.address
xml.tag!('contact:street', address.street) if address.street
xml.tag!('contact:street', address.street2) if address.street2
xml.tag!('contact:street', address.street3) if address.street3
xml.tag!('contact:cc', address.try(:country).try(:iso)) unless address.try(:country).nil?
end
xml << render('/epp/contacts/postal_info')
xml.tag!('contact:voice', @contact.phone)
xml.tag!('contact:fax', @contact.fax)
xml.tag!('contact:email', @contact.email)