Add validation

This commit is contained in:
Martin Lensment 2014-09-15 17:27:48 +03:00
parent 76d397465e
commit d286a67cc0
8 changed files with 88 additions and 5 deletions

View file

@ -0,0 +1,48 @@
class Admin::AdminContactsController < ApplicationController
before_action :set_domain
before_action :set_contact, only: [:destroy]
def new; end
def create
contact = Contact.find_by(id: params[:contact_id])
unless contact
flash.now[:alert] = I18n.t('shared.contact_was_not_found')
render 'new' and return
end
if @domain.admin_contacts.exists?(contact)
flash.now[:alert] = I18n.t('shared.contact_already_exists')
render 'new' and return
end
@domain.admin_contacts << contact
flash[:notice] = I18n.t('shared.contact_added')
redirect_to [:admin, @domain]
end
def destroy
unless @domain.can_remove_admin_contact?
flash[:alert] = @domain.errors[:admin_contacts].first
redirect_to [:admin, @domain] and return
end
if @domain.admin_contacts.delete(@contact)
flash[:notice] = I18n.t('shared.contact_detached')
else
flash[:alert] = I18n.t('shared.failed_to_detach_contact')
end
redirect_to [:admin, @domain]
end
private
def set_domain
@domain = Domain.find(params[:domain_id])
end
def set_contact
@contact = Contact.find(params[:id])
end
end

View file

@ -5,7 +5,7 @@ class Admin::TechContactsController < ApplicationController
def new; end
def create
contact = Contact.find_by(id: params[:tech_contact_id])
contact = Contact.find_by(id: params[:contact_id])
unless contact
flash.now[:alert] = I18n.t('shared.contact_was_not_found')

View file

@ -64,6 +64,12 @@ class Domain < ActiveRecord::Base
false
end
def can_remove_admin_contact?
return true if admin_contacts.length > 1
errors.add(:admin_contacts, :less_than_or_equal_to, { count: 1 })
false
end
def validate_nameservers_count
sg = SettingGroup.domain_validation
min, max = sg.setting(:ns_min_count).value.to_i, sg.setting(:ns_max_count).value.to_i

View file

@ -0,0 +1,22 @@
.row
.col-sm-6
%h2.text-center-xs
= "#{t('shared.new_admin_contact')}"
.col-sm-6
%h2.text-right.text-center-xs
= link_to(t('shared.back_to_domain'), [:admin, @domain], class: 'btn btn-default')
%hr
= form_tag(admin_domain_admin_contacts_path(@domain)) do |f|
.row
.col-md-12
.form-group.has-feedback
.form-group.has-feedback
= label_tag :tech_contact
= text_field_tag(:domain_contact, params[:domain_contact], class: 'form-control js-contact-typeahead', placeholder: t('shared.contact_code'), autocomplete: 'off')
%span.glyphicon.glyphicon-ok.form-control-feedback.js-typeahead-ok.hidden
%span.glyphicon.glyphicon-remove.form-control-feedback.js-typeahead-remove
= hidden_field_tag(:contact_id, params[:contact_id], class: 'js-contact-id')
.row
.col-md-12.text-right
= button_tag(t('shared.save'), class: 'btn btn-primary')

View file

@ -1,6 +1,10 @@
- panel_class = @domain.errors.messages[:admin_contacts] ? 'panel-danger' : 'panel-default'
.panel{class: panel_class}
.panel-heading= t('shared.admin_contacts')
.panel-heading.clearfix
.pull-left
= t('shared.admin_contacts')
.pull-right
= link_to(t('shared.add'), new_admin_domain_admin_contact_path(@domain), class: 'btn btn-primary btn-xs')
.table-responsive
%table.table.table-hover.table-bordered.table-condensed
%thead
@ -15,7 +19,7 @@
%td= x.email
%td
= link_to(t('shared.edit'), root_path, class: 'btn btn-primary btn-xs')
= link_to(t('shared.detach'), root_path, method: :delete, data: { confirm: t('shared.are_you_sure') }, class: 'btn btn-warning btn-xs')
= link_to(t('shared.detach'), admin_domain_admin_contact_path(@domain, x), method: :delete, data: { confirm: t('shared.are_you_sure') }, class: 'btn btn-warning btn-xs')
- if @domain.errors.messages[:admin_contacts]
%tfoot
- @domain.errors.messages[:admin_contacts].each do |x|

View file

@ -12,10 +12,10 @@
.form-group.has-feedback
.form-group.has-feedback
= label_tag :tech_contact
= text_field_tag(:domain_tech_contact, params[:domain_tech_contact], class: 'form-control js-contact-typeahead', placeholder: t('shared.contact_code'), autocomplete: 'off')
= text_field_tag(:domain_contact, params[:domain_contact], class: 'form-control js-contact-typeahead', placeholder: t('shared.contact_code'), autocomplete: 'off')
%span.glyphicon.glyphicon-ok.form-control-feedback.js-typeahead-ok.hidden
%span.glyphicon.glyphicon-remove.form-control-feedback.js-typeahead-remove
= hidden_field_tag(:tech_contact_id, params[:tech_contact_id], class: 'js-contact-id')
= hidden_field_tag(:contact_id, params[:contact_id], class: 'js-contact-id')
.row
.col-md-12.text-right
= button_tag(t('shared.save'), class: 'btn btn-primary')

View file

@ -80,6 +80,8 @@ en:
not_found: 'Contact was not found'
admin_contacts:
out_of_range: 'Admin contacts count must be between 1 - infinity'
less_than_or_equal_to: 'Admin contacts count must be less than or equal to %{count}'
greater_than_or_equal_to: 'Admin contacts count must be greater than or equal to %{count}'
nameservers:
invalid: 'Nameserver is invalid'
out_of_range: 'Nameservers count must be between %{min}-%{max}'

View file

@ -9,6 +9,7 @@ Rails.application.routes.draw do
resources :domains do
resources :nameservers
resources :tech_contacts
resources :admin_contacts
end
resources :setting_groups