one query for avis count

This commit is contained in:
simon lehericey 2021-04-23 14:56:17 +02:00
parent 5cfb4114ce
commit a4e813380a
2 changed files with 21 additions and 10 deletions

View file

@ -21,4 +21,17 @@ class Expert < ApplicationRecord
def self.by_email(email)
Expert.eager_load(:user).find_by(users: { email: email })
end
def avis_summary
if @avis_summary.present?
@avis_summary
else
query = <<~EOF
COUNT(*) FILTER (where answer IS NULL) AS unanswered,
COUNT(*) AS total
EOF
result = avis.select(query)[0]
@avis_summary = { unanswered: result.unanswered, total: result.total }
end
end
end

View file

@ -21,30 +21,28 @@
- if nav_bar_profile == :instructeur && instructeur_signed_in?
- current_url = request.path_info
%ul.header-tabs
- if current_instructeur.procedures.count > 0
- if current_instructeur.procedures.any?
%li
= active_link_to "Démarches", instructeur_procedures_path, active: ['dossiers','procedures'].include?(controller_name), class: 'tab-link'
- if current_instructeur.user.expert && current_expert.avis.count > 0
- if current_instructeur.user.expert && current_expert.avis_summary[:total] > 0
%li
= active_link_to expert_all_avis_path, active: controller_name == 'avis', class: 'tab-link' do
Avis
- avis_counter = current_expert.avis.without_answer.count
- if avis_counter > 0
%span.badge.warning= avis_counter
- if current_expert.avis_summary[:unanswered] > 0
%span.badge.warning= current_expert.avis_summary[:unanswered]
- if nav_bar_profile == :expert && expert_signed_in?
%ul.header-tabs
- if current_expert.user.instructeur && current_instructeur.procedures.count > 0
- if current_expert.user.instructeur && current_instructeur.procedures.any?
%li
= active_link_to "Démarches", instructeur_procedures_path, active: ['dossiers','procedures'].include?(controller_name), class: 'tab-link'
- if current_expert.avis.count > 0
- if current_expert.avis_summary[:total] > 0
%li
= active_link_to expert_all_avis_path, active: controller_name == 'avis', class: 'tab-link' do
Avis
- avis_counter = current_expert.avis.without_answer.count
- if avis_counter > 0
%span.badge.warning= avis_counter
- if current_expert.avis_summary[:unanswered] > 0
%span.badge.warning= current_expert.avis_summary[:unanswered]
- if nav_bar_profile == :user
%ul.header-tabs