diff --git a/app/controllers/demandes_controller.rb b/app/controllers/demandes_controller.rb index 312cde9e9..0975837b8 100644 --- a/app/controllers/demandes_controller.rb +++ b/app/controllers/demandes_controller.rb @@ -5,9 +5,12 @@ class DemandesController < ApplicationController end def update - @dossier = Dossier.find(params[:dossier_id]) - @dossier.update_attributes(formulaire_id: params[:formulaire]) - + dossier = Dossier.find(params[:dossier_id]) + if !dossier.formulaire.nil? + raise "La modification du formulaire n'est pas possible" + end + dossier.update_attributes(formulaire_id: params[:formulaire]) + dossier.build_default_pieces_jointes redirect_to url_for(controller: :carte, action: :show, dossier_id: params[:dossier_id]) end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index d91564c4f..793ad4403 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -16,6 +16,12 @@ class Dossier < ActiveRecord::Base pieces_jointes.where(type_piece_jointe_id: piece_jointe_id).last end + def build_default_pieces_jointes + formulaire.types_piece_jointe.each do |type_piece_jointe| + PieceJointe.create(type_piece_jointe_id: type_piece_jointe.id, dossier_id: id) + end + end + private def build_default_cerfa diff --git a/spec/controllers/demandes_controller_spec.rb b/spec/controllers/demandes_controller_spec.rb index 767cb643a..8ecaf5744 100644 --- a/spec/controllers/demandes_controller_spec.rb +++ b/spec/controllers/demandes_controller_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' RSpec.describe DemandesController, type: :controller do - let(:dossier) { create(:dossier) } + let(:dossier) { create(:dossier, formulaire_id: '') } let (:dossier_id) { dossier.id } describe "GET #show" do @@ -11,12 +11,19 @@ RSpec.describe DemandesController, type: :controller do end end - describe 'POST #choice' do + describe 'POST #upated' do context 'dans tous les cas on affiche la carte' do it { - post :update, :dossier_id => dossier_id, :type_demande => '1' + post :update, :dossier_id => dossier_id, :formulaire => '1' expect(response).to redirect_to(controller: :carte, action: :show, dossier_id: dossier_id) } end + context 'when dossier is already linked to formaulaire' do + let(:dossier) { create(:dossier) } + subject { post :update, :dossier_id => dossier_id, :formulaire => '1' } + it 'raise error' do + expect{subject}.to raise_error("La modification du formulaire n'est pas possible") + end + end end end diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index a98aaa6f5..4db38b186 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -63,4 +63,16 @@ describe Dossier do expect(dossier.cerfa).to eq(Cerfa.last) end end + + describe '#build_default_pieces_jointes' do + context 'when dossier is linked to a formualire' do + let(:dossier) { create(:dossier) } + before do + dossier.build_default_pieces_jointes + end + it 'build all pieces jointes needed' do + expect(dossier.pieces_jointes.count).to eq(7) + end + end + end end \ No newline at end of file