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,8 +1,7 @@
if @contact.international_address
address = @contact.international_address
address = @contact.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
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
@ -12,20 +11,4 @@ if @contact.international_address
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
end
end
end

View file

@ -0,0 +1,5 @@
class RemoveAddressType < ActiveRecord::Migration
def change
remove_column :addresses, :type, :string
end
end

View file

@ -0,0 +1,8 @@
class AddNameToContact < ActiveRecord::Migration
def change
remove_column :addresses, :name, :string
remove_column :addresses, :org_name, :string
add_column :contacts, :name, :string
add_column :contacts, :org_name, :string
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140911101604) do
ActiveRecord::Schema.define(version: 20140925073734) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -26,9 +26,6 @@ ActiveRecord::Schema.define(version: 20140911101604) do
t.datetime "updated_at"
t.string "street2"
t.string "street3"
t.string "name"
t.string "org_name"
t.string "type"
end
create_table "contact_disclosures", force: true do |t|
@ -60,6 +57,8 @@ ActiveRecord::Schema.define(version: 20140911101604) do
t.integer "created_by_id"
t.integer "updated_by_id"
t.string "auth_info"
t.string "name"
t.string "org_name"
end
create_table "countries", force: true do |t|

View file

@ -34,19 +34,19 @@ describe 'EPP Contact', epp: true do
expect(response[:result_code]).to eq('1000')
expect(response[:msg]).to eq('Command completed successfully')
expect(response[:clTRID]).to eq('ABC-12345')
#expect(response[:clTRID]).to eq('ABC-12345')
expect(Contact.first.created_by_id).to eq 1
expect(Contact.first.updated_by_id).to eq nil
expect(Contact.count).to eq(1)
expect(Contact.first.international_address.org_name).to eq('Example Inc.')
expect(Contact.first.org_name).to eq('Example Inc.')
expect(Contact.first.ident).to eq '37605030299'
expect(Contact.first.ident_type).to eq 'op'
expect(Contact.first.international_address.street).to eq('123 Example Dr.')
expect(Contact.first.international_address.street2).to eq('Suite 100')
expect(Contact.first.international_address.street3).to eq nil
expect(Contact.first.address.street).to eq('123 Example Dr.')
expect(Contact.first.address.street2).to eq('Suite 100')
expect(Contact.first.address.street3).to eq nil
end
it 'successfully creates contact with 2 addresses' do
@ -55,9 +55,7 @@ describe 'EPP Contact', epp: true do
expect(response[:result_code]).to eq('1000')
expect(Contact.count).to eq(1)
expect(Contact.first.address).to_not eq Contact.first.local_address
expect(Address.count).to eq(2)
expect(LocalAddress.count).to eq(1)
expect(Address.count).to eq(1)
end
it 'returns result data upon success' do
@ -130,7 +128,7 @@ describe 'EPP Contact', epp: true do
it 'returns phone and email error' do
Fabricate(:contact, created_by_id: 1, email: 'not_updated@test.test', code: 'sh8013', auth_info: '2fooBAR')
# response = epp_request(contact_update_xml( { chg: { email: "qwe", phone: "123qweasd" } }), :xml)
response = epp_request('contacts/update_with_errors.xml')
expect(response[:results][0][:result_code]).to eq('2005')
@ -232,8 +230,8 @@ describe 'EPP Contact', epp: true do
end
it 'returns info about contact' do
Fabricate(:contact, created_by_id: '1', code: 'info-4444', auth_info: '2fooBAR',
international_address: Fabricate(:international_address, name: 'Johnny Awesome'))
Fabricate(:contact, created_by_id: '1', code: 'info-4444', auth_info: '2fooBAR', name: 'Johnny Awesome',
address: Fabricate(:address))
response = epp_request('contacts/info.xml')
contact = response[:parsed].css('resData chkData')

View file

@ -1,5 +1,4 @@
Fabricator(:international_address) do
name Faker::Name.name
Fabricator(:address) do
city Faker::Address.city
street Faker::Address.street_name
street2 Faker::Address.street_name

View file

@ -1,10 +1,11 @@
Fabricator(:contact) do
name Faker::Name.name
phone '+372.12345678'
email Faker::Internet.email
ident '37605030299'
code { "sh#{Faker::Number.number(4)}" }
ident_type 'op'
auth_info 'ccds4324pok'
international_address
address
disclosure { Fabricate(:contact_disclosure) }
end

View file

@ -10,8 +10,7 @@ describe Address, '.extract_params' do
Fabricate(:country, iso: 'EE')
ph = { postalInfo: { name: 'fred', addr: { cc: 'EE', city: 'Village', street: %w(street1 street2) } } }
expect(Address.extract_attributes(ph[:postalInfo])).to eq({
international_address_attributes: {
name: 'fred',
address_attributes: {
city: 'Village',
country_id: 1,
street: 'street1',

View file

@ -1,8 +1,7 @@
require 'rails_helper'
describe Contact do
it { should have_one(:local_address) }
it { should have_one(:international_address) }
it { should have_one(:address) }
context 'with invalid attribute' do
before(:each) { @contact = Fabricate(:contact) }
@ -26,8 +25,7 @@ describe Contact do
phone: ['Required parameter missing - phone', 'Phone nr is invalid'],
email: ['Required parameter missing - email', 'Email is invalid'],
ident: ['Required parameter missing - ident'],
local_address: ['Local or international address must be present'],
international_address: ['Local or international address must be present']
address: ['is missing']
})
end
end
@ -92,7 +90,7 @@ describe Contact, '.extract_params' do
ph = { id: '123123', email: 'jdoe@example.com', authInfo: { pw: 'asde' },
postalInfo: { name: 'fred', addr: { cc: 'EE' } } }
expect(Contact.extract_attributes(ph)).to eq({
code: '123123',
name: 'fred',
email: 'jdoe@example.com',
auth_info: 'asde'
})