Merged the address types

This commit is contained in:
Andres Keskküla 2014-09-25 15:27:08 +03:00
parent 7c8e7e22af
commit c4deed6a55
17 changed files with 90 additions and 103 deletions

View file

@ -4,6 +4,8 @@ class Epp::CommandsController < ApplicationController
include Epp::ContactsHelper
include Shared::UserStamper
layout false
private
def create

View file

@ -1,5 +1,6 @@
class Epp::ErrorsController < ApplicationController
include Epp::Common
layout false
def error
epp_errors << { code: params[:code], msg: params[:msg] }

View file

@ -1,5 +1,6 @@
class Epp::SessionsController < ApplicationController
include Epp::Common
layout false
private

View file

@ -59,7 +59,7 @@ module Epp::ContactsHelper
return epp_errors.empty? unless @ph['postalInfo'].is_a?(Hash) || @ph['postalInfo'].is_a?(Array)
(epp_errors << Address.validate_postal_info_types(parsed_frame)).flatten!
#(epp_errors << Address.validate_postal_info_types(parsed_frame)).flatten!
xml_attrs_array_present?(@ph['postalInfo'], [%w(name), %w(addr city), %w(addr cc)])
end

View file

@ -14,41 +14,38 @@ class Address < ActiveRecord::Base
# validates_inclusion_of :type, in: TYPES
class << self
def validate_postal_info_types(parsed_frame)
errors, used = [], []
parsed_frame.css('postalInfo').each do |pi|
attr = pi.attributes['type'].try(:value)
errors << { code: 2003, msg: I18n.t('errors.messages.attr_missing', key: 'type') } and next unless attr
unless TYPES.include?(attr)
errors << { code: 2005, msg: I18n.t('errors.messages.invalid_type'), value: { obj: 'type', val: attr } }
next
end
errors << { code: 2005, msg: I18n.t('errors.messages.repeating_postal_info') } and next if used.include?(attr)
used << attr
end; errors
end
# def validate_postal_info_types(parsed_frame)
# errors, used = [], []
# parsed_frame.css('postalInfo').each do |pi|
# attr = pi.attributes['type'].try(:value)
# errors << { code: 2003, msg: I18n.t('errors.messages.attr_missing', key: 'type') } and next unless attr
# unless TYPES.include?(attr)
# errors << { code: 2005, msg: I18n.t('errors.messages.invalid_type'), value: { obj: 'type', val: attr } }
# next
# end
# errors << { code: 2005, msg: I18n.t('errors.messages.repeating_postal_info') } and next if used.include?(attr)
# used << attr
# end; errors
# end
def extract_attributes(ah)
address_hash = {}
[ah].flatten.each do |pi|
address_hash[local?(pi)] = addr_hash_from_params(pi)
end
ah = ah.first if ah.is_a?(Array)
address_hash[:address_attributes] = addr_hash_from_params(ah)
address_hash
end
private
def local?(postal_info)
return :local_address_attributes if postal_info[:type] == LOCAL_TYPE_SHORT
:international_address_attributes
end
# 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 {} if addr.nil?
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),
{ 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],

View file

@ -4,8 +4,9 @@ class Contact < ActiveRecord::Base
include EppErrors
has_one :local_address, dependent: :destroy
has_one :international_address, dependent: :destroy
#has_one :local_address, dependent: :destroy
#has_one :international_address, dependent: :destroy
has_one :address, dependent: :destroy
has_one :disclosure, class_name: 'ContactDisclosure'
has_many :domain_contacts
@ -14,24 +15,22 @@ class Contact < ActiveRecord::Base
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 :local_address, :international_address, :disclosure
accepts_nested_attributes_for :address, :disclosure
validates :code, :phone, :email, :ident, presence: true
validates :code, :phone, :email, :ident, :address, presence: true
validate :ident_must_be_valid
validate :presence_of_one_address
#validate :presence_of_one_address
validates :phone, format: /\+[0-9]{1,3}\.[0-9]{1,14}?/ # /\+\d{3}\.\d+/
validates :email, format: /@/
validates :code, uniqueness: { message: :epp_id_taken }
delegate :name, to: :international_address
delegate :country, to: :address, prefix: true
delegate :city, to: :address, prefix: true
delegate :street, to: :address, prefix: true
delegate :zip, to: :address, prefix: true
delegate :org_name, to: :address, prefix: true
delegate :country, to: :address#, prefix: true
delegate :city, to: :address#, prefix: true
delegate :street, to: :address#, prefix: true
delegate :zip, to: :address#, prefix: true
IDENT_TYPE_ICO = 'ico'
IDENT_TYPES = [
@ -49,16 +48,16 @@ class Contact < ActiveRecord::Base
# scope :named, -> { joins(:international_address).uniq.all }
# TEMP METHOD for transaction to STI
def address
international_address
end
#def address
# international_address
#end
##
def presence_of_one_address
return true if local_address || international_address
errors.add(:local_address, 'Local or international address must be present')
errors.add(:international_address, 'Local or international address must be present')
end
#def presence_of_one_address
# return true if local_address || international_address
# errors.add(:local_address, 'Local or international address must be present')
# errors.add(:international_address, 'Local or international address must be present')
#end
def ident_must_be_valid
# TODO: Ident can also be passport number or company registry code.
@ -137,13 +136,14 @@ class Contact < ActiveRecord::Base
# EPP
def extract_attributes(ph, type = :create)
ph[:postalInfo] = ph[:postalInfo].first if ph[:postalInfo].is_a?(Array)
contact_hash = {
phone: ph[:voice],
ident: ph[:ident],
email: ph[:email]
email: ph[:email],
name: ph[:postalInfo].try(:[], :name),
org_name: ph[:postalInfo].try(:[], :org)
}
contact_hash[:code] = ph[:id] if type == :create
contact_hash[:auth_info] = ph[:authInfo][:pw] if type == :create
contact_hash.delete_if { |_k, v| v.nil? }
end

View file

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

View file

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

View file

@ -1,30 +1,13 @@
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) if @contact.disclosure.int_name
xml.tag!('contact:org', address.org_name) if @contact.disclosure.int_org_name
if @contact.disclosure.int_addr
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
end
if @contact.local_address
address = @contact.local_address
xml.tag!('contact:postalInfo', type: 'loc') do
xml.tag!('contact:name', address.name) if @contact.disclosure.loc_name
xml.tag!('contact:org', address.org_name) if @contact.disclosure.loc_org_name
if @contact.disclosure.loc_addr
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
address = @contact.address
xml.tag!('contact:postalInfo', type: 'int') do # TODO instance method of defining type
xml.tag!('contact:name', @contact.name) if @contact.disclosure.int_name
xml.tag!('contact:org', @contact.org_name) if @contact.disclosure.int_org_name
if @contact.disclosure.int_addr
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