From 10aabf750c4add77fd9fcc1594d102f5915d7a6a Mon Sep 17 00:00:00 2001 From: dinsmol Date: Tue, 14 Sep 2021 16:24:56 +0300 Subject: [PATCH] changed filter rules --- .../admin/registrars_controller.rb | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/registrars_controller.rb b/app/controllers/admin/registrars_controller.rb index 4e2a7cb15..1d310bfa4 100644 --- a/app/controllers/admin/registrars_controller.rb +++ b/app/controllers/admin/registrars_controller.rb @@ -6,6 +6,8 @@ module Admin helper_method :registry_vat_rate helper_method :iban_max_length + SQL_SUM_STR = 'sum(case active when TRUE then 1 else 0 end)'.freeze + def index registrars = filter_by_status @q = registrars.ransack(params[:q]) @@ -58,14 +60,28 @@ module Admin def filter_by_status case params[:status] when 'Active' - Registrar.includes(:accounts, :api_users).where.not(api_users: { id: nil }).ordered + active_registrars when 'Inactive' - Registrar.includes(:accounts, :api_users).where(api_users: { id: nil }).ordered + inactive_registrars else Registrar.includes(:accounts, :api_users).ordered end end + def active_registrars + Registrar.includes(:accounts, :api_users).where( + id: ApiUser.having("#{SQL_SUM_STR} > 0").group(:registrar_id).pluck(:registrar_id) + ).ordered + end + + def inactive_registrars + Registrar.includes(:accounts, :api_users).where(api_users: { id: nil }).or( + Registrar.includes(:accounts, :api_users).where( + id: ApiUser.having("#{SQL_SUM_STR} = 0").group(:registrar_id).pluck(:registrar_id) + ) + ).ordered + end + def set_registrar_status_filter params[:status] ||= 'Active' end