WhoisBody refactored to has_one

This commit is contained in:
Priit Tark 2015-04-22 16:41:52 +03:00
parent 82fd2db963
commit db81a9e7bc
6 changed files with 62 additions and 27 deletions

View file

@ -33,6 +33,7 @@ class Domain < ActiveRecord::Base
has_many :dnskeys, dependent: :destroy
has_many :keyrelays
has_one :whois_body, dependent: :destroy
accepts_nested_attributes_for :dnskeys, allow_destroy: true
@ -124,6 +125,7 @@ class Domain < ActiveRecord::Base
includes(
:registrar,
:nameservers,
:whois_body,
{ tech_contacts: :registrar },
{ admin_contacts: :registrar }
)
@ -242,17 +244,15 @@ class Domain < ActiveRecord::Base
end
def update_whois_body
whois = Whois::Body.new(self)
# validations, callbacks and updated_at are skipped
update_columns(
whois_json: whois.whois_json,
whois_body: whois.whois_body
)
self.whois_body = WhoisBody.create if whois_body.blank?
whois_body.update
end
def update_whois_server
wd = Whois::Domain.find_or_initialize_by(name: name)
wd.whois_body = whois_body
wd.save
if whois_body.present?
whois_body.update_whois_server
else
logger.info "NO WHOIS BODY for domain: #{name}"
end
end
end

View file

@ -1,10 +1,20 @@
class Whois::Body
class WhoisBody < ActiveRecord::Base
belongs_to :domain
def update_whois_server
return logger.info "NO WHOIS NAME for whois_body id: #{id}" if name.blank?
wd = Whois::Domain.find_or_initialize_by(name: name)
wd.whois_body = whois_body
wd.whois_json = whois_json
wd.save
end
# rubocop:disable Metrics/MethodLength
def h
@h ||= HashWithIndifferentAccess.new
end
def initialize(domain)
def update
h[:name] = domain.name
h[:registrant] = domain.registrant.name
h[:status] = domain.domain_statuses.map(&:human_value).join(', ')
@ -41,13 +51,14 @@ class Whois::Body
updated_at: ns.updated_at.to_s(:db)
}
end
self.name = h[:name]
self.whois_body = body
self.whois_json = h
save
end
def whois_json
h
end
def whois_body
def body
<<-EOS
Estonia .ee Top Level Domain WHOIS server