From 26431ac52862d788e29747d8fcf18a6b4f2309a3 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Mon, 14 Mar 2016 17:30:22 +0100 Subject: [PATCH 1/3] Add column created_at on cerfa and piece_justificative --- app/serializers/cerfa_serializer.rb | 4 +++- app/serializers/piece_justificative_serializer.rb | 3 ++- .../20160314160801_add_created_at_to_cerfa.rb | 5 +++++ ...161959_add_created_at_to_piece_justificative.rb | 5 +++++ db/schema.rb | 14 ++++++++------ .../controllers/api/v1/dossiers_controller_spec.rb | 2 ++ spec/models/cerfa_spec.rb | 1 + spec/models/piece_justificative_spec.rb | 1 + 8 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20160314160801_add_created_at_to_cerfa.rb create mode 100644 db/migrate/20160314161959_add_created_at_to_piece_justificative.rb diff --git a/app/serializers/cerfa_serializer.rb b/app/serializers/cerfa_serializer.rb index 836161b7e..2b99ded67 100644 --- a/app/serializers/cerfa_serializer.rb +++ b/app/serializers/cerfa_serializer.rb @@ -1,5 +1,7 @@ class CerfaSerializer < ActiveModel::Serializer - attributes :content_url => :url + attributes :created_at, + :content_url => :url + end \ No newline at end of file diff --git a/app/serializers/piece_justificative_serializer.rb b/app/serializers/piece_justificative_serializer.rb index 0fe9d0e10..2950e69a6 100644 --- a/app/serializers/piece_justificative_serializer.rb +++ b/app/serializers/piece_justificative_serializer.rb @@ -1,5 +1,6 @@ class PieceJustificativeSerializer < ActiveModel::Serializer - attributes :content_url => :url + attributes :created_at, + :content_url => :url has_one :type_de_piece_justificative end \ No newline at end of file diff --git a/db/migrate/20160314160801_add_created_at_to_cerfa.rb b/db/migrate/20160314160801_add_created_at_to_cerfa.rb new file mode 100644 index 000000000..a9e3a25d7 --- /dev/null +++ b/db/migrate/20160314160801_add_created_at_to_cerfa.rb @@ -0,0 +1,5 @@ +class AddCreatedAtToCerfa < ActiveRecord::Migration + def change + add_column :cerfas, :created_at, :datetime + end +end diff --git a/db/migrate/20160314161959_add_created_at_to_piece_justificative.rb b/db/migrate/20160314161959_add_created_at_to_piece_justificative.rb new file mode 100644 index 000000000..61cec7fb9 --- /dev/null +++ b/db/migrate/20160314161959_add_created_at_to_piece_justificative.rb @@ -0,0 +1,5 @@ +class AddCreatedAtToPieceJustificative < ActiveRecord::Migration + def change + add_column :pieces_justificatives, :created_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 03b0171eb..2833d09e2 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160314102523) do +ActiveRecord::Schema.define(version: 20160314161959) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -68,8 +68,9 @@ ActiveRecord::Schema.define(version: 20160314102523) do end create_table "cerfas", force: :cascade do |t| - t.string "content" - t.integer "dossier_id" + t.string "content" + t.integer "dossier_id" + t.datetime "created_at" end add_index "cerfas", ["dossier_id"], name: "index_cerfas_on_dossier_id", using: :btree @@ -195,9 +196,10 @@ ActiveRecord::Schema.define(version: 20160314102523) do add_index "module_api_cartos", ["procedure_id"], name: "index_module_api_cartos_on_procedure_id", unique: true, using: :btree create_table "pieces_justificatives", force: :cascade do |t| - t.string "content" - t.integer "dossier_id" - t.integer "type_de_piece_justificative_id" + t.string "content" + t.integer "dossier_id" + t.integer "type_de_piece_justificative_id" + t.datetime "created_at" end add_index "pieces_justificatives", ["type_de_piece_justificative_id"], name: "index_pieces_justificatives_on_type_de_piece_justificative_id", using: :btree diff --git a/spec/controllers/api/v1/dossiers_controller_spec.rb b/spec/controllers/api/v1/dossiers_controller_spec.rb index ea277770f..56fbcd0ea 100644 --- a/spec/controllers/api/v1/dossiers_controller_spec.rb +++ b/spec/controllers/api/v1/dossiers_controller_spec.rb @@ -172,6 +172,7 @@ describe API::V1::DossiersController do subject { super().first } it { expect(subject.keys.include?(:url)).to be_truthy } + it { expect(subject[:created_at]).not_to be_nil } it { expect(subject.keys.include?(:type_de_piece_justificative)).to be_truthy } describe 'type de piece justificative' do @@ -242,6 +243,7 @@ describe API::V1::DossiersController do subject { super()[:cerfa] } + it { expect(subject[:created_at]).not_to be_nil } it { expect(subject[:url]).to match /^http:\/\/.*downloads.*_CERFA\.pdf$/ } end diff --git a/spec/models/cerfa_spec.rb b/spec/models/cerfa_spec.rb index 336a9144f..47d7f432d 100644 --- a/spec/models/cerfa_spec.rb +++ b/spec/models/cerfa_spec.rb @@ -3,6 +3,7 @@ require 'spec_helper' describe Cerfa do describe 'database columns' do it { is_expected.to have_db_column(:content) } + it { is_expected.to have_db_column(:created_at) } end describe 'associations' do diff --git a/spec/models/piece_justificative_spec.rb b/spec/models/piece_justificative_spec.rb index 2c5817a5a..feb2e515d 100644 --- a/spec/models/piece_justificative_spec.rb +++ b/spec/models/piece_justificative_spec.rb @@ -3,6 +3,7 @@ require 'spec_helper' describe PieceJustificative do describe 'database columns' do it { is_expected.to have_db_column(:content) } + it { is_expected.to have_db_column(:created_at) } end describe 'associations' do From 494f80afffd262d817253fafe363f0be3899fc4f Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 15 Mar 2016 11:23:25 +0100 Subject: [PATCH 2/3] Can be check TypeDeChamps with attribut mandatory --- app/controllers/admin/types_de_champ_controller.rb | 2 +- app/views/admin/types_de_champ/_fields.html.haml | 5 +++++ db/migrate/20160315101245_add_mandatory_to_type_de_champs.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20160315101245_add_mandatory_to_type_de_champs.rb diff --git a/app/controllers/admin/types_de_champ_controller.rb b/app/controllers/admin/types_de_champ_controller.rb index 66349aef3..b7af379ec 100644 --- a/app/controllers/admin/types_de_champ_controller.rb +++ b/app/controllers/admin/types_de_champ_controller.rb @@ -19,7 +19,7 @@ class Admin::TypesDeChampController < AdminController end def update_params - params.require(:procedure).permit(types_de_champ_attributes: [:libelle, :description, :order_place, :type_champ, :id]) + params.require(:procedure).permit(types_de_champ_attributes: [:libelle, :description, :order_place, :type_champ, :id, :mandatory]) end def move_up diff --git a/app/views/admin/types_de_champ/_fields.html.haml b/app/views/admin/types_de_champ/_fields.html.haml index 371217f76..42118cdd3 100644 --- a/app/views/admin/types_de_champ/_fields.html.haml +++ b/app/views/admin/types_de_champ/_fields.html.haml @@ -9,6 +9,11 @@ .form-group %h4 Description = ff.text_area :description, class: 'form-control description', placeholder: 'Description' + .form-group + %h4 Obligatoire ? + .center + = ff.check_box :mandatory, placeholder: 'Obligatoire ?' + .form-group = ff.hidden_field :order_place, value: ff.index = ff.hidden_field :id diff --git a/db/migrate/20160315101245_add_mandatory_to_type_de_champs.rb b/db/migrate/20160315101245_add_mandatory_to_type_de_champs.rb new file mode 100644 index 000000000..d08aefcfc --- /dev/null +++ b/db/migrate/20160315101245_add_mandatory_to_type_de_champs.rb @@ -0,0 +1,5 @@ +class AddMandatoryToTypeDeChamps < ActiveRecord::Migration + def change + add_column :types_de_champ, :mandatory, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 2833d09e2..50daf5b02 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160314161959) do +ActiveRecord::Schema.define(version: 20160315101245) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -244,6 +244,7 @@ ActiveRecord::Schema.define(version: 20160314161959) do t.integer "order_place" t.integer "procedure_id" t.text "description" + t.boolean "mandatory", default: false end create_table "types_de_piece_justificative", force: :cascade do |t| From bd61a459fb657c55e0f0fc6f33a1cf2ffe730e70 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 15 Mar 2016 17:17:56 +0100 Subject: [PATCH 3/3] Add verification on user dossier description for champs mandatory --- .../users/description_controller.rb | 13 ++++-- app/models/champ.rb | 7 ++- app/views/users/description/show.html.haml | 6 ++- .../admin/types_de_champ_controller_spec.rb | 10 +++-- .../users/description_controller_spec.rb | 45 +++++++++++-------- spec/factories/procedure.rb | 8 ++++ spec/factories/type_de_champ.rb | 1 + 7 files changed, 63 insertions(+), 27 deletions(-) diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index 212973185..71797d199 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -4,8 +4,7 @@ class Users::DescriptionController < UsersController end def show - @dossier = current_user_dossier - @dossier = @dossier.decorate + @dossier = current_user_dossier.decorate @procedure = @dossier.procedure @champs = @dossier.ordered_champs @@ -24,10 +23,10 @@ class Users::DescriptionController < UsersController def create @dossier = current_user_dossier @procedure = @dossier.procedure - + @champs = @dossier.ordered_champs + unless @dossier.update_attributes(create_params) @dossier = @dossier.decorate - @procedure = @dossier.procedure flash.now.alert = @dossier.errors.full_messages.join('
').html_safe return render 'show' @@ -47,6 +46,12 @@ class Users::DescriptionController < UsersController unless params[:champs].nil? @dossier.champs.each do |champ| champ.value = params[:champs]["'#{champ.id}'"] + + if champ.mandatory? && (champ.value.nil? || champ.value.blank?) + flash.now.alert = "Le champ #{champ.libelle} doit être rempli." + return render 'show' + end + champ.save end end diff --git a/app/models/champ.rb b/app/models/champ.rb index c862c6735..2679d1923 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -1,5 +1,10 @@ class Champ < ActiveRecord::Base belongs_to :dossier belongs_to :type_de_champ - delegate :libelle, :type_champ, :order_place, to: :type_de_champ + + delegate :libelle, :type_champ, :order_place, :mandatory, to: :type_de_champ + + def mandatory? + mandatory + end end diff --git a/app/views/users/description/show.html.haml b/app/views/users/description/show.html.haml index 61b3fc6e6..bf6e5edf0 100644 --- a/app/views/users/description/show.html.haml +++ b/app/views/users/description/show.html.haml @@ -26,11 +26,15 @@ -if champ.type_champ == 'checkbox' %h4{style:'margin-left:15px;'} = champ.libelle + - if champ.mandatory? + = '*' %input{type: 'hidden', name:"champs['#{champ.id}']", id: "champs_#{champ.id}", value: ''} %input{type: 'checkbox', style:'margin-left: 15px;', name:"champs['#{champ.id}']", id: "champs_#{champ.id}", checked: ('checked' if champ.value == 'on')} -else %h4 = champ.libelle + - if champ.mandatory? + = '*' -if champ.type_champ == 'textarea' %textarea.form-control{name:"champs['#{champ.id}']", @@ -62,7 +66,7 @@ - if @procedure.cerfa_flag %tr %th{class:'col-lg-6'} - ='Formulaire (.pdf / .doc / .docx / .xls / .xlsx)' + ='Formulaire' %td{class:'col-lg-5'} -if @dossier.cerfa_available? diff --git a/spec/controllers/admin/types_de_champ_controller_spec.rb b/spec/controllers/admin/types_de_champ_controller_spec.rb index 36c366a73..5227c56ae 100644 --- a/spec/controllers/admin/types_de_champ_controller_spec.rb +++ b/spec/controllers/admin/types_de_champ_controller_spec.rb @@ -37,6 +37,7 @@ describe Admin::TypesDeChampController, type: :controller do let(:description) { 'titi' } let(:order_place) { '' } let(:types_de_champ_id) { '' } + let(:mandatory) { 'on' } let(:procedure_params) do { types_de_champ_attributes: @@ -46,7 +47,8 @@ describe Admin::TypesDeChampController, type: :controller do type_champ: type_champ, description: description, order_place: order_place, - id: types_de_champ_id + id: types_de_champ_id, + mandatory: mandatory } } } @@ -67,7 +69,7 @@ describe Admin::TypesDeChampController, type: :controller do it { expect(subject.libelle).to eq('mon libelle') } it { expect(subject.type_champ).to eq('text') } it { expect(subject.description).to eq('titi') } - # it { expect(subject.order_place).to eq('0') } + it { expect(subject.mandatory).to be_truthy } end context 'when type_de_champ already exist' do @@ -78,6 +80,7 @@ describe Admin::TypesDeChampController, type: :controller do let(:type_champ) { 'text' } let(:description) { 'citrouille' } let(:order_place) { '0' } + let(:mandatory) { 'on' } before do request procedure.reload @@ -86,7 +89,8 @@ describe Admin::TypesDeChampController, type: :controller do it { expect(subject.libelle).to eq('toto') } it { expect(subject.type_champ).to eq('text') } it { expect(subject.description).to eq('citrouille') } - # it { expect(subject.order_place).to eq(0) } + it { expect(subject.order_place).to eq(0) } + it { expect(subject.order_place).to be_truthy } end end context 'when procedure is not found' do diff --git a/spec/controllers/users/description_controller_spec.rb b/spec/controllers/users/description_controller_spec.rb index 273469f12..ddfa4f341 100644 --- a/spec/controllers/users/description_controller_spec.rb +++ b/spec/controllers/users/description_controller_spec.rb @@ -4,7 +4,7 @@ describe Users::DescriptionController, type: :controller do let(:user) { create(:user) } let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ, cerfa_flag: true) } - let!(:dossier) { create(:dossier, procedure: procedure, user: user) } + let(:dossier) { create(:dossier, procedure: procedure, user: user) } let(:dossier_id) { dossier.id } let(:bad_dossier_id) { Dossier.count + 10000 } @@ -103,9 +103,9 @@ describe Users::DescriptionController, type: :controller do context 'Attribut(s) manquant(s)' do subject { post :create, - dossier_id: dossier_id, - nom_projet: nom_projet, - description: description + dossier_id: dossier_id, + nom_projet: nom_projet, + description: description } before { subject } @@ -126,9 +126,9 @@ describe Users::DescriptionController, type: :controller 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 + nom_projet: nom_projet, + description: description, + cerfa_pdf: cerfa_pdf dossier.reload end @@ -164,11 +164,11 @@ describe Users::DescriptionController, type: :controller do context 'Quand la procédure n\'accepte pas les CERFA' do context 'Sauvegarde du CERFA PDF' do let!(:procedure) { create(:procedure) } - before do + before do post :create, dossier_id: dossier_id, - nom_projet: nom_projet, - description: description, - cerfa_pdf: cerfa_pdf + nom_projet: nom_projet, + description: description, + cerfa_pdf: cerfa_pdf dossier.reload end @@ -178,7 +178,7 @@ describe Users::DescriptionController, type: :controller do end end - context 'Sauvegarde des champs' do + describe 'Sauvegarde des champs' do let(:champs_dossier) { dossier.champs } let(:dossier_champs_first) { 'test value' } @@ -194,22 +194,31 @@ describe Users::DescriptionController, type: :controller do end it { expect(dossier.champs.first.value).to eq(dossier_champs_first) } + it { expect(response).to redirect_to users_dossier_recapitulatif_path } context 'when champs value is empty' do - let(:dossier_champs_first) { 'test value' } + let(:dossier_champs_first) { '' } it { expect(dossier.champs.first.value).to eq(dossier_champs_first) } + it { expect(response).to redirect_to users_dossier_recapitulatif_path } + + context 'when champs is mandatory' do + let(:procedure) { create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champ_mandatory, cerfa_flag: true) } + + it { expect(response).not_to redirect_to users_dossier_recapitulatif_path } + it { expect(flash[:alert]).to be_present } + end end end context 'Sauvegarde des pièces justificatives' do - let(:all_pj_type){ dossier.procedure.type_de_piece_justificative_ids } + let(:all_pj_type) { dossier.procedure.type_de_piece_justificative_ids } before do post :create, {dossier_id: dossier_id, - nom_projet: nom_projet, - description: description, - 'piece_justificative_'+all_pj_type[0].to_s => piece_justificative_0, - 'piece_justificative_'+all_pj_type[1].to_s => piece_justificative_1} + nom_projet: nom_projet, + description: description, + 'piece_justificative_'+all_pj_type[0].to_s => piece_justificative_0, + 'piece_justificative_'+all_pj_type[1].to_s => piece_justificative_1} dossier.reload end diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 43fddb844..c8dea5a80 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -27,6 +27,14 @@ FactoryGirl.define do end end + trait :with_type_de_champ_mandatory do + after(:build) do |procedure, _evaluator| + type_de_champ = create(:type_de_champ, mandatory: true) + + procedure.types_de_champ << type_de_champ + end + end + trait :with_two_type_de_piece_justificative do after(:build) do |procedure, _evaluator| rib = create(:type_de_piece_justificative, :rib) diff --git a/spec/factories/type_de_champ.rb b/spec/factories/type_de_champ.rb index 45b0f69e2..2b26e9ca0 100644 --- a/spec/factories/type_de_champ.rb +++ b/spec/factories/type_de_champ.rb @@ -4,5 +4,6 @@ FactoryGirl.define do description 'description de votre projet' type_champ 'textarea' order_place 1 + mandatory false end end