Refactor zones

- Rename "zonefile_setting" to "zone"
- Remove version

#475
This commit is contained in:
Artur Beljajev 2017-04-20 17:20:36 +03:00
parent f1d7e53734
commit bff7437277
51 changed files with 425 additions and 389 deletions

View file

@ -0,0 +1,64 @@
module Admin
module DNS
class ZonesController < AdminController
#load_and_authorize_resource(class: DNS::Zone)
skip_authorization_check
before_action :load_zone, only: %i[edit update destroy]
def index
@zones = ::DNS::Zone.all
end
def new
@zone = ::DNS::Zone.new
end
def create
@zone = ::DNS::Zone.new(zone_params)
if @zone.save
flash[:notice] = t('.created')
redirect_to_index
else
render :new
end
end
def edit
@zone = ::DNS::Zone.find(params[:id])
end
def update
if @zone.update(zone_params)
flash[:notice] = t('.updated')
redirect_to_index
else
render :edit
end
end
def destroy
@zone.destroy!
flash[:notice] = t('.destroyed')
redirect_to_index
end
private
def load_zone
@zone = ::DNS::Zone.find(params[:id])
end
def zone_params
params.require(:zone).permit(
:origin, :ttl, :refresh, :retry, :expire, :minimum_ttl, :email,
:master_nameserver, :ns_records, :a_records, :a4_records
)
end
def redirect_to_index
redirect_to admin_zones_url
end
end
end
end

View file

@ -1,60 +0,0 @@
class Admin::ZonefileSettingsController < AdminController
load_and_authorize_resource
before_action :set_zonefile_setting, only: [:update, :edit]
def index
@zonefile_settings = ZonefileSetting.all
end
def new
@zonefile_setting = ZonefileSetting.new
end
def create
@zonefile_setting = ZonefileSetting.new(zonefile_setting_params)
if @zonefile_setting.save
flash[:notice] = I18n.t('record_created')
redirect_to admin_zonefile_settings_path
else
flash.now[:alert] = I18n.t('failed_to_create_record')
render 'new'
end
end
def edit
@zonefile_setting = ZonefileSetting.find(params[:id])
end
def update
if @zonefile_setting.update(zonefile_setting_params)
flash[:notice] = I18n.t('record_updated')
redirect_to admin_zonefile_settings_path
else
flash.now[:alert] = I18n.t('failed_to_update_record')
render 'edit'
end
end
def destroy
if @zonefile_setting.destroy
flash[:notice] = I18n.t('record_deleted')
redirect_to admin_zonefile_settings_path
else
flash.now[:alert] = I18n.t('failed_to_delete_record')
render 'edit'
end
end
private
def set_zonefile_setting
@zonefile_setting = ZonefileSetting.find(params[:id])
end
def zonefile_setting_params
params.require(:zonefile_setting).permit(
:origin, :ttl, :refresh, :retry, :expire, :minimum_ttl, :email,
:master_nameserver, :ns_records, :a_records, :a4_records
)
end
end

View file

@ -3,7 +3,7 @@ class Admin::ZonefilesController < ApplicationController
# TODO: Refactor this
def create
if ZonefileSetting.origins.include?(params[:origin])
if DNS::Zone.origins.include?(params[:origin])
@zonefile = ActiveRecord::Base.connection.execute(
"select generate_zonefile('#{params[:origin]}')"

View file

@ -93,7 +93,7 @@ class Ability
can :manage, Setting
can :manage, BlockedDomain
can :manage, ReservedDomain
can :manage, ZonefileSetting
can :manage, DNS::Zone
can :manage, DomainVersion
can :manage, ContactVersion
can :manage, Pricelist

5
app/models/dns.rb Normal file
View file

@ -0,0 +1,5 @@
module DNS
def self.use_relative_model_naming?
true
end
end

48
app/models/dns/zone.rb Normal file
View file

@ -0,0 +1,48 @@
module DNS
class Zone < ActiveRecord::Base
validates :origin, :ttl, :refresh, :retry, :expire, :minimum_ttl, :email, :master_nameserver, presence: true
validates :ttl, :refresh, :retry, :expire, :minimum_ttl, numericality: { only_integer: true }
validates :origin, uniqueness: true
before_destroy :check_for_dependencies
def check_for_dependencies
dc = Domain.where("name ILIKE ?", "%.#{origin}").count
return if dc == 0
errors.add(:base, I18n.t('there_are_count_domains_in_this_zone', count: dc))
false
end
def self.generate_zonefiles
pluck(:origin).each do |origin|
generate_zonefile(origin)
end
end
def self.generate_zonefile(origin)
filename = "#{origin}.zone"
STDOUT << "#{Time.zone.now.utc} - Generating zonefile #{filename}\n"
zf = ActiveRecord::Base.connection.execute(
"select generate_zonefile('#{origin}')"
)[0]['generate_zonefile']
File.open("#{ENV['zonefile_export_dir']}/#{filename}", 'w') { |f| f.write(zf) }
STDOUT << "#{Time.zone.now.utc} - Successfully generated zonefile #{filename}\n"
end
def self.origins
pluck(:origin)
end
def to_s
origin
end
def to_partial_path
'zone'
end
end
end

View file

@ -1,5 +0,0 @@
class ZonefileSettingVersion < PaperTrail::Version
include VersionSession
self.table_name = :log_zonefile_settings
self.sequence_name = :log_zonefile_settings_id_seq
end

View file

@ -1,42 +0,0 @@
class ZonefileSetting < ActiveRecord::Base
include Versions # version/zonefile_setting_version.rb
validates :origin, :ttl, :refresh, :retry, :expire, :minimum_ttl, :email, :master_nameserver, presence: true
validates :ttl, :refresh, :retry, :expire, :minimum_ttl, numericality: { only_integer: true }
validates :origin, uniqueness: true
before_destroy :check_for_dependencies
def check_for_dependencies
dc = Domain.where("name ILIKE ?", "%.#{origin}").count
return if dc == 0
errors.add(:base, I18n.t('there_are_count_domains_in_this_zone', count: dc))
false
end
def self.generate_zonefiles
pluck(:origin).each do |origin|
generate_zonefile(origin)
end
end
def self.generate_zonefile(origin)
filename = "#{origin}.zone"
STDOUT << "#{Time.zone.now.utc} - Generating zonefile #{filename}\n"
zf = ActiveRecord::Base.connection.execute(
"select generate_zonefile('#{origin}')"
)[0]['generate_zonefile']
File.open("#{ENV['zonefile_export_dir']}/#{filename}", 'w') { |f| f.write(zf) }
STDOUT << "#{Time.zone.now.utc} - Successfully generated zonefile #{filename}\n"
end
def self.origins
pluck(:origin)
end
def to_s
origin
end
end

View file

@ -12,7 +12,7 @@ class DomainNameValidator < ActiveModel::EachValidator
return true unless value
value = value.mb_chars.downcase.strip
origins = ZonefileSetting.origins
origins = DNS::Zone.origins
# if someone tries to register an origin domain, let this validation pass
# the error will be caught in blocked domains validator
return true if origins.include?(value)
@ -38,7 +38,7 @@ class DomainNameValidator < ActiveModel::EachValidator
def validate_blocked(value)
return true unless value
return false if BlockedDomain.where(name: value).count > 0
ZonefileSetting.where(origin: value).count.zero?
DNS::Zone.where(origin: value).count.zero?
end
end
end

View file

@ -31,7 +31,7 @@
%li.divider
%li.dropdown-header= t(:system)
%li= link_to t(:settings), admin_settings_path
%li= link_to t(:zonefile), admin_zonefile_settings_path
%li= link_to t('.zones'), admin_zones_path
%li= link_to t('.blocked_domains'), admin_blocked_domains_path
%li= link_to t('.reserved_domains'), admin_reserved_domains_path
%li= link_to t(:mail_templates), admin_mail_templates_path

View file

@ -1,5 +1,4 @@
= form_for [:admin, @zonefile_setting], html: { class: 'form-horizontal' } do |f|
= form_for [:admin, zone], html: { class: 'form-horizontal' } do |f|
.row
.col-md-8
#domain-statuses
@ -9,7 +8,7 @@
.col-md-4.control-label
= f.label :origin
.col-md-8
- if @zonefile_setting.persisted?
- if zone.persisted?
= f.text_field :origin, class: 'form-control', disabled: true
- else
= f.text_field :origin, class: 'form-control'
@ -77,4 +76,4 @@
%hr
.row
.col-md-8.text-right
%button.btn.btn-primary= t(:save)
%button.btn.btn-success= t(".#{zone.new_record? ? 'create' : 'update'}_btn")

View file

@ -0,0 +1,9 @@
<tr>
<td><%= zone.origin %></td>
<td>
<%= link_to t('.edit_btn'), edit_admin_zone_path(zone), class: 'btn btn-xs btn-primary' %>
<%= link_to t('.generate_zone_file_btn'),
admin_zonefiles_path(origin: zone.origin),
method: 'post', class: 'btn btn-xs btn-primary' %>
</td>
</tr>

View file

@ -0,0 +1,20 @@
<ol class="breadcrumb">
<li><%= link_to t('admin.dns.zones.index.title'), admin_zones_path %></li>
</ol>
<div class="page-header">
<div class="row">
<div class="col-sm-10">
<h1><%= t '.title' %></h1>
</div>
<div class="col-sm-2 text-right">
<%= link_to(t('.delete_btn'), admin_zone_path(@zone),
method: :delete,
data: { confirm: t('.delete_btn_confirm') },
class: 'btn btn-danger') %>
</div>
</div>
</div>
<%= render 'form', zone: @zone %>

View file

@ -0,0 +1,28 @@
<div class="page-header">
<div class="row">
<div class="col-sm-10">
<h1><%= t '.title' %></h1>
</div>
<div class="col-sm-2 text-right">
<%= link_to t('.new_btn'), new_admin_zone_path, class: 'btn btn-primary' %>
</div>
</div>
</div>
<% if @zones.present? %>
<table class="table table-hover table-bordered table-wrapped">
<thead>
<tr>
<th><%= DNS::Zone.human_attribute_name :origin %></th>
<th></th>
</tr>
</thead>
<tbody>
<%= render @zones %>
</tbody>
</table>
<% else %>
<div class="alert alert-info"><%= t '.not_found' %></div>
<% end %>

View file

@ -0,0 +1,9 @@
<ol class="breadcrumb">
<li><%= link_to t('admin.dns.zones.index.title'), admin_zones_path %></li>
</ol>
<div class="page-header">
<h1><%= t '.title' %></h1>
</div>
<%= render 'form', zone: @zone %>

View file

@ -1,7 +0,0 @@
- content_for :actions do
= link_to(t(:back), admin_zonefile_settings_path, class: 'btn btn-default')
= link_to(t(:delete), admin_zonefile_setting_path(@zonefile_setting),
method: :delete, data: { confirm: t(:are_you_sure) }, class: 'btn btn-danger')
= render 'shared/title', name: t(:edit_zone)
= render 'form'

View file

@ -1,22 +0,0 @@
- content_for :actions do
= link_to(t(:new), new_admin_zonefile_setting_path, class: 'btn btn-primary')
= render 'shared/title', name: t(:zonefile_settings)
.row
.col-md-12
.table-responsive
%table.table.table-hover.table-bordered.table-condensed
%thead
%tr
%th{class: 'col-xs-10'}
= t(:origin)
%th{class: 'col-xs-2'}
= t(:action)
%tbody
- @zonefile_settings.each do |x|
%tr
%td= link_to(x, edit_admin_zonefile_setting_path(x))
%td
= link_to(t(:generate_zonefile),
admin_zonefiles_path(origin: x.origin),
method: 'post', class: 'btn btn-xs btn-primary')

View file

@ -1,5 +0,0 @@
- content_for :actions do
= link_to(t(:back), admin_zonefile_settings_path, class: 'btn btn-default')
= render 'shared/title', name: t(:new_zone)
= render 'form'