diff --git a/app/jobs/update_whois_record_job.rb b/app/jobs/update_whois_record_job.rb index 4d41a70d0..16f4e0e79 100644 --- a/app/jobs/update_whois_record_job.rb +++ b/app/jobs/update_whois_record_job.rb @@ -47,10 +47,7 @@ class UpdateWhoisRecordJob < Que::Job end def delete_reserved(name) - Whois::Record.where(name: name).each do |r| - r.json['status'] = r.json['Reserved'].delete_if { |status| status == 'Reserved' } - r.json['status'].blank? ? r.destroy : r.save - end + remove_status_from_whois(domain_name: name, domain_status: 'Reserved') end def delete_blocked(name) @@ -60,8 +57,12 @@ class UpdateWhoisRecordJob < Que::Job def delete_disputed(name) return if Dispute.active.find_by(domain_name: name).present? - Whois::Record.where(name: name).each do |r| - r.json['status'] = r.json['status'].delete_if { |status| status == 'disputed' } + remove_status_from_whois(domain_name: name, domain_status: 'disputed') + end + + def remove_status_from_whois(domain_name:, domain_status:) + Whois::Record.where(name: domain_name).each do |r| + r.json['status'] = r.json['status'].delete_if { |status| status == domain_status } r.json['status'].blank? ? r.destroy : r.save end end diff --git a/app/models/concerns/whois_status_populate.rb b/app/models/concerns/whois_status_populate.rb new file mode 100644 index 000000000..616cc7d22 --- /dev/null +++ b/app/models/concerns/whois_status_populate.rb @@ -0,0 +1,15 @@ +module WhoisStatusPopulate + extend ActiveSupport::Concern + + def generate_json(record, domain_status:) + h = HashWithIndifferentAccess.new(name: record.name, status: [domain_status]) + return h if record.json.blank? + + status_arr = (record.json['status'] ||= []) + return record.json if status_arr.include? domain_status + + status_arr.push(domain_status) + record.json['status'] = status_arr + record.json + end +end diff --git a/app/models/dispute.rb b/app/models/dispute.rb index 7434c8e26..986aa8c96 100644 --- a/app/models/dispute.rb +++ b/app/models/dispute.rb @@ -1,4 +1,5 @@ class Dispute < ApplicationRecord + include WhoisStatusPopulate validates :domain_name, :password, :starts_at, :expires_at, presence: true before_validation :fill_empty_passwords, :set_expiry_date validate :validate_domain_name_format @@ -48,7 +49,7 @@ class Dispute < ApplicationRecord return if domain wr = Whois::Record.find_or_initialize_by(name: domain_name) - wr.json = @json = generate_json(wr) + wr.json = @json = generate_json(wr, domain_status: 'disputed') wr.save end @@ -79,18 +80,6 @@ class Dispute < ApplicationRecord record.save end - def generate_json(record) - h = HashWithIndifferentAccess.new(name: domain_name, status: ['disputed']) - return h if record.json.blank? - - status_arr = (record.json['status'] ||= []) - return record.json if status_arr.include? 'disputed' - - status_arr.push('disputed') - record.json['status'] = status_arr - record.json - end - def remove_data UpdateWhoisRecordJob.enqueue domain_name, 'disputed' end diff --git a/app/models/reserved_domain.rb b/app/models/reserved_domain.rb index 6ca22887b..4c9df3269 100644 --- a/app/models/reserved_domain.rb +++ b/app/models/reserved_domain.rb @@ -1,5 +1,6 @@ class ReservedDomain < ApplicationRecord include Versions # version/reserved_domain_version.rb + include WhoisStatusPopulate before_save :fill_empty_passwords before_save :generate_data before_save :sync_dispute_password @@ -51,24 +52,12 @@ class ReservedDomain < ApplicationRecord return if Domain.where(name: name).any? wr = Whois::Record.find_or_initialize_by(name: name) - wr.json = @json = generate_json(wr) # we need @json to bind to class + wr.json = @json = generate_json(wr, domain_status: 'Reserved') # we need @json to bind to class wr.save end alias_method :update_whois_record, :generate_data - def generate_json(record) - h = HashWithIndifferentAccess.new(name: name, status: ['Reserved']) - return h if record.json.blank? - - status_arr = (record.json['status'] ||= []) - return record.json if status_arr.include? 'Reserved' - - status_arr.push('Reserved') - record.json['status'] = status_arr - record.json - end - def remove_data UpdateWhoisRecordJob.enqueue name, 'reserved' end