mirror of
https://github.com/internetee/registry.git
synced 2025-05-17 01:47:18 +02:00
Contact CRUD in admin section
This commit is contained in:
parent
d6543f2148
commit
c644a297e1
6 changed files with 149 additions and 7 deletions
|
@ -1,15 +1,51 @@
|
|||
class Admin::ContactsController < ApplicationController
|
||||
before_action :set_contact, only: [:show]
|
||||
|
||||
def new
|
||||
@contact = Contact.new
|
||||
end
|
||||
# TODO created_by and updated_by ids
|
||||
before_action :set_contact, only: [:show, :destroy, :edit, :update]
|
||||
|
||||
def index
|
||||
@q = Contact.search(params[:q])
|
||||
@contacts = @q.result.page(params[:page])
|
||||
end
|
||||
|
||||
def new
|
||||
@contact = Contact.new
|
||||
@contact.build_local_address
|
||||
@contact.build_international_address
|
||||
end
|
||||
|
||||
def create
|
||||
@contact = Contact.new(contact_params)
|
||||
binding.pry
|
||||
@contact.generate_code
|
||||
if @contact.save
|
||||
flash[:notice] = I18n.t('shared.contact_added')
|
||||
redirect_to [:admin, @contact]
|
||||
else
|
||||
flash[:alert] = I18n.t('shared.failed_to_create_contact')
|
||||
render "new"
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
if @contact.destroy_and_clean
|
||||
flash[:notice] = I18n.t('shared.contact_deleted')
|
||||
redirect_to admin_contacts_path
|
||||
else
|
||||
flash[:alert] = I18n.t('shared.failed_to_delete_contact')
|
||||
redirect_to [:admin, @contact]
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
if @contact.update_attributes(contact_params)
|
||||
flash[:notice] = I18n.t('shared.contact_updated')
|
||||
redirect_to [:admin, @contact]
|
||||
else
|
||||
flash[:alert] = I18n.t('shared.failed_to_update_contact')
|
||||
redirect_to [:admin, @contact]
|
||||
end
|
||||
end
|
||||
|
||||
def search
|
||||
render json: Contact.search_by_query(params[:q])
|
||||
end
|
||||
|
@ -19,4 +55,10 @@ class Admin::ContactsController < ApplicationController
|
|||
def set_contact
|
||||
@contact = Contact.find(params[:id])
|
||||
end
|
||||
|
||||
def contact_params
|
||||
params.require(:contact).permit( :email, :phone, :fax, :ident_type, :ident, :auth_info,
|
||||
local_address_attributes: [:city, :street, :zip, :street2, :street3, :name, :org_name, :country_id],
|
||||
international_address_attributes: [:city, :street, :zip, :street2, :street3, :name, :org_name, :country_id])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -19,6 +19,7 @@ class Contact < ActiveRecord::Base
|
|||
validates :code, :phone, :email, :ident, presence: true
|
||||
|
||||
validate :ident_must_be_valid
|
||||
validate :presence_of_one_address
|
||||
|
||||
validates :phone, format: /\+[0-9]{1,3}\.[0-9]{1,14}?/ # /\+\d{3}\.\d+/
|
||||
validates :email, format: /@/
|
||||
|
@ -50,6 +51,12 @@ class Contact < ActiveRecord::Base
|
|||
end
|
||||
##
|
||||
|
||||
def presence_of_one_address
|
||||
return true if local_address || international_address
|
||||
errors.add(:local_address, 'Local or international address must be present')
|
||||
errors.add(:international_address, 'Local or international address must be present')
|
||||
end
|
||||
|
||||
def ident_must_be_valid
|
||||
# TODO: Ident can also be passport number or company registry code.
|
||||
# so have to make changes to validations (and doc/schema) accordingly
|
||||
|
|
|
@ -1 +1,77 @@
|
|||
to be done
|
||||
= form_for([:admin, @contact]) do |f|
|
||||
- if @contact.errors.any?
|
||||
- @contact.errors.each do |attr, err|
|
||||
= err
|
||||
%br
|
||||
- if @contact.errors.any?
|
||||
%hr
|
||||
|
||||
.row
|
||||
.col-md-6.text-left
|
||||
.form-group
|
||||
= f.label :email
|
||||
= f.text_field(:email, class: 'form-control')
|
||||
= f.label :phone
|
||||
= f.text_field(:phone, class: 'form-control')
|
||||
= f.label :fax
|
||||
= f.text_field(:fax, class: 'form-control')
|
||||
|
||||
.col-md-6.text-left
|
||||
.form-group
|
||||
= f.label :ident_type
|
||||
= f.select :ident_type, options_for_select(Contact::IDENT_TYPES, @contact.ident_type), {}, {class: 'form-control'}
|
||||
= f.label :ident
|
||||
= f.text_field(:ident, class: 'form-control')
|
||||
= f.label :auth_info
|
||||
= f.text_field(:auth_info, class: 'form-control')
|
||||
|
||||
%hr
|
||||
.row
|
||||
.col-md-6.text-left
|
||||
%h3
|
||||
International Address
|
||||
.form-group
|
||||
= f.fields_for :international_address do |ia|
|
||||
= ia.label :name
|
||||
= ia.text_field(:name, class: 'form-control')
|
||||
= ia.label :org_name
|
||||
= ia.text_field(:org_name, class: 'form-control')
|
||||
|
||||
= ia.label :country_id, t(:country)
|
||||
= ia.collection_select :country_id, Country.all, :id, :name,{}, { class: 'form-control' }
|
||||
|
||||
= ia.label :city
|
||||
= ia.text_field(:city, class: 'form-control')
|
||||
= ia.label :street
|
||||
= ia.text_field(:street, class: 'form-control')
|
||||
= ia.label :street2
|
||||
= ia.text_field(:street2, class: 'form-control')
|
||||
= ia.label :street3
|
||||
= ia.text_field(:street2, class: 'form-control')
|
||||
|
||||
|
||||
.col-md-6.text-left
|
||||
%h3
|
||||
Local Address
|
||||
= f.fields_for :local_address do |ia|
|
||||
= ia.label :name
|
||||
= ia.text_field(:name, class: 'form-control')
|
||||
|
||||
= ia.label :country_id, t(:country)
|
||||
= ia.collection_select :country_id, Country.all, :id, :name,{}, { class: 'form-control' }
|
||||
|
||||
= ia.label :org_name
|
||||
= ia.text_field(:org_name, class: 'form-control')
|
||||
|
||||
= ia.label :city
|
||||
= ia.text_field(:city, class: 'form-control')
|
||||
= ia.label :street
|
||||
= ia.text_field(:street, class: 'form-control')
|
||||
= ia.label :street2
|
||||
= ia.text_field(:street2, class: 'form-control')
|
||||
= ia.label :street3
|
||||
= ia.text_field(:street2, class: 'form-control')
|
||||
|
||||
.row
|
||||
.col-md-12.text-right
|
||||
= button_tag(t('shared.save'), class: 'btn btn-primary')
|
||||
|
|
9
app/views/admin/contacts/edit.haml
Normal file
9
app/views/admin/contacts/edit.haml
Normal file
|
@ -0,0 +1,9 @@
|
|||
.row
|
||||
.col-sm-6
|
||||
%h2.text-center-xs
|
||||
= "#{t('shared.edit_contact')}"
|
||||
.col-sm-6
|
||||
%h2.text-right.text-center-xs
|
||||
= link_to(t('shared.back_to_contact'), [:admin, @contact], class: 'btn btn-default')
|
||||
%hr
|
||||
= render 'form'
|
|
@ -259,3 +259,9 @@ en:
|
|||
failed_to_add_domain: 'Failed to add domain!'
|
||||
domain_added: 'Domain added!'
|
||||
new_contact: 'New contact'
|
||||
failed_to_create_contact: 'Failed to create contact'
|
||||
contact_deleted: 'Contact deleted'
|
||||
failed_to_delete_contact: 'Failed to delete contact'
|
||||
edit_contact: 'Edit contact'
|
||||
failed_to_update_contact: 'Failed to update contact'
|
||||
contact_updated: 'Contact updated'
|
||||
|
|
|
@ -25,7 +25,9 @@ describe Contact do
|
|||
code: ['Required parameter missing - code'],
|
||||
phone: ['Required parameter missing - phone', 'Phone nr is invalid'],
|
||||
email: ['Required parameter missing - email', 'Email is invalid'],
|
||||
ident: ['Required parameter missing - ident']
|
||||
ident: ['Required parameter missing - ident'],
|
||||
local_address: ['Local or international address must be present'],
|
||||
international_address: ['Local or international address must be present']
|
||||
})
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue