From 954ac2bc8695bb878e479f1e5ef2ec7284768845 Mon Sep 17 00:00:00 2001 From: Xavier J Date: Tue, 14 Jun 2016 11:59:50 +0200 Subject: [PATCH] Fix bug revisualization controller --- .../admin/previsualisations_controller.rb | 2 +- app/services/previsualisation_service.rb | 5 +++ .../services/previsualisation_service_spec.rb | 31 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 app/services/previsualisation_service.rb create mode 100644 spec/services/previsualisation_service_spec.rb diff --git a/app/controllers/admin/previsualisations_controller.rb b/app/controllers/admin/previsualisations_controller.rb index 22f39d343..a5c873e7f 100644 --- a/app/controllers/admin/previsualisations_controller.rb +++ b/app/controllers/admin/previsualisations_controller.rb @@ -5,7 +5,7 @@ class Admin::PrevisualisationsController < AdminController @procedure @dossier = Dossier.new(id: 0, procedure: @procedure) - Champ.where(dossier_id: @dossier.id).destroy_all + PrevisualisationService.destroy_all_champs @dossier @dossier.build_default_champs @champs = @dossier.ordered_champs diff --git a/app/services/previsualisation_service.rb b/app/services/previsualisation_service.rb new file mode 100644 index 000000000..316c0a4cd --- /dev/null +++ b/app/services/previsualisation_service.rb @@ -0,0 +1,5 @@ +class PrevisualisationService + def self.destroy_all_champs dossier + Champ.where(dossier_id: dossier.id, type_de_champ_id: dossier.procedure.types_de_champ.ids).destroy_all + end +end \ No newline at end of file diff --git a/spec/services/previsualisation_service_spec.rb b/spec/services/previsualisation_service_spec.rb new file mode 100644 index 000000000..2fcc4ee00 --- /dev/null +++ b/spec/services/previsualisation_service_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe PrevisualisationService do + + describe '.destroy_all_champs' do + subject { described_class.destroy_all_champs dossier } + + let(:procedure_1) { create :procedure, :with_type_de_champ } + let(:procedure_2) { create :procedure, :with_type_de_champ } + + let!(:dossier_1) { create :dossier, procedure: procedure_1 } + let!(:dossier_2) { create :dossier, procedure: procedure_2 } + + it { expect(Dossier.all.size).to eq 2 } + it { expect(TypeDeChamp.all.size).to eq 2 } + it { expect(Champ.all.size).to eq 2 } + + context 'when function destroy_all_champs is call' do + let(:dossier) { dossier_1 } + + before do + subject + end + + it { expect(Dossier.all.size).to eq 2 } + it { expect(TypeDeChamp.all.size).to eq 2 } + it { expect(Champ.all.size).to eq 1 } + it { expect(Champ.first.type_de_champ).to eq procedure_2.types_de_champ.first } + end + end +end \ No newline at end of file