fix: avoid queries in the view

This commit is contained in:
simon lehericey 2023-10-09 22:27:45 +02:00
parent 51aba39787
commit 9861f16a05
3 changed files with 30 additions and 22 deletions

View file

@ -18,6 +18,12 @@ module Users
if user_signed_in?
set_prefilled_dossier_ownership if @prefilled_dossier&.orphan?
check_prefilled_dossier_ownership if @prefilled_dossier
revision = @revision.draft? ? @revision : @procedure.revisions.where.not(id: @procedure.draft_revision_id)
@dossiers = current_user.dossiers.visible_by_user.where(revision:)
@drafts = @dossiers.brouillon
@not_drafts = @dossiers.state_not_brouillon
@preview_dossiers = @dossiers.order(created_at: :desc).limit(3)
end
@usual_traitement_time = @procedure.stats_usual_traitement_time

View file

@ -14,22 +14,17 @@
%li= link_to t('views.shared.account.already_user'), commencer_sign_in_path(path: @procedure.path, prefill_token: @prefilled_dossier&.prefill_token), class: 'fr-btn fr-btn--secondary'
- else
- revision = @revision.draft? ? @revision : @procedure.revisions.where.not(id: @procedure.draft_revision_id)
- dossiers = current_user.dossiers.visible_by_user.where(revision:)
- drafts = dossiers.brouillon
- not_drafts = dossiers.state_not_brouillon
- if @prefilled_dossier
= render Dsfr::CalloutComponent.new(title: t(".prefilled_draft"), heading_level: 'h2') do |c|
- c.body do
%p= t('.prefilled_draft_detail_html', time_ago: time_ago_in_words(@prefilled_dossier.created_at), procedure: @procedure.libelle)
= link_to t('.go_to_prefilled_file'), url_for_dossier(@prefilled_dossier), class: 'fr-btn fr-mb-2w'
- elsif dossiers.empty?
- elsif @dossiers.empty?
= link_to t('.start_procedure'), url_for_new_dossier(@revision), class: 'fr-btn fr-mb-2w'
- elsif drafts.size == 1 && not_drafts.empty?
- dossier = drafts.first
- elsif @drafts.size == 1 && @not_drafts.empty?
- dossier = @drafts.first
= render Dsfr::CalloutComponent.new(title: t(".already_draft"), heading_level: 'h2') do |c|
- c.body do
%p
@ -38,8 +33,8 @@
%li= link_to t('.continue_file'), url_for_dossier(dossier), class: 'fr-btn'
%li= link_to t('.start_new_file'), url_for_new_dossier(@revision), class: 'fr-btn fr-btn--secondary'
- elsif not_drafts.size == 1
- dossier = not_drafts.first
- elsif @not_drafts.size == 1
- dossier = @not_drafts.first
= render Dsfr::CalloutComponent.new(title: t(".already_not_draft"), heading_level: 'h2') do |c|
- c.body do
%p
@ -54,10 +49,9 @@
%ul.fr-btns-group.fr-btns-group--inline
%li= link_to t('.show_dossiers'), dossiers_path(procedure_id: @procedure.id), class: "fr-btn"
%li= link_to t('.start_new_file'), url_for_new_dossier(@revision), class: "fr-btn fr-btn--secondary"
- preview_dossiers = dossiers.order(created_at: :desc).limit(3)
%p= t('.already_created', count: preview_dossiers.size)
%p= t('.already_created', count: @preview_dossiers.size)
%ul
- preview_dossiers.each do |dossier|
- @preview_dossiers.each do |dossier|
%li= link_to t('.already_created_details_html',
id: number_with_html_delimiter(dossier.id),
created_at: time_ago_in_words(dossier.created_at),

View file

@ -3,10 +3,18 @@ RSpec.describe 'commencer/show', type: :view do
let(:stored_query_params) { false }
let(:procedure) { create(:procedure, :published, :for_individual, :with_service) }
let(:dossiers) { drafts + not_drafts }
let(:drafts) { [] }
let(:not_drafts) { [] }
let(:preview_dossiers) { dossiers.take(3) }
before do
assign(:procedure, procedure)
assign(:revision, procedure.published_revision)
assign(:dossiers, dossiers)
assign(:drafts, drafts)
assign(:not_drafts, not_drafts)
assign(:preview_dossiers, preview_dossiers)
if user
sign_in user
end
@ -42,33 +50,33 @@ RSpec.describe 'commencer/show', type: :view do
end
context 'and they have a pending draft' do
let!(:brouillon) { create(:dossier, user: user, procedure: procedure) }
let!(:drafts) { [create(:dossier, user: user, procedure: procedure)] }
it_behaves_like 'it renders a link to create a new dossier', 'Commencer un nouveau dossier'
it 'renders a link to resume the pending draft' do
subject
expect(rendered).to have_text(time_ago_in_words(brouillon.created_at))
expect(rendered).to have_link('Continuer à remplir mon dossier', href: brouillon_dossier_path(brouillon))
expect(rendered).to have_text(time_ago_in_words(drafts.first.created_at))
expect(rendered).to have_link('Continuer à remplir mon dossier', href: brouillon_dossier_path(drafts.first))
end
end
context 'and they have a submitted dossier' do
let!(:brouillon) { create(:dossier, user: user, procedure: procedure) }
let!(:dossier) { create(:dossier, :en_construction, :with_individual, user: user, procedure: procedure) }
let!(:drafts) { [create(:dossier, user: user, procedure: procedure)] }
let!(:not_drafts) { [create(:dossier, :en_construction, :with_individual, user: user, procedure: procedure)] }
it_behaves_like 'it renders a link to create a new dossier', 'Commencer un nouveau dossier'
it 'renders a link to the submitted dossier' do
subject
expect(rendered).to have_text(time_ago_in_words(dossier.depose_at))
expect(rendered).to have_link('Voir mon dossier', href: dossier_path(dossier))
expect(rendered).to have_text(time_ago_in_words(not_drafts.first.depose_at))
expect(rendered).to have_link('Voir mon dossier', href: dossier_path(not_drafts.first))
end
end
context 'and they have several submitted dossiers' do
let!(:brouillon) { create(:dossier, user: user, procedure: procedure) }
let!(:dossiers) { create_list(:dossier, 2, :en_construction, :with_individual, user: user, procedure: procedure) }
let!(:drafts) { [create(:dossier, user: user, procedure: procedure)] }
let!(:not_drafts) { create_list(:dossier, 2, :en_construction, :with_individual, user: user, procedure: procedure) }
it_behaves_like 'it renders a link to create a new dossier', 'Commencer un nouveau dossier'