From e077ba7ee8f00da1353e55b2877fb75696802f04 Mon Sep 17 00:00:00 2001 From: Stas Date: Mon, 15 Feb 2016 12:38:05 +0200 Subject: [PATCH] 105842700-contact_and_domain_query_change --- .../registrant/contacts_controller.rb | 5 ++-- app/models/business_registry_cache.rb | 12 ++++++--- app/models/contact.rb | 25 +++++++++++++++++++ .../registrants/partials/_domains.haml | 2 +- 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/app/controllers/registrant/contacts_controller.rb b/app/controllers/registrant/contacts_controller.rb index dfef2723a..fd4d32a37 100644 --- a/app/controllers/registrant/contacts_controller.rb +++ b/app/controllers/registrant/contacts_controller.rb @@ -1,14 +1,15 @@ class Registrant::ContactsController < RegistrantController def show - @contact = Contact.where(id: contacts).find(params[:id]) + @contact = Contact.where(id: contacts).find_by(id: params[:id]) + @current_user = current_user authorize! :read, @contact end def contacts ident_cc, ident = @current_user.registrant_ident.to_s.split '-' begin - BusinessRegistryCache.fetch_by_ident_and_cc(ident, ident_cc).associated_contacts + DomainContact.where(domain_id: BusinessRegistryCache.fetch_by_ident_and_cc(ident, ident_cc).associated_domain_ids).pluck(:contact_id) rescue Soap::Arireg::NotAvailableError => error flash[:notice] = I18n.t(error.json[:message]) Rails.logger.fatal("[EXCEPTION] #{error.to_s}") diff --git a/app/models/business_registry_cache.rb b/app/models/business_registry_cache.rb index cab39e7ee..f73a5cfe8 100644 --- a/app/models/business_registry_cache.rb +++ b/app/models/business_registry_cache.rb @@ -28,16 +28,20 @@ class BusinessRegistryCache < ActiveRecord::Base contact_ids end - def associated_domains - domains = [] + def associated_domain_ids + domain_ids = [] contact_ids = associated_contacts unless contact_ids.blank? - domains = DomainContact.distinct.where(contact_id: contact_ids).pluck(:domain_id) + domain_ids = DomainContact.distinct.where(contact_id: contact_ids).pluck(:domain_id) end - Domain.includes(:registrar, :registrant).where(id: domains) + domain_ids + end + + def associated_domains + Domain.includes(:registrar, :registrant).where(id: associated_domain_ids) end class << self diff --git a/app/models/contact.rb b/app/models/contact.rb index 1d6f069e5..035fedf59 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -407,6 +407,31 @@ class Contact < ActiveRecord::Base domains end + def all_registrant_domains(page: nil, per: nil, params: {}, registrant: nil) + + if registrant + sorts = params.fetch(:sort, {}).first || [] + sort = Domain.column_names.include?(sorts.first) ? sorts.first : "valid_to" + order = {"asc"=>"desc", "desc"=>"asc"}[sorts.second] || "desc" + + domain_ids = DomainContact.distinct.where(contact_id: registrant.id).pluck(:domain_id) + + domains = Domain.where(id: domain_ids).includes(:registrar).page(page).per(per) + if sorts.first == "registrar_name".freeze + domains = domains.includes(:registrar).where.not(registrars: {id: nil}).order("registrars.name #{order} NULLS LAST") + else + domains = domains.order("#{sort} #{order} NULLS LAST") + end + + domain_c = Hash.new([]) + registrant_domains.where(id: domains.map(&:id)).each{|d| domain_c[d.id] |= ["Registrant".freeze] } + DomainContact.where(contact_id: id, domain_id: domains.map(&:id)).each{|d| domain_c[d.domain_id] |= [d.type] } + domains.each{|d| d.roles = domain_c[d.id].uniq} + domains + end + [] + end + def set_linked statuses << LINKED if statuses.detect { |s| s == LINKED }.blank? end diff --git a/app/views/registrant/registrants/partials/_domains.haml b/app/views/registrant/registrants/partials/_domains.haml index d0180532d..e528a331d 100644 --- a/app/views/registrant/registrants/partials/_domains.haml +++ b/app/views/registrant/registrants/partials/_domains.haml @@ -1,4 +1,4 @@ -- domains = contact.all_domains(page: params[:domain_page], per: 20, params: params) +- domains = contact.all_registrant_domains(page: params[:domain_page], per: 20, params: params, current_user: current_user) #contacts.panel.panel-default .panel-heading .pull-left