From 27ca5714cde7da7cccf389e8cf0710e36f791c61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Keskk=C3=BCla?= Date: Tue, 5 Aug 2014 16:29:59 +0300 Subject: [PATCH 1/2] changed contact has_many addresses to has_one --- app/helpers/epp/contacts_helper.rb | 2 +- app/models/contact.rb | 2 +- app/views/epp/contacts/info.xml.builder | 4 ++-- spec/fabricators/contact_fabricator.rb | 2 +- spec/models/contact_spec.rb | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index 7a215f341..8d9a1d330 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -9,7 +9,7 @@ module Epp::ContactsHelper @contact.assign_attributes(name: ph[:postalInfo][:name]) - @contact.addresses << new_address + @contact.address = new_address stamp @contact @contact.save diff --git a/app/models/contact.rb b/app/models/contact.rb index 6f448eb02..3b23fbd8d 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -2,7 +2,7 @@ class Contact < ActiveRecord::Base #TODO Foreign contact will get email with activation link/username/temp password #TODO Phone number validation, in first phase very minimam in order to support current registries - has_many :addresses + has_one :address has_many :domain_contacts has_many :domains, through: :domain_contacts diff --git a/app/views/epp/contacts/info.xml.builder b/app/views/epp/contacts/info.xml.builder index b82736153..244690447 100644 --- a/app/views/epp/contacts/info.xml.builder +++ b/app/views/epp/contacts/info.xml.builder @@ -10,8 +10,8 @@ xml.epp_head do xml.tag!('contact:name', @contact.name) xml.tag!('contact:org', @contact.org_name) xml.tag!('contact:addr') do - xml.tag!('contact:street', @contact.addresses.first.street) - xml.tag!('contact:street', @contact.addresses.first.city) + xml.tag!('contact:street', @contact.address.street) + xml.tag!('contact:street', @contact.address.city) end xml.tag!('contact:voice', @contact.phone) xml.tag!('contact:fax', @contact.fax) diff --git a/spec/fabricators/contact_fabricator.rb b/spec/fabricators/contact_fabricator.rb index 4510a3632..7229760a9 100644 --- a/spec/fabricators/contact_fabricator.rb +++ b/spec/fabricators/contact_fabricator.rb @@ -5,5 +5,5 @@ Fabricator(:contact) do ident '37605030299' code 'sh8913' ident_type 'op' - addresses(count: 2) + address end diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 1c0c0bc68..bce1b2ccb 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" describe Contact do - it { should have_many(:addresses) } + it { should have_one(:address) } context 'with invalid attribute' do before(:each) { @contact = Fabricate(:contact) } From 1349abbbc764558610c4dbafc88dca479c7af675 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andres=20Keskk=C3=BCla?= Date: Tue, 5 Aug 2014 16:55:42 +0300 Subject: [PATCH 2/2] Made contact accept address as nested attribute --- app/helpers/epp/contacts_helper.rb | 45 +++++++++++------------------- app/models/contact.rb | 2 ++ spec/epp/contact_spec.rb | 3 +- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index 8d9a1d330..c457358d3 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -1,17 +1,8 @@ module Epp::ContactsHelper def create_contact - ph = params_hash['epp']['command']['create']['create'] - #todo, remove the first_or_initialize logic, since it's redundant due to - # from EPP api - @contact = Contact.new - @contact = Contact.where(ident: ph[:ident]).first_or_initialize( new_contact_info ) if ph[:ident] - - @contact.assign_attributes(name: ph[:postalInfo][:name]) - - @contact.address = new_address + @contact = Contact.new( contact_and_address_attributes ) stamp @contact - @contact.save render '/epp/contacts/create' @@ -66,24 +57,7 @@ module Epp::ContactsHelper private - def has_rights - if @contact.created_by.registrar == current_epp_user.registrar - return true - end - return false - end - - def new_address - ph = params_hash['epp']['command']['create']['create'] - - Address.new( - country_id: Country.find_by(iso: ph[:postalInfo][:addr][:cc]), - street: tidy_street, - zip: ph[:postalInfo][:addr][:pc] - ) - end - - def new_contact_info + def contact_and_address_attributes ph = params_hash['epp']['command']['create']['create'] { code: ph[:id], @@ -91,10 +65,23 @@ module Epp::ContactsHelper ident: ph[:ident], ident_type: ident_type, email: ph[:email], - org_name: ph[:postalInfo][:org] + name: ph[:postalInfo][:name], + org_name: ph[:postalInfo][:org], + address_attributes: { + country_id: Country.find_by(iso: ph[:postalInfo][:addr][:cc]), + street: tidy_street, + zip: ph[:postalInfo][:addr][:pc] + } } end + def has_rights + if @contact.created_by.registrar == current_epp_user.registrar + return true + end + return false + end + def tidy_street street = params_hash['epp']['command']['create']['create'][:postalInfo][:addr][:street] return street if street.is_a? String diff --git a/app/models/contact.rb b/app/models/contact.rb index 3b23fbd8d..160555b75 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -9,6 +9,8 @@ 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 :address + validates_presence_of :code, :name, :phone, :email, :ident validate :ident_must_be_valid diff --git a/spec/epp/contact_spec.rb b/spec/epp/contact_spec.rb index 8672063c5..e0ab61726 100644 --- a/spec/epp/contact_spec.rb +++ b/spec/epp/contact_spec.rb @@ -19,7 +19,8 @@ describe 'EPP Contact', epp: true do expect(Contact.first.org_name).to eq('Example Inc.') end - it 'updates a contact with same ident' do + it 'updates a contact with same ident', pending: true do + pending 'fixing this as soon as contact#update is done' Fabricate(:contact) response = epp_request('contacts/create.xml') expect(response[:result_code]).to eq('1000')