mirror of
https://github.com/internetee/registry.git
synced 2025-05-17 09:57:23 +02:00
make contacts searchable
This commit is contained in:
parent
b088d39b0e
commit
3d2c9c01a6
7 changed files with 56 additions and 12 deletions
|
@ -4,6 +4,7 @@
|
|||
#= require jquery.validate.additional-methods
|
||||
#= require turbolinks
|
||||
#= require bootstrap-sprockets
|
||||
#= require typeahead.bundle.min
|
||||
#= require jquery.nested_attributes
|
||||
#= require shared/jquery.validate.bootstrap
|
||||
#= require jquery-ui/datepicker
|
||||
|
@ -11,4 +12,5 @@
|
|||
|
||||
#= require shared/general
|
||||
|
||||
#= require registrar/autocomplete
|
||||
#= require registrar/application
|
||||
|
|
27
app/assets/javascripts/registrar/autocomplete.coffee
Normal file
27
app/assets/javascripts/registrar/autocomplete.coffee
Normal file
|
@ -0,0 +1,27 @@
|
|||
class @Autocomplete
|
||||
constructor: ->
|
||||
@buildAutocomplete(el) for el in document.querySelectorAll('[data-autocomplete]')
|
||||
|
||||
buildAutocomplete: (el)->
|
||||
name = el.dataset.autocomplete[1..-1].replace(/\//g, "_") # cahcing
|
||||
|
||||
$(el).typeahead 'destroy'
|
||||
$(el).typeahead(
|
||||
name: name,
|
||||
highlight: true,
|
||||
hint: false
|
||||
,
|
||||
displayKey: "display_key"
|
||||
source: (query, syncResults)->
|
||||
$.getJSON "#{el.dataset.autocomplete}?query=#{query}", (data)->
|
||||
syncResults(data)
|
||||
|
||||
|
||||
).on('typeahead:selected', (e, item) ->
|
||||
console.log e.currentTarget.id
|
||||
orig = document.querySelector('#' + e.currentTarget.id.replace(/_helper$/, ''))
|
||||
orig.value = item.value
|
||||
)
|
||||
|
||||
$(document).on "ready page:load", ->
|
||||
new Autocomplete
|
|
@ -1,6 +1,6 @@
|
|||
class Registrar::DomainsController < Registrar::DeppController # EPP controller
|
||||
before_action :init_domain, except: :new
|
||||
before_action :init_contacts_autocomplete_map, only: [:new, :edit, :create, :update]
|
||||
helper_method :contacts
|
||||
|
||||
# rubocop: disable Metrics/PerceivedComplexity
|
||||
# rubocop: disable Metrics/CyclomaticComplexity
|
||||
|
@ -138,17 +138,27 @@ class Registrar::DomainsController < Registrar::DeppController # EPP controller
|
|||
end
|
||||
end
|
||||
|
||||
def search_contacts
|
||||
authorize! :create, Depp::Domain
|
||||
|
||||
scope = current_user.registrar.contacts.limit(10)
|
||||
if params[:query].present?
|
||||
escaped_str = ActiveRecord::Base.connection.quote_string params[:query]
|
||||
scope = scope.where("name ilike '%#{escaped_str}%' OR code ilike '%#{escaped_str}%' ")
|
||||
end
|
||||
|
||||
render json: scope.pluck(:name, :code).map { |c| {display_key: "#{c.second} #{c.first}", value: c.second} }
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def init_domain
|
||||
@domain = Depp::Domain.new(current_user: depp_current_user)
|
||||
end
|
||||
|
||||
def init_contacts_autocomplete_map
|
||||
@contacts_autocomplete_map ||=
|
||||
current_user.registrar.contacts.pluck(:name, :code).map { |c| ["#{c.second} #{c.first}", c.second] }
|
||||
# @priv_contacts_autocomplete_map ||=
|
||||
# current_user.registrar.priv_contacts.pluck(:name, :code).map { |c| ["#{c.second} #{c.first}", c.second] }
|
||||
|
||||
def contacts
|
||||
current_user.registrar.contacts
|
||||
end
|
||||
|
||||
def normalize_search_parameters
|
||||
|
|
|
@ -345,6 +345,10 @@ class Contact < ActiveRecord::Base
|
|||
end
|
||||
end
|
||||
|
||||
def search_name
|
||||
"#{code} #{name}"
|
||||
end
|
||||
|
||||
def set_linked
|
||||
statuses << LINKED if statuses.detect { |s| s == LINKED }.blank?
|
||||
end
|
||||
|
|
|
@ -18,9 +18,8 @@
|
|||
.col-md-3.control-label
|
||||
= label_tag "domain_contacts_attributes_#{k}_code", t(:id), class: 'required'
|
||||
.col-md-7.has-feedback
|
||||
= select_tag "domain[contacts_attributes][#{k}][code]",
|
||||
options_for_select(@contacts_autocomplete_map, selected: v['code']),
|
||||
include_blank: true, class: 'js-combobox js-contact-code', required: true
|
||||
= text_field_tag "domain[contacts_attributes][#{k}][code]", v['code'], class: "hidden"
|
||||
= text_field_tag "domain[contacts_attributes][#{k}][code_helper]", contacts.find_by(code: v['code']).try(:search_name), class: 'form-control', data: {autocomplete: search_contacts_registrar_domains_path}, required: true
|
||||
|
||||
:coffee
|
||||
clone = $('.js-contact:first').clone()
|
||||
|
@ -39,4 +38,5 @@
|
|||
# remove link for temp
|
||||
item.find('a.add-domain-contact').each (k, v) ->
|
||||
$(v).hide()
|
||||
new Autocomplete()
|
||||
$clone: clone
|
||||
|
|
|
@ -20,9 +20,9 @@
|
|||
.col-md-3.control-label
|
||||
= label_tag :domain_registrant, t(:registrant), class: 'required'
|
||||
.col-md-7
|
||||
= select_tag "domain[registrant]",
|
||||
options_for_select(@contacts_autocomplete_map, selected: @domain_params[:registrant]),
|
||||
include_blank: true, class: 'js-combobox', required: true
|
||||
= text_field_tag 'domain[registrant]', @domain_params[:registrant], class: "hidden"
|
||||
= text_field_tag 'domain[registrant_helper]', contacts.find_by(code: @domain_params[:registrant]).try(:search_name),
|
||||
class: 'form-control', data: {autocomplete: search_contacts_registrar_domains_path}, required: true
|
||||
|
||||
- unless params[:domain_name]
|
||||
.form-group
|
||||
|
|
|
@ -56,6 +56,7 @@ Rails.application.routes.draw do
|
|||
get 'info'
|
||||
get 'check'
|
||||
get 'delete'
|
||||
get 'search_contacts'
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue