mirror of
https://github.com/internetee/registry.git
synced 2025-05-18 18:29:40 +02:00
Add validation
This commit is contained in:
parent
76d397465e
commit
d286a67cc0
8 changed files with 88 additions and 5 deletions
48
app/controllers/admin/admin_contacts_controller.rb
Normal file
48
app/controllers/admin/admin_contacts_controller.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue