internetee-registry/app/models/blocked_domain.rb
2023-03-16 11:28:25 +02:00

46 lines
935 B
Ruby

class BlockedDomain < ApplicationRecord
include Versions
before_save :generate_data
after_destroy :remove_data
validates :name, domain_name: true, uniqueness: true
class << self
def ransackable_attributes(auth_object = nil)
super
end
def ransackable_associations(auth_object = nil)
super
end
def by_domain(name)
where(name: name)
end
end
def name=(val)
super SimpleIDN.to_unicode(val)
end
def generate_data
return if Domain.where(name: name).any?
wr = Whois::Record.find_or_initialize_by(name: name)
wr.json = @json = generate_json # we need @json to bind to class
wr.save
end
alias_method :update_whois_record, :generate_data
def generate_json
h = HashWithIndifferentAccess.new
h[:name] = name
h[:status] = ['Blocked']
h
end
def remove_data
UpdateWhoisRecordJob.perform_later name, 'blocked'
end
end