diff --git a/app/helpers/epp/contacts_helper.rb b/app/helpers/epp/contacts_helper.rb index 594ecf2b5..0ec5baeed 100644 --- a/app/helpers/epp/contacts_helper.rb +++ b/app/helpers/epp/contacts_helper.rb @@ -18,12 +18,13 @@ module Epp::ContactsHelper end def delete_contact - #no deleting, implement PaperTrail or something similar. - #TODO check for relation before 'destroying' - @contact = find_contact - handle_errors(@contact) and return unless @contact - @contact.destroy - render '/epp/contacts/delete' + Contact.transaction do + @contact = find_contact + handle_errors(@contact) and return unless @contact + handle_errors(@contact) and return unless @contact.destroy_and_clean + + render '/epp/contacts/delete' + end end def check_contact diff --git a/app/models/contact.rb b/app/models/contact.rb index 92a65f384..ecce51574 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -71,8 +71,32 @@ class Contact < ActiveRecord::Base return false end + def get_relation( model = :domain_contacts ) + send(model) + rescue NoMethodError => e + nil + end + + def has_relation( model = :domain_contacts ) + relation = get_relation(model) + return true unless relation.nil? || relation.blank? + false + end + + + + #should use only in transaction + def destroy_and_clean + clean_up_address + destroy + rescue + errors.add(:contact, msg: I18n.t('errors.messages.epp_command_failed'), value: { obj: 'contact', val: code }) + false + end + class << self + def extract_attributes ph, type=:create contact_hash = { @@ -107,4 +131,11 @@ class Contact < ActiveRecord::Base end end + private + + def clean_up_address + address.destroy if address + end + + end diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index c485ce362..86ab1881a 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -39,6 +39,29 @@ describe Contact do end end +describe Contact, '#get_relation' do + before(:each) { Fabricate(:contact) } + it 'should return nil if no method' do + expect(Contact.first.get_relation(:chewbacca)).to eq nil + end + + it 'should return domain_contacts by default' do + expect(Contact.first.get_relation).to eq [] + end +end + +describe Contact, '#has_relation' do + before(:each) { Fabricate(:domain) } + it 'should return false if no relation' do + expect(Contact.last.has_relation(:chewbacca)).to eq false + end + + it 'should return true if relation' do + expect(Contact.last.has_relation).to eq true + expect(Contact.last.has_relation(:address)).to eq true + end +end + describe Contact, '#crID' do before(:each) { Fabricate(:contact, code: "asd12", created_by: Fabricate(:epp_user)) }