build default pieces jointes when user choose formulaire

This commit is contained in:
Tanguy PATTE 2015-08-18 15:15:34 +02:00
parent 892484b494
commit 516cafc72a
4 changed files with 34 additions and 6 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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