From 3d2c9c01a6e0c4b49e3914ff59399cbb050b2f56 Mon Sep 17 00:00:00 2001 From: Vladimir Krylov Date: Thu, 22 Oct 2015 09:49:36 +0300 Subject: [PATCH] make contacts searchable --- .../javascripts/registrar-manifest.coffee | 2 ++ .../javascripts/registrar/autocomplete.coffee | 27 +++++++++++++++++++ .../registrar/domains_controller.rb | 22 ++++++++++----- app/models/contact.rb | 4 +++ .../domains/form_partials/_contacts.haml | 6 ++--- .../domains/form_partials/_general.haml | 6 ++--- config/routes.rb | 1 + 7 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 app/assets/javascripts/registrar/autocomplete.coffee diff --git a/app/assets/javascripts/registrar-manifest.coffee b/app/assets/javascripts/registrar-manifest.coffee index 50d127bc5..29b950fb4 100644 --- a/app/assets/javascripts/registrar-manifest.coffee +++ b/app/assets/javascripts/registrar-manifest.coffee @@ -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 diff --git a/app/assets/javascripts/registrar/autocomplete.coffee b/app/assets/javascripts/registrar/autocomplete.coffee new file mode 100644 index 000000000..f2a846cad --- /dev/null +++ b/app/assets/javascripts/registrar/autocomplete.coffee @@ -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 \ No newline at end of file diff --git a/app/controllers/registrar/domains_controller.rb b/app/controllers/registrar/domains_controller.rb index 374f09d4a..a02fb4aa1 100644 --- a/app/controllers/registrar/domains_controller.rb +++ b/app/controllers/registrar/domains_controller.rb @@ -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 diff --git a/app/models/contact.rb b/app/models/contact.rb index b6be9f76d..adf375041 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -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 diff --git a/app/views/registrar/domains/form_partials/_contacts.haml b/app/views/registrar/domains/form_partials/_contacts.haml index 5781d5d45..931f206b5 100644 --- a/app/views/registrar/domains/form_partials/_contacts.haml +++ b/app/views/registrar/domains/form_partials/_contacts.haml @@ -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 diff --git a/app/views/registrar/domains/form_partials/_general.haml b/app/views/registrar/domains/form_partials/_general.haml index 8057a1848..e4c124803 100644 --- a/app/views/registrar/domains/form_partials/_general.haml +++ b/app/views/registrar/domains/form_partials/_general.haml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 5431cdbf4..d8f45180e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -56,6 +56,7 @@ Rails.application.routes.draw do get 'info' get 'check' get 'delete' + get 'search_contacts' end end