From 87e731bc757d04018888da115bd8a45f8de2e069 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 8 Oct 2018 17:55:20 +0200 Subject: [PATCH 1/2] Dossier: move state translation to model dossier --- app/models/dossier.rb | 17 +++++++++++++ app/serializers/dossier_serializer.rb | 15 +---------- spec/models/dossier_spec.rb | 28 +++++++++++++++++++++ spec/serializers/dossier_serializer_spec.rb | 19 -------------- 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/app/models/dossier.rb b/app/models/dossier.rb index f4c28c44d75..8da633957ea 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -277,6 +277,23 @@ def delete_and_keep_track DossierMailer.notify_deletion_to_user(deleted_dossier, user.email).deliver_later end + def old_state_value + case state + when Dossier.states.fetch(:en_construction) + 'initiated' + when Dossier.states.fetch(:en_instruction) + 'received' + when Dossier.states.fetch(:accepte) + 'closed' + when Dossier.states.fetch(:refuse) + 'refused' + when Dossier.states.fetch(:sans_suite) + 'without_continuation' + else + state + end + end + private def update_state_dates diff --git a/app/serializers/dossier_serializer.rb b/app/serializers/dossier_serializer.rb index 05575a90b55..252c362e63c 100644 --- a/app/serializers/dossier_serializer.rb +++ b/app/serializers/dossier_serializer.rb @@ -43,20 +43,7 @@ def entreprise end def state - case object.state - when Dossier.states.fetch(:en_construction) - 'initiated' - when Dossier.states.fetch(:en_instruction) - 'received' - when Dossier.states.fetch(:accepte) - 'closed' - when Dossier.states.fetch(:refuse) - 'refused' - when Dossier.states.fetch(:sans_suite) - 'without_continuation' - else - object.state - end + object.old_state_value end def simplified_state diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 351a890d877..5554f4797d8 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -1004,4 +1004,32 @@ it { expect(long_expired_dossier).to be_retention_expired } end end + + describe 'old_state_value' do + subject { dossier.old_state_value } + + context 'when the dossier is en instruction' do + let(:dossier) { create(:dossier, :en_instruction) } + + it { is_expected.to eq('received') } + end + + context 'when the dossier is accepte' do + let(:dossier) { create(:dossier, state: Dossier.states.fetch(:accepte)) } + + it { is_expected.to eq('closed') } + end + + context 'when the dossier is refuse' do + let(:dossier) { create(:dossier, state: Dossier.states.fetch(:refuse)) } + + it { is_expected.to eq('refused') } + end + + context 'when the dossier is sans_suite' do + let(:dossier) { create(:dossier, state: Dossier.states.fetch(:sans_suite)) } + + it { is_expected.to eq('without_continuation') } + end + end end diff --git a/spec/serializers/dossier_serializer_spec.rb b/spec/serializers/dossier_serializer_spec.rb index 1de1c5e3905..4cf4b398dd4 100644 --- a/spec/serializers/dossier_serializer_spec.rb +++ b/spec/serializers/dossier_serializer_spec.rb @@ -13,25 +13,6 @@ let(:dossier) { create(:dossier, :en_instruction) } it { is_expected.to include(received_at: dossier.en_instruction_at) } - it { is_expected.to include(state: 'received') } - end - - context 'when the dossier is accepte' do - let(:dossier) { create(:dossier, state: Dossier.states.fetch(:accepte)) } - - it { is_expected.to include(state: 'closed') } - end - - context 'when the dossier is refuse' do - let(:dossier) { create(:dossier, state: Dossier.states.fetch(:refuse)) } - - it { is_expected.to include(state: 'refused') } - end - - context 'when the dossier is sans_suite' do - let(:dossier) { create(:dossier, state: Dossier.states.fetch(:sans_suite)) } - - it { is_expected.to include(state: 'without_continuation') } end end end From 2288303de9189ed356acb94dc97b57f6ed408169 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Mon, 8 Oct 2018 17:56:18 +0200 Subject: [PATCH 2/2] [fix #2777] DossiersSerializer: add state --- app/serializers/dossiers_serializer.rb | 7 ++++++- spec/controllers/api/v1/dossiers_controller_spec.rb | 3 ++- spec/serializers/dossiers_serializer_spec.rb | 1 + 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/serializers/dossiers_serializer.rb b/app/serializers/dossiers_serializer.rb index 98fa3cfb01a..a1c225229a5 100644 --- a/app/serializers/dossiers_serializer.rb +++ b/app/serializers/dossiers_serializer.rb @@ -1,9 +1,14 @@ class DossiersSerializer < ActiveModel::Serializer attributes :id, :updated_at, - :initiated_at + :initiated_at, + :state def initiated_at object.en_construction_at end + + def state + object.old_state_value + end end diff --git a/spec/controllers/api/v1/dossiers_controller_spec.rb b/spec/controllers/api/v1/dossiers_controller_spec.rb index de9727fa9d9..de3ef4f3eda 100644 --- a/spec/controllers/api/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/v1/dossiers_controller_spec.rb @@ -77,7 +77,8 @@ it { expect(subject[:id]).to eq(dossier.id) } it { expect(subject[:updated_at]).to eq("2008-09-01T10:05:00.000Z") } it { expect(subject[:initiated_at]).to eq("2008-09-01T10:06:00.000Z") } - it { expect(subject.keys.size).to eq(3) } + it { expect(subject[:state]).to eq("initiated") } + it { expect(subject.keys.size).to eq(4) } end end diff --git a/spec/serializers/dossiers_serializer_spec.rb b/spec/serializers/dossiers_serializer_spec.rb index f2259574e9f..1fd01f41e6f 100644 --- a/spec/serializers/dossiers_serializer_spec.rb +++ b/spec/serializers/dossiers_serializer_spec.rb @@ -6,6 +6,7 @@ let(:dossier) { create(:dossier, :en_construction) } it { is_expected.to include(initiated_at: dossier.en_construction_at) } + it { is_expected.to include(state: 'initiated') } end end end