111297422-blocked_domains

This commit is contained in:
Stas 2016-01-11 10:12:15 +02:00
parent 4095450fb8
commit 11d543d967
5 changed files with 465 additions and 137 deletions

View file

@ -2,20 +2,45 @@ class Admin::BlockedDomainsController < AdminController
load_and_authorize_resource
def index
bd = BlockedDomain.first_or_initialize
@blocked_domains = bd.names.join("\n")
bd = BlockedDomain.pluck(:name)
if bd
@blocked_domains = bd.to_yaml.gsub("---\n", '').gsub("-", '').gsub(/\.\.\..?\n/, '')
end
end
def create
names = params[:blocked_domains].split("\r\n").map(&:strip)
@blocked_domains = params[:blocked_domains]
bd = BlockedDomain.first_or_create
begin
params[:blocked_domains] = "---\n" if params[:blocked_domains].blank?
names = YAML.load(params[:blocked_domains])
fail if names == false
rescue
flash.now[:alert] = I18n.t('invalid_yaml')
logger.warn 'Invalid YAML'
render :index and return
end
if bd.update(names: names)
result = true
BlockedDomain.transaction do
# removing old ones
existing = BlockedDomain.any_of_domains(names).pluck(:id)
BlockedDomain.where.not(id: existing).destroy_all
#updating and adding
names.each do |name|
rec = BlockedDomain.find_or_initialize_by(name: name)
unless rec.save
result = false
raise ActiveRecord::Rollback
end
end
end
if result
flash[:notice] = I18n.t('record_updated')
redirect_to :back
else
@blocked_domains = params[:blocked_domains]
flash.now[:alert] = I18n.t('failed_to_update_record')
render :index
end

View file

@ -1,5 +1,49 @@
class BlockedDomain < ActiveRecord::Base
include Versions
before_save :generate_data
before_destroy :remove_data
after_initialize -> { self.names = [] if names.nil? }
class << self
def by_domain name
where(name: name)
end
def any_of_domains names
where(name: names)
end
end
def name= val
super SimpleIDN.to_unicode(val)
end
def generate_data
@json = generate_json
@body = generate_body
update_whois_server
end
def update_whois_server
wr = Whois::Record.find_or_initialize_by(name: name)
wr.body = @body
wr.json = @json
wr.save
end
def generate_body
template = Rails.root.join("app/views/for_models/whois_other.erb".freeze)
ERB.new(template.read, nil, "-").result(binding)
end
def generate_json
h = HashWithIndifferentAccess.new
h[:name] = self.name
h[:status] = 'Blocked'
h
end
def remove_data
Whois::Record.where(name: name).delete_all
end
end