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 :dnskeys, dependent: :destroy
has_many :keyrelays has_many :keyrelays
has_one :whois_body, dependent: :destroy
accepts_nested_attributes_for :dnskeys, allow_destroy: true accepts_nested_attributes_for :dnskeys, allow_destroy: true
@ -124,6 +125,7 @@ class Domain < ActiveRecord::Base
includes( includes(
:registrar, :registrar,
:nameservers, :nameservers,
:whois_body,
{ tech_contacts: :registrar }, { tech_contacts: :registrar },
{ admin_contacts: :registrar } { admin_contacts: :registrar }
) )
@ -242,17 +244,15 @@ class Domain < ActiveRecord::Base
end end
def update_whois_body def update_whois_body
whois = Whois::Body.new(self) self.whois_body = WhoisBody.create if whois_body.blank?
# validations, callbacks and updated_at are skipped whois_body.update
update_columns(
whois_json: whois.whois_json,
whois_body: whois.whois_body
)
end end
def update_whois_server def update_whois_server
wd = Whois::Domain.find_or_initialize_by(name: name) if whois_body.present?
wd.whois_body = whois_body whois_body.update_whois_server
wd.save else
logger.info "NO WHOIS BODY for domain: #{name}"
end
end 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 # rubocop:disable Metrics/MethodLength
def h def h
@h ||= HashWithIndifferentAccess.new @h ||= HashWithIndifferentAccess.new
end end
def initialize(domain) def update
h[:name] = domain.name h[:name] = domain.name
h[:registrant] = domain.registrant.name h[:registrant] = domain.registrant.name
h[:status] = domain.domain_statuses.map(&:human_value).join(', ') h[:status] = domain.domain_statuses.map(&:human_value).join(', ')
@ -41,13 +51,14 @@ class Whois::Body
updated_at: ns.updated_at.to_s(:db) updated_at: ns.updated_at.to_s(:db)
} }
end end
self.name = h[:name]
self.whois_body = body
self.whois_json = h
save
end end
def whois_json def body
h
end
def whois_body
<<-EOS <<-EOS
Estonia .ee Top Level Domain WHOIS server Estonia .ee Top Level Domain WHOIS server

View file

@ -0,0 +1,15 @@
class AddWhoisBodyToRegistry < ActiveRecord::Migration
def change
create_table :whois_bodies, force: :cascade do |t|
t.integer :domain_id
t.string :name
t.text :whois_body
t.json :whois_json
t.datetime :created_at, null: false
t.datetime :updated_at, null: false
end
add_index :whois_bodies, :domain_id
remove_column :domains, :whois_body, :text
remove_column :domains, :whois_json, :json
end
end

View file

@ -294,13 +294,15 @@ ActiveRecord::Schema.define(version: 20150423083308) do
t.string "period_unit", limit: 1 t.string "period_unit", limit: 1
t.string "creator_str" t.string "creator_str"
t.string "updator_str" t.string "updator_str"
t.text "whois_body"
t.integer "legacy_id" t.integer "legacy_id"
t.integer "legacy_registrar_id" t.integer "legacy_registrar_id"
t.integer "legacy_registrant_id" t.integer "legacy_registrant_id"
t.json "whois_json" t.datetime "outzone_at"
t.datetime "delete_at"
end end
add_index "domains", ["delete_at"], name: "index_domains_on_delete_at", using: :btree
add_index "domains", ["outzone_at"], name: "index_domains_on_outzone_at", using: :btree
add_index "domains", ["registrant_id"], name: "index_domains_on_registrant_id", using: :btree add_index "domains", ["registrant_id"], name: "index_domains_on_registrant_id", using: :btree
add_index "domains", ["registrar_id"], name: "index_domains_on_registrar_id", using: :btree add_index "domains", ["registrar_id"], name: "index_domains_on_registrar_id", using: :btree
@ -908,6 +910,17 @@ ActiveRecord::Schema.define(version: 20150423083308) do
t.text "depricated_table_but_somehow_paper_trail_tests_fails_without_it" t.text "depricated_table_but_somehow_paper_trail_tests_fails_without_it"
end end
create_table "whois_bodies", force: :cascade do |t|
t.integer "domain_id"
t.string "name"
t.text "whois_body"
t.json "whois_json"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "whois_bodies", ["domain_id"], name: "index_whois_bodies_on_domain_id", using: :btree
create_table "zonefile_settings", force: :cascade do |t| create_table "zonefile_settings", force: :cascade do |t|
t.string "origin" t.string "origin"
t.integer "ttl" t.integer "ttl"

View file

@ -14,9 +14,9 @@ namespace :whois do
task export: :environment do task export: :environment do
start = Time.zone.now.to_f start = Time.zone.now.to_f
print "-----> Delete whois database data and sync with Registry master database..." print "-----> Delete whois database data and sync with Registry master database..."
domains = Domain.pluck(:name, :whois_body, :whois_json) whois_bodies = WhoisBody.pluck(:name, :whois_body, :whois_json)
Whois::Domain.delete_all Whois::Domain.delete_all
Whois::Domain.import([:name, :whois_body, :whois_json], domains) Whois::Domain.import([:name, :whois_body, :whois_json], whois_bodies)
puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds" puts "\n-----> all done in #{(Time.zone.now.to_f - start).round(2)} seconds"
end end

View file

@ -34,13 +34,9 @@ describe Domain do
@domain.versions.should == [] @domain.versions.should == []
end end
it 'should not have whois_body' do it 'should not have whois body' do
@domain.whois_body.should == nil @domain.whois_body.should == nil
end end
it 'should not have whois json' do
@domain.whois_json.should == nil
end
end end
context 'with valid attributes' do context 'with valid attributes' do
@ -82,7 +78,7 @@ describe Domain do
end end
it 'should have whois json by default' do it 'should have whois json by default' do
@domain.whois_json.present?.should == true @domain.whois_body.whois_json.present?.should == true
end end
it 'should have whois_body present by default' do it 'should have whois_body present by default' do
@ -113,7 +109,7 @@ describe Domain do
@domain.nameservers = [ns1, ns2] @domain.nameservers = [ns1, ns2]
@domain.update_whois_body @domain.update_whois_body
@domain.whois_body.should == <<-EOS @domain.whois_body.whois_body.should == <<-EOS
Estonia .ee Top Level Domain WHOIS server Estonia .ee Top Level Domain WHOIS server
Domain: Domain: