From 7e4f473027c47718afb86fe729803cea0bc5f36f Mon Sep 17 00:00:00 2001 From: Mathieu Magnin Date: Mon, 6 Mar 2017 15:03:51 +0100 Subject: [PATCH] When procedure is archived user can update dossier if not in brouillon --- .../users/description_controller.rb | 4 ++-- app/models/dossier.rb | 4 ++++ app/views/users/description/_show.html.haml | 11 +++++----- .../description_controller_shared_example.rb | 21 +++++++++++++++---- .../users/description_controller_spec.rb | 3 ++- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index fb4bedda7..fa2d26f8d 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -17,7 +17,7 @@ class Users::DescriptionController < UsersController acc end - if @procedure.archived? + unless @dossier.can_be_initiated? flash[:alert] = t('errors.messages.procedure_archived') end @@ -30,7 +30,7 @@ class Users::DescriptionController < UsersController @dossier = current_user_dossier @procedure = @dossier.procedure - return head :forbidden if @procedure.archived? + return head :forbidden unless @dossier.can_be_initiated? @champs = @dossier.ordered_champs diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 5877e87fd..8031da67b 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -304,4 +304,8 @@ class Dossier < ActiveRecord::Base def invite_by_user? email (invites_user.pluck :email).include? email end + + def can_be_initiated? + !(procedure.archived && draft?) + end end diff --git a/app/views/users/description/_show.html.haml b/app/views/users/description/_show.html.haml index 13bb982e9..14b17b160 100644 --- a/app/views/users/description/_show.html.haml +++ b/app/views/users/description/_show.html.haml @@ -46,12 +46,11 @@ %div{style: 'text-align:right'} %h6 Tous les champs portant un * sont obligatoires. - - if @procedure.archived? + - if !@dossier.can_be_initiated? .alert.alert-danger = t('errors.messages.procedure_archived') + - elsif !@dossier.draft? + = render partial: '/layouts/modifications_terminees' - else - - if !@dossier.draft? - = render partial: '/layouts/modifications_terminees' - - else - = submit_tag 'Soumettre mon dossier', id: 'suivant', name: 'submit[nouveaux]', class: 'btn btn btn-success', style: 'float:right', disabled: @procedure.archived?, data: { disable_with: 'Soumettre votre dossier', submit: true} - = submit_tag 'Enregistrer un brouillon', id: 'brouillon', name: 'submit[brouillon]', class: 'btn btn-xs btn-default', style: 'float:right; margin-right: 10px; margin-top: 6px', disabled: @procedure.archived?, data: {disable_with: 'Enregistrer un brouillon', submit: true} + = submit_tag 'Soumettre mon dossier', id: 'suivant', name: 'submit[nouveaux]', class: 'btn btn btn-success', style: 'float:right', disabled: @procedure.archived?, data: { disable_with: 'Soumettre votre dossier', submit: true} + = submit_tag 'Enregistrer un brouillon', id: 'brouillon', name: 'submit[brouillon]', class: 'btn btn-xs btn-default', style: 'float:right; margin-right: 10px; margin-top: 6px', disabled: @procedure.archived?, data: {disable_with: 'Enregistrer un brouillon', submit: true} diff --git a/spec/controllers/users/description_controller_shared_example.rb b/spec/controllers/users/description_controller_shared_example.rb index 89ebfa094..f74eb29b1 100644 --- a/spec/controllers/users/description_controller_shared_example.rb +++ b/spec/controllers/users/description_controller_shared_example.rb @@ -24,12 +24,19 @@ shared_examples 'description_controller_spec' do expect(response).to have_http_status(:success) end - context 'but procedure is archived' do - let(:archived) { true } + context 'procedure is archived' do render_views + let(:archived) { true } it { expect(response).to have_http_status(:success) } - it { expect(response.body).to have_content(I18n.t('errors.messages.procedure_archived')) } + it { expect(response.body).to_not have_content(I18n.t('errors.messages.procedure_archived')) } + + context 'dossier is a draft' do + let(:state) { 'draft' } + + it { expect(response).to have_http_status(:success) } + it { expect(response.body).to have_content(I18n.t('errors.messages.procedure_archived')) } + end end end @@ -297,7 +304,13 @@ shared_examples 'description_controller_spec' do post :update, params: { dossier_id: dossier.id } end - it { expect(response.status).to eq(403) } + it { expect(response.status).to eq(302) } + + context 'Le dossier est en brouillon' do + let(:state) { 'draft' } + + it { expect(response.status).to eq(403) } + end end end diff --git a/spec/controllers/users/description_controller_spec.rb b/spec/controllers/users/description_controller_spec.rb index 9511f3d4a..0cd1c0414 100644 --- a/spec/controllers/users/description_controller_spec.rb +++ b/spec/controllers/users/description_controller_spec.rb @@ -6,9 +6,10 @@ describe Users::DescriptionController, type: :controller, vcr: {cassette_name: ' let(:owner_user) { create(:user) } let(:invite_by_user) { create :user, email: 'invite@plop.com' } let(:archived) { false } + let(:state) { 'initiated' } let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, :with_datetime, cerfa_flag: true, archived: archived) } - let(:dossier) { create(:dossier, procedure: procedure, user: owner_user, state: 'initiated') } + let(:dossier) { create(:dossier, procedure: procedure, user: owner_user, state: state) } let(:dossier_id) { dossier.id } let(:bad_dossier_id) { Dossier.count + 10000 }