From 801318b0538e7e8bdb77b08c92b80ebda97c8240 Mon Sep 17 00:00:00 2001 From: gregoirenovel Date: Thu, 28 Sep 2017 19:07:18 +0200 Subject: [PATCH] Memoize statut --- .../new_gestionnaire/procedures_controller.rb | 8 +++-- .../procedures_controller_spec.rb | 31 ++++++++++++++++--- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/app/controllers/new_gestionnaire/procedures_controller.rb b/app/controllers/new_gestionnaire/procedures_controller.rb index 876b28e97..338a26209 100644 --- a/app/controllers/new_gestionnaire/procedures_controller.rb +++ b/app/controllers/new_gestionnaire/procedures_controller.rb @@ -54,9 +54,7 @@ module NewGestionnaire @archived_dossiers = procedure.dossiers.includes(:user).archived - @statut = params[:statut].present? ? params[:statut] : 'a-suivre' - - @dossiers = case @statut + @dossiers = case statut when 'a-suivre' @a_suivre_dossiers when 'suivis' @@ -138,6 +136,10 @@ module NewGestionnaire private + def statut + @statut ||= params[:statut].present? ? params[:statut] : 'a-suivre' + end + def procedure Procedure.find(params[:procedure_id]) end diff --git a/spec/controllers/new_gestionnaire/procedures_controller_spec.rb b/spec/controllers/new_gestionnaire/procedures_controller_spec.rb index 99ed9d86c..bd11a7e00 100644 --- a/spec/controllers/new_gestionnaire/procedures_controller_spec.rb +++ b/spec/controllers/new_gestionnaire/procedures_controller_spec.rb @@ -172,15 +172,22 @@ describe NewGestionnaire::ProceduresController, type: :controller do context "when logged in" do before do sign_in(gestionnaire) - get :show, params: { procedure_id: procedure.id } end - it { expect(response).to have_http_status(:ok) } - it { expect(assigns(:procedure)).to eq(procedure) } + context "without anything" do + before { get :show, params: { procedure_id: procedure.id } } + + it { expect(response).to have_http_status(:ok) } + it { expect(assigns(:procedure)).to eq(procedure) } + end context 'with a new draft dossier' do let!(:draft_dossier) { create(:dossier, procedure: procedure, state: 'draft') } + before do + get :show, params: { procedure_id: procedure.id } + end + it { expect(assigns(:a_suivre_dossiers)).to be_empty } it { expect(assigns(:followed_dossiers)).to be_empty } it { expect(assigns(:termines_dossiers)).to be_empty } @@ -191,6 +198,10 @@ describe NewGestionnaire::ProceduresController, type: :controller do context 'with a new dossier without follower' do let!(:new_unfollow_dossier) { create(:dossier, procedure: procedure, state: 'received') } + before do + get :show, params: { procedure_id: procedure.id } + end + it { expect(assigns(:a_suivre_dossiers)).to match([new_unfollow_dossier]) } it { expect(assigns(:followed_dossiers)).to be_empty } it { expect(assigns(:termines_dossiers)).to be_empty } @@ -201,7 +212,10 @@ describe NewGestionnaire::ProceduresController, type: :controller do context 'with a new dossier with a follower' do let!(:new_followed_dossier) { create(:dossier, procedure: procedure, state: 'received') } - before { gestionnaire.followed_dossiers << new_followed_dossier } + before do + gestionnaire.followed_dossiers << new_followed_dossier + get :show, params: { procedure_id: procedure.id } + end it { expect(assigns(:a_suivre_dossiers)).to be_empty } it { expect(assigns(:followed_dossiers)).to match([new_followed_dossier]) } @@ -213,6 +227,10 @@ describe NewGestionnaire::ProceduresController, type: :controller do context 'with a termine dossier with a follower' do let!(:termine_dossier) { create(:dossier, procedure: procedure, state: 'closed') } + before do + get :show, params: { procedure_id: procedure.id } + end + it { expect(assigns(:a_suivre_dossiers)).to be_empty } it { expect(assigns(:followed_dossiers)).to be_empty } it { expect(assigns(:termines_dossiers)).to match([termine_dossier]) } @@ -223,6 +241,10 @@ describe NewGestionnaire::ProceduresController, type: :controller do context 'with an archived dossier' do let!(:archived_dossier) { create(:dossier, procedure: procedure, state: 'received', archived: true) } + before do + get :show, params: { procedure_id: procedure.id } + end + it { expect(assigns(:a_suivre_dossiers)).to be_empty } it { expect(assigns(:followed_dossiers)).to be_empty } it { expect(assigns(:termines_dossiers)).to be_empty } @@ -235,6 +257,7 @@ describe NewGestionnaire::ProceduresController, type: :controller do let!(:new_followed_dossier) { Timecop.freeze(2.day.ago){ create(:dossier, procedure: procedure, state: 'received') } } let!(:termine_dossier) { Timecop.freeze(3.day.ago){ create(:dossier, procedure: procedure, state: 'closed') } } let!(:archived_dossier) { Timecop.freeze(4.day.ago){ create(:dossier, procedure: procedure, state: 'received', archived: true) } } + before do gestionnaire.followed_dossiers << new_followed_dossier get :show, params: { procedure_id: procedure.id, statut: statut }