diff --git a/app/controllers/admin/procedures_controller.rb b/app/controllers/admin/procedures_controller.rb index a8865aaf4..0ace5c1b5 100644 --- a/app/controllers/admin/procedures_controller.rb +++ b/app/controllers/admin/procedures_controller.rb @@ -86,7 +86,7 @@ class Admin::ProceduresController < AdminController private def create_procedure_params - params.require(:procedure).permit(:libelle, :description, :organisation, :direction, :lien_demarche, :euro_flag, :logo, module_api_carto_attributes: [:id, :use_api_carto, :quartiers_prioritaires, :cadastre]).merge(administrateur_id: current_administrateur.id) + params.require(:procedure).permit(:libelle, :description, :organisation, :direction, :lien_demarche, :euro_flag, :logo, :cerfa_flag, module_api_carto_attributes: [:id, :use_api_carto, :quartiers_prioritaires, :cadastre]).merge(administrateur_id: current_administrateur.id) end def create_module_api_carto_params diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index 584d5767d..78516eaab 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -23,6 +23,8 @@ class Users::DescriptionController < UsersController def create @dossier = current_user_dossier + @procedure = @dossier.procedure + unless @dossier.update_attributes(create_params) @dossier = @dossier.decorate @procedure = @dossier.procedure @@ -30,10 +32,12 @@ class Users::DescriptionController < UsersController flash.now.alert = @dossier.errors.full_messages.join('
').html_safe return render 'show' end - unless params[:cerfa_pdf].nil? - cerfa = @dossier.cerfa - cerfa.content = params[:cerfa_pdf] - cerfa.save + if @procedure.cerfa_flag? + unless params[:cerfa_pdf].nil? + cerfa = @dossier.cerfa + cerfa.content = params[:cerfa_pdf] + cerfa.save + end end unless params[:champs].nil? diff --git a/app/models/dossier.rb b/app/models/dossier.rb index ae017800b..22fd45a97 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -25,8 +25,8 @@ class Dossier < ActiveRecord::Base delegate :types_de_piece_justificative, to: :procedure delegate :types_de_champ, to: :procedure - before_create :build_default_cerfa + before_save :build_default_cerfa, if: Proc.new { procedure_id_changed? } after_save :build_default_pieces_justificatives, if: Proc.new { procedure_id_changed? } after_save :build_default_champs, if: Proc.new { procedure_id_changed? } @@ -188,10 +188,15 @@ class Dossier < ActiveRecord::Base return composed_scope, dossier end + def cerfa_available? + procedure.cerfa_flag? && !cerfa.empty? + end + private def build_default_cerfa - build_cerfa + build_cerfa if procedure.cerfa_flag? true end + end diff --git a/app/views/admin/procedures/_informations.html.haml b/app/views/admin/procedures/_informations.html.haml index 6e6ad9e76..6e649ac75 100644 --- a/app/views/admin/procedures/_informations.html.haml +++ b/app/views/admin/procedures/_informations.html.haml @@ -21,22 +21,28 @@ =f.check_box :euro_flag Afficher le drapeau européen - %br -%h4 Cartographie +.row + .col-md-6.col-lg-6 + %h4 Cartographie -= f.fields_for :module_api_carto, @procedure.module_api_carto do |ff| - %label - =ff.check_box :use_api_carto, {id: :procedure_module_api_carto_use_api_carto} - Utilisation de la cartographie + = f.fields_for :module_api_carto, @procedure.module_api_carto do |ff| + %label + =ff.check_box :use_api_carto, {id: :procedure_module_api_carto_use_api_carto} + Utilisation de la cartographie - %ul#modules_api_carto - %li - %label - = ff.check_box :quartiers_prioritaires - Quartiers Prioritaires - %li - %label - = ff.check_box :cadastre - Cadastre + %ul#modules_api_carto + %li + %label + = ff.check_box :quartiers_prioritaires + Quartiers Prioritaires + %li + %label + = ff.check_box :cadastre + Cadastre + .col-md-6.col-lg-6 + %h4 Formulaire / CERFA + %label + =f.check_box :cerfa_flag + Activer l'envoi de formulaire / CERFA diff --git a/app/views/dossiers/_pieces_justificatives.html.haml b/app/views/dossiers/_pieces_justificatives.html.haml index 71eaff73f..aee309545 100644 --- a/app/views/dossiers/_pieces_justificatives.html.haml +++ b/app/views/dossiers/_pieces_justificatives.html.haml @@ -8,7 +8,7 @@ %th{class:'col-lg-6'} ='Formulaire' %td.col-lg-6.col-md-6 - - if !@facade.dossier.cerfa.empty? + - if @facade.dossier.cerfa_available? - if user_signed_in? = 'Pièce fournie' - elsif gestionnaire_signed_in? diff --git a/app/views/users/description/show.html.haml b/app/views/users/description/show.html.haml index c9b1382d9..471364b03 100644 --- a/app/views/users/description/show.html.haml +++ b/app/views/users/description/show.html.haml @@ -49,17 +49,18 @@ %br //TODO a refactorer %table{class:'table', style:'width:55%; margin-left:5%'} - %tr - %th{class:'col-lg-6'} - ='Formulaire (.pdf / .doc)' + - if @procedure.cerfa_flag + %tr + %th{class:'col-lg-6'} + ='Formulaire (.pdf / .doc)' - %td{class:'col-lg-5'} - -if !@dossier.cerfa.empty? - %span.btn.btn-sm.btn-file.btn-success - Modifier + %td{class:'col-lg-5'} + -if @dossier.cerfa_available? + %span.btn.btn-sm.btn-file.btn-success + Modifier + %input{type: 'file', name:'cerfa_pdf', id:'cerfa_pdf', accept: ".pdf"} + -else %input{type: 'file', name:'cerfa_pdf', id:'cerfa_pdf', accept: ".pdf"} - -else - %input{type: 'file', name:'cerfa_pdf', id:'cerfa_pdf', accept: ".pdf"} - @dossier.pieces_justificatives.each do |piece_justificative| %tr @@ -74,7 +75,7 @@ -else %span.btn.btn-sm.btn-file.btn-success Modifier - = file_field_tag "piece_justificative_#{piece_justificative.type}", accept: '.pdf' + = file_f ield_tag "piece_justificative_#{piece_justificative.type}", accept: '.pdf' %div{style: 'text-align:right'} %h6 Tous les champs portant un * sont obligatoires. diff --git a/db/migrate/20160127162841_add_cerfa_flag_to_procedure.rb b/db/migrate/20160127162841_add_cerfa_flag_to_procedure.rb new file mode 100644 index 000000000..5589bbbf1 --- /dev/null +++ b/db/migrate/20160127162841_add_cerfa_flag_to_procedure.rb @@ -0,0 +1,5 @@ +class AddCerfaFlagToProcedure < ActiveRecord::Migration + def change + add_column :procedures, :cerfa_flag, :boolean, :default => false + end +end diff --git a/db/schema.rb b/db/schema.rb index afb5bbfb1..352407c67 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,8 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. - -ActiveRecord::Schema.define(version: 20160121110603) do +ActiveRecord::Schema.define(version: 20160127162841) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -190,6 +189,7 @@ ActiveRecord::Schema.define(version: 20160121110603) do t.boolean "archived", default: false t.boolean "euro_flag", default: false t.string "logo" + t.boolean "cerfa_flag", default: false end create_table "quartier_prioritaires", force: :cascade do |t| diff --git a/spec/controllers/users/description_controller_spec.rb b/spec/controllers/users/description_controller_spec.rb index 89403bdab..87dd12320 100644 --- a/spec/controllers/users/description_controller_spec.rb +++ b/spec/controllers/users/description_controller_spec.rb @@ -2,7 +2,8 @@ require 'spec_helper' describe Users::DescriptionController, type: :controller do let(:user) { create(:user) } - let(:dossier) { create(:dossier, :with_procedure, user: user) } + let(:procedure) { create(:procedure, cerfa_flag: true) } + let(:dossier) { create(:dossier, procedure: procedure, user: user) } let(:dossier_id) { dossier.id } let(:bad_dossier_id) { Dossier.count + 10 } @@ -117,38 +118,57 @@ describe Users::DescriptionController, type: :controller do it { is_expected.to render_template(:show) } it { expect(flash[:alert]).to be_present } end - end + end - context 'Sauvegarde du CERFA PDF' do - before do - post :create, dossier_id: dossier_id, - nom_projet: nom_projet, - description: description, - cerfa_pdf: cerfa_pdf - dossier.reload - end - - context 'un CERFA PDF est envoyé' do - subject { dossier.cerfa } - it 'content' do - expect(subject['content']).to eq(name_piece_justificative) + context 'Quand la procédure accepte les CERFA' do + context 'Sauvegarde du CERFA PDF' do + before do + post :create, dossier_id: dossier_id, + nom_projet: nom_projet, + description: description, + cerfa_pdf: cerfa_pdf + dossier.reload end - it 'dossier_id' do - expect(subject.dossier_id).to eq(dossier_id) + context 'un CERFA PDF est envoyé' do + subject { dossier.cerfa } + it 'content' do + expect(subject['content']).to eq(name_piece_justificative) + end + + it 'dossier_id' do + expect(subject.dossier_id).to eq(dossier_id) + end + end + + context 'les anciens CERFA PDF sont écrasées à chaque fois' do + it 'il n\'y a qu\'un CERFA PDF par dossier' do + post :create, dossier_id: dossier_id, nom_projet: nom_projet, description: description, cerfa_pdf: cerfa_pdf + cerfa = PieceJustificative.where(type_de_piece_justificative_id: '0', dossier_id: dossier_id) + expect(cerfa.many?).to eq(false) + end + end + + context 'pas de CERFA PDF' do + # TODO à écrire end end + end - context 'les anciens CERFA PDF sont écrasées à chaque fois' do - it 'il n\'y a qu\'un CERFA PDF par dossier' do - post :create, dossier_id: dossier_id, nom_projet: nom_projet, description: description, cerfa_pdf: cerfa_pdf - cerfa = PieceJustificative.where(type_de_piece_justificative_id: '0', dossier_id: dossier_id) - expect(cerfa.many?).to eq(false) + context 'Quand la procédure n\'accepte pas les CERFA' do + context 'Sauvegarde du CERFA PDF' do + let!(:procedure) { create(:procedure) } + before do + post :create, dossier_id: dossier_id, + nom_projet: nom_projet, + description: description, + cerfa_pdf: cerfa_pdf + dossier.reload end - end - context 'pas de CERFA PDF' do - # TODO à écrire + context 'un CERFA PDF est envoyé' do + it { expect(dossier.cerfa_available?).to be_falsey } + end end end diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index 65463bd72..e967a1296 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -4,7 +4,13 @@ FactoryGirl.define do description "Ma super description" state 'draft' association :user, factory:[:user] - + unless procedure + before(:create) do |dossier, _evaluator| + procedure = create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ) + dossier.procedure = procedure + end + end + trait :with_entreprise do after(:build) do |dossier, _evaluator| etablissement = create(:etablissement) @@ -15,7 +21,7 @@ FactoryGirl.define do end trait :with_procedure do - after(:build) do |dossier, _evaluator| + before(:create) do |dossier, _evaluator| procedure = create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ) dossier.procedure = procedure end diff --git a/spec/features/description_page/upload_piece_justificative_spec.rb b/spec/features/description_page/upload_piece_justificative_spec.rb index 84db09193..c688ff15d 100644 --- a/spec/features/description_page/upload_piece_justificative_spec.rb +++ b/spec/features/description_page/upload_piece_justificative_spec.rb @@ -1,7 +1,8 @@ require 'spec_helper' feature 'user is on description page' do - let(:dossier) { create(:dossier, :with_entreprise, :with_procedure) } + let!(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, cerfa_flag: true) } + let!(:dossier) { create(:dossier, :with_entreprise, procedure: procedure) } before do visit users_dossier_description_path dossier @@ -42,7 +43,7 @@ feature 'user is on description page' do context 'when he adds a piece_justificative and submit form' do before do file_input_id = 'piece_justificative_' + dossier.pieces_justificatives.first.type.to_s - attach_file(file_input_id, File.path('spec/support/files/dossierPDF.pdf')) + attach_file(file_inp ut_id, File.path('spec/support/files/dossierPDF.pdf')) click_on('Soumettre mon dossier') dossier.reload end diff --git a/spec/features/users/list_dossiers_spec.rb b/spec/features/users/list_dossiers_spec.rb index 44b277fc9..d9b61897e 100644 --- a/spec/features/users/list_dossiers_spec.rb +++ b/spec/features/users/list_dossiers_spec.rb @@ -4,8 +4,8 @@ feature 'user access to the list of his dossier' do let(:user) { create(:user) } let!(:last_updated_dossier) { create(:dossier, :with_procedure, user: user, state: 'replied')} - let!(:dossier1) { create(:dossier, :with_procedure, user: user, nom_projet: 'mon permier dossier', state: 'replied') } - let!(:dossier2) { create(:dossier, nom_projet: 'mon deuxième dossier') } + let!(:dossier1) { create(:dossier, user: user, nom_projet: 'mon permier dossier', state: 'replied') } + let!(:dossier2) { create(:dossier, nom_projet: 'mon deuxième dossier') } before do last_updated_dossier.update_attributes(nom_projet: 'salut la compagnie') diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 6025ceb3c..d73afeb7f 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -63,13 +63,27 @@ describe Dossier do end describe 'creation' do - it 'create default cerfa' do - expect { described_class.create(user: user) }.to change { Cerfa.count }.by(1) + describe 'Procedure accepts cerfa upload' do + let(:procedure) { create(:procedure, cerfa_flag: true) } + let(:dossier) { create(:dossier, :with_entreprise, procedure: procedure, user: user) } + it 'create default cerfa' do + expect { subject.to change(Cerfa.count).by(1) } + expect { subject.cerfa_available.to be_truthy } + end + + it 'link cerfa to dossier' do + expect { subject.cerfa.to eq(Cerfa.last) } + end end - it 'link cerfa to dossier' do - dossier = described_class.create - expect(dossier.cerfa).to eq(Cerfa.last) + describe 'Procedure does not accept cerfa upload' do + let(:procedure) { create(:procedure, cerfa_flag: false) } + let(:dossier) { create(:dossier, :with_entreprise, :with_procedure, user: user) } + it 'default cerfa is not created' do + expect { subject.to change(Cerfa.count).by(0) } + expect { subject.cerfa.to eq(nil) } + expect { subject.cerfa_available.to be_falsey } + end end end @@ -104,20 +118,21 @@ describe Dossier do end describe '#save' do - subject { create(:dossier, procedure_id: nil, user: user) } + subject { build(:dossier, procedure: procedure, user: user) } let!(:procedure) { create(:procedure) } context 'when is linked to a procedure' do it 'creates default pieces justificatives' do expect(subject).to receive(:build_default_pieces_justificatives) - subject.update_attributes(procedure_id: procedure.id) + subject.save end it 'creates default champs' do expect(subject).to receive(:build_default_champs) - subject.update_attributes(procedure_id: procedure.id) + subject.save end end context 'when is not linked to a procedure' do + subject { create(:dossier, procedure: procedure, user: user) } it 'does not create default pieces justificatives' do expect(subject).not_to receive(:build_default_pieces_justificatives) subject.update_attributes(description: 'plop') @@ -504,4 +519,26 @@ describe Dossier do end end end + + describe '#cerfa_available?' do + let(:procedure) { create(:procedure, cerfa_flag: cerfa_flag) } + let(:dossier) { create(:dossier, procedure: procedure)} + context 'Procedure accepts CERFA' do + let(:cerfa_flag) { true } + context 'when cerfa is not uploaded' do + it { expect(dossier.cerfa_available?).to be_falsey } + end + context 'when cerfa is uploaded' do + let(:dossier_with_cerfa) { create(:dossier, procedure: procedure) } + before do + allow_any_instance_of(Cerfa).to receive(:empty?).and_return(false) + end + it { expect(dossier_with_cerfa.cerfa_available?).to be_truthy } + end + end + context 'Procedure does not accept CERFA' do + let(:cerfa_flag) { false } + it { expect(dossier.cerfa_available?).to be_falsey } + end + end end diff --git a/spec/models/procedure_spec.rb b/spec/models/procedure_spec.rb index a42ba946a..066034086 100644 --- a/spec/models/procedure_spec.rb +++ b/spec/models/procedure_spec.rb @@ -17,6 +17,7 @@ describe Procedure do it { is_expected.to have_db_column(:test) } it { is_expected.to have_db_column(:euro_flag) } it { is_expected.to have_db_column(:logo) } + it { is_expected.to have_db_column(:cerfa_flag) } end describe 'validation' do diff --git a/spec/views/users/description/show.html.haml_spec.rb b/spec/views/users/description/show.html.haml_spec.rb index 88b9f177a..33ae7a608 100644 --- a/spec/views/users/description/show.html.haml_spec.rb +++ b/spec/views/users/description/show.html.haml_spec.rb @@ -2,7 +2,9 @@ require 'spec_helper' describe 'users/description/show.html.haml', type: :view do let(:user) { create(:user) } - let(:dossier) { create(:dossier, :with_procedure, user: user) } + let(:cerfa_flag) { true } + let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, cerfa_flag: cerfa_flag) } + let(:dossier) { create(:dossier, procedure: procedure, user: user) } let(:dossier_id) { dossier.id } before do @@ -125,4 +127,15 @@ describe 'users/description/show.html.haml', type: :view do end end end + + context 'Envoi des CERFA désactivé' do + let!(:cerfa_flag) { false } + + before do + render + end + + it { expect(rendered).to_not have_css("#cerfa_flag") } + it { expect(rendered).to_not have_selector('input[type=file][name=cerfa_pdf][id=cerfa_pdf]') } + end end diff --git a/spec/views/users/dossiers/new_html.haml_spec.rb b/spec/views/users/dossiers/new_html.haml_spec.rb index 2d0193f82..74919acc9 100644 --- a/spec/views/users/dossiers/new_html.haml_spec.rb +++ b/spec/views/users/dossiers/new_html.haml_spec.rb @@ -3,8 +3,9 @@ require 'spec_helper' describe 'users/dossiers/new.html.haml', type: :view do let(:user) { create(:user) } let(:euro_flag) { false } + let(:cerfa_flag) { false } let(:logo) { '' } - let(:procedure) { create(:procedure, euro_flag: euro_flag, logo: logo) } + let(:procedure) { create(:procedure, euro_flag: euro_flag, cerfa_flag: cerfa_flag, logo: logo) } let!(:dossier) { create(:dossier, procedure: procedure, user: user).decorate } before do