mirror of
https://github.com/internetee/registry.git
synced 2025-05-16 09:27:19 +02:00
WhoisBody refactored to has_one
This commit is contained in:
parent
82fd2db963
commit
db81a9e7bc
6 changed files with 62 additions and 27 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
15
db/migrate/20150422092514_add_whois_body_to_registry.rb
Normal file
15
db/migrate/20150422092514_add_whois_body_to_registry.rb
Normal 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
|
17
db/schema.rb
17
db/schema.rb
|
@ -294,13 +294,15 @@ ActiveRecord::Schema.define(version: 20150423083308) do
|
|||
t.string "period_unit", limit: 1
|
||||
t.string "creator_str"
|
||||
t.string "updator_str"
|
||||
t.text "whois_body"
|
||||
t.integer "legacy_id"
|
||||
t.integer "legacy_registrar_id"
|
||||
t.integer "legacy_registrant_id"
|
||||
t.json "whois_json"
|
||||
t.datetime "outzone_at"
|
||||
t.datetime "delete_at"
|
||||
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", ["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"
|
||||
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|
|
||||
t.string "origin"
|
||||
t.integer "ttl"
|
||||
|
|
|
@ -14,9 +14,9 @@ namespace :whois do
|
|||
task export: :environment do
|
||||
start = Time.zone.now.to_f
|
||||
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.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"
|
||||
end
|
||||
|
||||
|
|
|
@ -34,13 +34,9 @@ describe Domain do
|
|||
@domain.versions.should == []
|
||||
end
|
||||
|
||||
it 'should not have whois_body' do
|
||||
it 'should not have whois body' do
|
||||
@domain.whois_body.should == nil
|
||||
end
|
||||
|
||||
it 'should not have whois json' do
|
||||
@domain.whois_json.should == nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'with valid attributes' do
|
||||
|
@ -82,7 +78,7 @@ describe Domain do
|
|||
end
|
||||
|
||||
it 'should have whois json by default' do
|
||||
@domain.whois_json.present?.should == true
|
||||
@domain.whois_body.whois_json.present?.should == true
|
||||
end
|
||||
|
||||
it 'should have whois_body present by default' do
|
||||
|
@ -113,7 +109,7 @@ describe Domain do
|
|||
@domain.nameservers = [ns1, ns2]
|
||||
|
||||
@domain.update_whois_body
|
||||
@domain.whois_body.should == <<-EOS
|
||||
@domain.whois_body.whois_body.should == <<-EOS
|
||||
Estonia .ee Top Level Domain WHOIS server
|
||||
|
||||
Domain:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue