From 27dbd3a87839635d6fbdfd0350f2830ce1c01a5c Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 3 Nov 2015 15:27:49 +0100 Subject: [PATCH] Add auto champs objects at a dossier (same as PJ) Add dynamic of Champs on the description page Rename model 'Champs' to 'Champ' --- app/assets/stylesheets/description.scss | 44 ++++++++++++ app/controllers/dossiers_controller.rb | 70 ------------------- .../users/description_controller.rb | 1 - app/controllers/users/dossiers_controller.rb | 1 + app/models/{champs.rb => champ.rb} | 3 +- app/models/dossier.rb | 9 +++ app/models/procedure.rb | 1 - app/models/type_de_champs.rb | 2 +- app/views/users/description/show.html.haml | 22 +++++- config/initializers/inflections.rb | 1 - spec/factories/dossier.rb | 3 +- spec/models/{champs_spec.rb => champ_spec.rb} | 2 +- spec/models/dossier_spec.rb | 23 +++++- spec/models/type_de_champs_spec.rb | 2 +- 14 files changed, 103 insertions(+), 81 deletions(-) create mode 100644 app/assets/stylesheets/description.scss rename app/models/{champs.rb => champ.rb} (77%) rename spec/models/{champs_spec.rb => champ_spec.rb} (96%) diff --git a/app/assets/stylesheets/description.scss b/app/assets/stylesheets/description.scss new file mode 100644 index 000000000..4d5f0a0be --- /dev/null +++ b/app/assets/stylesheets/description.scss @@ -0,0 +1,44 @@ +@import "bootstrap"; +@import "bootstrap-datepicker3"; + +#description_page #liste_champs{ + h4{ + padding-top: 10px; + } +} + +.type_champs-text { + @extend .col-md-6; + @extend .col-lg-6; + + input[type='text'] { + width: 100%; + } +} + +.type_champs-textarea { + @extend .col-md-9; + @extend .col-lg-9; + + textarea.form-control { + width:100%; + } +} + +.type_champs-number { + @extend .col-md-3; + @extend .col-lg-3; + + input[type='number']{ + width: 100%; + } +} + +.type_champs-datetime { + @extend .col-md-2; + @extend .col-lg-2; + + input[type='number']{ + width: 100%; + } +} diff --git a/app/controllers/dossiers_controller.rb b/app/controllers/dossiers_controller.rb index 8f8ad60f1..27bb6fd03 100644 --- a/app/controllers/dossiers_controller.rb +++ b/app/controllers/dossiers_controller.rb @@ -1,73 +1,3 @@ class DossiersController < ApplicationController - # def show - # @dossier = Dossier.find(params[:id]) - # @etablissement = @dossier.etablissement - # @entreprise = @dossier.entreprise.decorate - # rescue ActiveRecord::RecordNotFound - # flash.alert = t('errors.messages.dossier_not_found') - # redirect_to url_for(controller: :siret) - # end - - # def create - # procedure = Procedure.find(params['procedure_id']) - # @etablissement = Etablissement.new(SIADE::EtablissementAdapter.new(siret).to_params) - # @entreprise = Entreprise.new(SIADE::EntrepriseAdapter.new(siren).to_params) - # @dossier = Dossier.create - # @dossier.draft! - - # @dossier.procedure = procedure - # @dossier.save - - # @entreprise.dossier = @dossier - # @entreprise.save - - # @etablissement.dossier = @dossier - # @etablissement.entreprise = @entreprise - # @etablissement.save - - # redirect_to url_for(controller: :dossiers, action: :show, id: @dossier.id) - - # rescue RestClient::ResourceNotFound - # flash.alert = t('errors.messages.invalid_siret') - # redirect_to url_for(controller: :siret, procedure_id: params['procedure_id']) - # rescue ActiveRecord::RecordNotFound - # flash.alert = t('errors.messages.dossier_not_found') - # redirect_to url_for(controller: :siret) - # end - - # def update - # @dossier = Dossier.find(params[:id]) - # if checked_autorisation_donnees? - # @dossier.update_attributes(update_params) - # redirect_to url_for(controller: :description, action: :show, dossier_id: @dossier.id) - # else - # @etablissement = @dossier.etablissement - # @entreprise = @dossier.entreprise.decorate - # flash.now.alert = 'Les conditions sont obligatoires.' - # render 'show' - # end - # end - - # private - - # def update_params - # params.require(:dossier).permit(:autorisation_donnees) - # end - - # def dossier_id_is_present? - # @dossier_id != '' - # end - - # def checked_autorisation_donnees? - # update_params[:autorisation_donnees] == '1' - # end - - # def siret - # params[:siret] - # end - - # def siren - # siret[0..8] - # end end diff --git a/app/controllers/users/description_controller.rb b/app/controllers/users/description_controller.rb index aca7d4a3c..5291f720d 100644 --- a/app/controllers/users/description_controller.rb +++ b/app/controllers/users/description_controller.rb @@ -50,7 +50,6 @@ class Users::DescriptionController < UsersController flash.notice = 'Félicitation, votre demande a bien été enregistrée.' redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier.id) - end private diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 1287b9bc8..a52d1c474 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -1,5 +1,6 @@ class Users::DossiersController < UsersController before_action :authenticate_user! + def index @dossiers = current_user.dossiers.order(updated_at: 'DESC').decorate end diff --git a/app/models/champs.rb b/app/models/champ.rb similarity index 77% rename from app/models/champs.rb rename to app/models/champ.rb index aa9cd6068..6ed992fc4 100644 --- a/app/models/champs.rb +++ b/app/models/champ.rb @@ -1,6 +1,5 @@ -class Champs < ActiveRecord::Base +class Champ < ActiveRecord::Base belongs_to :dossier belongs_to :type_de_champs delegate :libelle, :type_champs, :order_place, to: :type_de_champs - end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 340cd8fbe..aaddc4e2a 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -19,10 +19,12 @@ class Dossier < ActiveRecord::Base delegate :siren, to: :entreprise delegate :siret, to: :etablissement delegate :types_de_piece_justificative, to: :procedure + delegate :types_de_champs, to: :procedure before_create :build_default_cerfa after_save :build_default_pieces_justificatives, if: Proc.new { procedure_id_changed? } + after_save :build_default_champs, if: Proc.new { procedure_id_changed? } validates :nom_projet, presence: true, allow_blank: false, allow_nil: true validates :description, presence: true, allow_blank: false, allow_nil: true @@ -33,11 +35,18 @@ class Dossier < ActiveRecord::Base end def build_default_pieces_justificatives + procedure.types_de_piece_justificative.each do |type_de_piece_justificative| PieceJustificative.create(type_de_piece_justificative_id: type_de_piece_justificative.id, dossier_id: id) end end + def build_default_champs + procedure.types_de_champs.each do |type_de_champs| + Champ.create(type_de_champs_id: type_de_champs.id, dossier_id: id) + end + end + def sous_domaine if Rails.env.production? 'tps' diff --git a/app/models/procedure.rb b/app/models/procedure.rb index 099f25479..421732d22 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -6,5 +6,4 @@ class Procedure < ActiveRecord::Base validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :description, presence: true, allow_blank: false, allow_nil: false - #validates :lien_demarche, presence: true, allow_blank: false, allow_nil: false end diff --git a/app/models/type_de_champs.rb b/app/models/type_de_champs.rb index 9692f11f0..ad2bd37be 100644 --- a/app/models/type_de_champs.rb +++ b/app/models/type_de_champs.rb @@ -6,7 +6,7 @@ class TypeDeChamps < ActiveRecord::Base } belongs_to :procedure - has_many :champs + has_many :champ validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :type_champs, presence: true, allow_blank: false, allow_nil: false diff --git a/app/views/users/description/show.html.haml b/app/views/users/description/show.html.haml index 3e12ce168..f44c673c1 100644 --- a/app/views/users/description/show.html.haml +++ b/app/views/users/description/show.html.haml @@ -3,7 +3,6 @@ %br = form_tag(url_for({controller: :description, action: :create, dossier_id: @dossier.id}), class: 'form-inline', method: 'POST', multipart: true) do - %div .row .col-md-12 @@ -15,6 +14,27 @@ %h4 Description de votre projet * = text_area_tag :description, @dossier.description, rows: '6', placeholder: 'Description du projet', class: 'form-control' %br + + #liste_champs.row + -@dossier.champs.each do |champ| + %div{class: "type_champs-#{champ.type_champs}"} + %h4 + = champ.libelle + + -if champ.type_champs == 'textarea' + %textarea.form-control{name:"champs[#{champ.id}]", + placeholder: champ.libelle, + id: "champs_#{champ.id}"} + =champ.value + -else + %input.form-control{name:"champs[#{champ.id}]", + value: champ.value, + placeholder: champ.libelle, + id: "champs_#{champ.id}", + type:"#{champ.type_champs}", + 'data-provide' => ('datepicker' if champ.type_champs == 'datetime'), + 'data-date-format' => ('dd/mm/yyyy' if champ.type_champs == 'datetime')} + %h3 Documents administratifs %br diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index 99c88f5ee..dcad59e52 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -11,7 +11,6 @@ ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym 'API' inflect.irregular 'piece_justificative', 'pieces_justificatives' inflect.irregular 'type_de_piece_justificative', 'types_de_piece_justificative' - inflect.irregular 'champs', 'champs' inflect.irregular 'type_de_champs', 'types_de_champs' end diff --git a/spec/factories/dossier.rb b/spec/factories/dossier.rb index 589719c3d..378d4f745 100644 --- a/spec/factories/dossier.rb +++ b/spec/factories/dossier.rb @@ -2,6 +2,7 @@ FactoryGirl.define do factory :dossier do nom_projet "Demande de subvention dans le cadre d'accompagnement d'enfant à l'étranger" state 'draft' + trait :with_entreprise do after(:build) do |dossier, _evaluator| etablissement = create(:etablissement) @@ -13,7 +14,7 @@ FactoryGirl.define do trait :with_procedure do after(:build) do |dossier, _evaluator| - procedure = create(:procedure, :with_two_type_de_piece_justificative) + procedure = create(:procedure, :with_two_type_de_piece_justificative, :with_type_de_champs) dossier.procedure = procedure end end diff --git a/spec/models/champs_spec.rb b/spec/models/champ_spec.rb similarity index 96% rename from spec/models/champs_spec.rb rename to spec/models/champ_spec.rb index 9ca345173..05d39d1c1 100644 --- a/spec/models/champs_spec.rb +++ b/spec/models/champ_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe Champs do +describe Champ do describe 'database columns' do it { is_expected.to have_db_column(:value) } end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index b4cb7f310..a450add60 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -28,6 +28,7 @@ describe Dossier do it { is_expected.to delegate_method(:siren).to(:entreprise) } it { is_expected.to delegate_method(:siret).to(:etablissement) } it { is_expected.to delegate_method(:types_de_piece_justificative).to(:procedure) } + it { is_expected.to delegate_method(:types_de_champs).to(:procedure) } end describe 'validation' do @@ -91,12 +92,27 @@ describe Dossier do end end + describe '#build_default_champs' do + context 'when dossier is linked to a procedure' do + let(:dossier) { create(:dossier, :with_procedure, user: user) } + it 'build all champs needed' do + expect(dossier.champs.count).to eq(1) + end + end + end + describe '#save' do subject { create(:dossier, procedure_id: nil, 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: 1) + subject.update_attributes(procedure_id: procedure.id) + end + + it 'creates default champs' do + expect(subject).to receive(:build_default_champs) + subject.update_attributes(procedure_id: procedure.id) end end context 'when is not linked to a procedure' do @@ -104,6 +120,11 @@ describe Dossier do expect(subject).not_to receive(:build_default_pieces_justificatives) subject.update_attributes(description: 'plop') end + + it 'does not create default champss' do + expect(subject).not_to receive(:build_default_champs) + subject.update_attributes(description: 'plop') + end end end diff --git a/spec/models/type_de_champs_spec.rb b/spec/models/type_de_champs_spec.rb index 9885f29cc..1295a5a53 100644 --- a/spec/models/type_de_champs_spec.rb +++ b/spec/models/type_de_champs_spec.rb @@ -10,7 +10,7 @@ describe TypeDeChamps do describe 'associations' do it { is_expected.to belong_to(:procedure) } - it { is_expected.to have_many(:champs) } + it { is_expected.to have_many(:champ) } end describe 'validation' do