Add auto champs objects at a dossier (same as PJ)

Add dynamic of Champs on the description page
Rename model 'Champs' to 'Champ'
This commit is contained in:
Xavier J 2015-11-03 15:27:49 +01:00
parent 56eccf55df
commit 27dbd3a878
14 changed files with 103 additions and 81 deletions

View file

@ -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%;
}
}

View file

@ -1,73 +1,3 @@
class DossiersController < ApplicationController 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 end

View file

@ -50,7 +50,6 @@ class Users::DescriptionController < UsersController
flash.notice = 'Félicitation, votre demande a bien été enregistrée.' flash.notice = 'Félicitation, votre demande a bien été enregistrée.'
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier.id) redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier.id)
end end
private private

View file

@ -1,5 +1,6 @@
class Users::DossiersController < UsersController class Users::DossiersController < UsersController
before_action :authenticate_user! before_action :authenticate_user!
def index def index
@dossiers = current_user.dossiers.order(updated_at: 'DESC').decorate @dossiers = current_user.dossiers.order(updated_at: 'DESC').decorate
end end

View file

@ -1,6 +1,5 @@
class Champs < ActiveRecord::Base class Champ < ActiveRecord::Base
belongs_to :dossier belongs_to :dossier
belongs_to :type_de_champs belongs_to :type_de_champs
delegate :libelle, :type_champs, :order_place, to: :type_de_champs delegate :libelle, :type_champs, :order_place, to: :type_de_champs
end end

View file

@ -19,10 +19,12 @@ class Dossier < ActiveRecord::Base
delegate :siren, to: :entreprise delegate :siren, to: :entreprise
delegate :siret, to: :etablissement delegate :siret, to: :etablissement
delegate :types_de_piece_justificative, to: :procedure delegate :types_de_piece_justificative, to: :procedure
delegate :types_de_champs, to: :procedure
before_create :build_default_cerfa before_create :build_default_cerfa
after_save :build_default_pieces_justificatives, 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? }
validates :nom_projet, presence: true, allow_blank: false, allow_nil: true validates :nom_projet, presence: true, allow_blank: false, allow_nil: true
validates :description, 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 end
def build_default_pieces_justificatives def build_default_pieces_justificatives
procedure.types_de_piece_justificative.each do |type_de_piece_justificative| 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) PieceJustificative.create(type_de_piece_justificative_id: type_de_piece_justificative.id, dossier_id: id)
end end
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 def sous_domaine
if Rails.env.production? if Rails.env.production?
'tps' 'tps'

View file

@ -6,5 +6,4 @@ class Procedure < ActiveRecord::Base
validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :description, 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 end

View file

@ -6,7 +6,7 @@ class TypeDeChamps < ActiveRecord::Base
} }
belongs_to :procedure belongs_to :procedure
has_many :champs has_many :champ
validates :libelle, presence: true, allow_blank: false, allow_nil: false validates :libelle, presence: true, allow_blank: false, allow_nil: false
validates :type_champs, presence: true, allow_blank: false, allow_nil: false validates :type_champs, presence: true, allow_blank: false, allow_nil: false

View file

@ -3,7 +3,6 @@
%br %br
= form_tag(url_for({controller: :description, action: :create, dossier_id: @dossier.id}), class: 'form-inline', method: 'POST', multipart: true) do = form_tag(url_for({controller: :description, action: :create, dossier_id: @dossier.id}), class: 'form-inline', method: 'POST', multipart: true) do
%div %div
.row .row
.col-md-12 .col-md-12
@ -15,6 +14,27 @@
%h4 Description de votre projet * %h4 Description de votre projet *
= text_area_tag :description, @dossier.description, rows: '6', placeholder: 'Description du projet', class: 'form-control' = text_area_tag :description, @dossier.description, rows: '6', placeholder: 'Description du projet', class: 'form-control'
%br %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 %h3 Documents administratifs
%br %br

View file

@ -11,7 +11,6 @@ ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'API' inflect.acronym 'API'
inflect.irregular 'piece_justificative', 'pieces_justificatives' inflect.irregular 'piece_justificative', 'pieces_justificatives'
inflect.irregular 'type_de_piece_justificative', 'types_de_piece_justificative' inflect.irregular 'type_de_piece_justificative', 'types_de_piece_justificative'
inflect.irregular 'champs', 'champs'
inflect.irregular 'type_de_champs', 'types_de_champs' inflect.irregular 'type_de_champs', 'types_de_champs'
end end

View file

@ -2,6 +2,7 @@ FactoryGirl.define do
factory :dossier do factory :dossier do
nom_projet "Demande de subvention dans le cadre d'accompagnement d'enfant à l'étranger" nom_projet "Demande de subvention dans le cadre d'accompagnement d'enfant à l'étranger"
state 'draft' state 'draft'
trait :with_entreprise do trait :with_entreprise do
after(:build) do |dossier, _evaluator| after(:build) do |dossier, _evaluator|
etablissement = create(:etablissement) etablissement = create(:etablissement)
@ -13,7 +14,7 @@ FactoryGirl.define do
trait :with_procedure do trait :with_procedure do
after(:build) do |dossier, _evaluator| 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 dossier.procedure = procedure
end end
end end

View file

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe Champs do describe Champ do
describe 'database columns' do describe 'database columns' do
it { is_expected.to have_db_column(:value) } it { is_expected.to have_db_column(:value) }
end end

View file

@ -28,6 +28,7 @@ describe Dossier do
it { is_expected.to delegate_method(:siren).to(:entreprise) } it { is_expected.to delegate_method(:siren).to(:entreprise) }
it { is_expected.to delegate_method(:siret).to(:etablissement) } 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_piece_justificative).to(:procedure) }
it { is_expected.to delegate_method(:types_de_champs).to(:procedure) }
end end
describe 'validation' do describe 'validation' do
@ -91,12 +92,27 @@ describe Dossier do
end end
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 describe '#save' do
subject { create(:dossier, procedure_id: nil, user: user) } subject { create(:dossier, procedure_id: nil, user: user) }
let!(:procedure) { create(:procedure) }
context 'when is linked to a procedure' do context 'when is linked to a procedure' do
it 'creates default pieces justificatives' do it 'creates default pieces justificatives' do
expect(subject).to receive(:build_default_pieces_justificatives) 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
end end
context 'when is not linked to a procedure' do 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) expect(subject).not_to receive(:build_default_pieces_justificatives)
subject.update_attributes(description: 'plop') subject.update_attributes(description: 'plop')
end end
it 'does not create default champss' do
expect(subject).not_to receive(:build_default_champs)
subject.update_attributes(description: 'plop')
end
end end
end end

View file

@ -10,7 +10,7 @@ describe TypeDeChamps do
describe 'associations' do describe 'associations' do
it { is_expected.to belong_to(:procedure) } it { is_expected.to belong_to(:procedure) }
it { is_expected.to have_many(:champs) } it { is_expected.to have_many(:champ) }
end end
describe 'validation' do describe 'validation' do