From 228b6a06c71e7ae9638370a2fde9628fcd441d80 Mon Sep 17 00:00:00 2001 From: guillaume imary Date: Wed, 13 Nov 2024 17:11:04 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Change=20l'export=20afin=20d'export?= =?UTF-8?q?er=20unz=20ou=20plusieurs=20=C3=A0=20la=20fois?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/questions.rb | 4 +- app/models/import_export/questions/export.rb | 60 ++++++++++--------- .../questions/import_export_donnees.rb | 10 ++-- .../import_export/questions/export_spec.rb | 8 ++- .../questions/import_export_donnees_spec.rb | 4 +- 5 files changed, 47 insertions(+), 39 deletions(-) diff --git a/app/admin/questions.rb b/app/admin/questions.rb index 71c0e6ab7..09a65b702 100644 --- a/app/admin/questions.rb +++ b/app/admin/questions.rb @@ -5,7 +5,9 @@ member_action :export_xls, method: :get do question = Question.find(params[:id]) - export = ImportExport::Questions::ImportExportDonnees.new(question: question).exporte_donnees + export = + ImportExport::Questions::ImportExportDonnees.new(questions: [question], type: question.type) + .exporte_donnees send_data export[:xls], content_type: export[:content_type], filename: export[:filename] diff --git a/app/models/import_export/questions/export.rb b/app/models/import_export/questions/export.rb index 46ad6c5be..80c775553 100644 --- a/app/models/import_export/questions/export.rb +++ b/app/models/import_export/questions/export.rb @@ -3,9 +3,9 @@ module ImportExport module Questions class Export < ::ImportExport::ExportXls - def initialize(question, headers) + def initialize(questions, headers) super() - @question = question + @questions = questions @headers = headers end @@ -16,31 +16,35 @@ def to_xls retourne_le_contenu_du_xls end - def nom_du_fichier - genere_fichier(@question.nom_technique) + def nom_du_fichier(type) + genere_fichier(type) end private def remplie_la_feuille - @headers.each { |_valeur| remplis_champs_commun } + @questions.each_with_index do |question, index| + @question = question + @ligne = index + 1 + remplis_champs_commun + end end def remplis_champs_commun - @sheet[1, 0] = @question.libelle - @sheet[1, 1] = @question.nom_technique - @sheet[1, 2] = @question.illustration_url - @sheet[1, 3] = @question.transcription_intitule&.ecrit - @sheet[1, 4] = @question.transcription_intitule&.audio_url + @sheet[@ligne, 0] = @question.libelle + @sheet[@ligne, 1] = @question.nom_technique + @sheet[@ligne, 2] = @question.illustration_url + @sheet[@ligne, 3] = @question.transcription_intitule&.ecrit + @sheet[@ligne, 4] = @question.transcription_intitule&.audio_url remplis_champs_additionnels end def remplis_champs_additionnels return if @question.sous_consigne? - @sheet[1, 5] = @question.transcription_modalite_reponse&.ecrit - @sheet[1, 6] = @question.transcription_modalite_reponse&.audio_url - @sheet[1, 7] = @question.description + @sheet[@ligne, 6] = @question.transcription_modalite_reponse&.audio_url + @sheet[@ligne, 5] = @question.transcription_modalite_reponse&.ecrit + @sheet[@ligne, 7] = @question.description remplis_champs_specifiques end @@ -54,43 +58,43 @@ def remplis_champs_specifiques end def remplis_champs_clic_dans_image - @sheet[1, 8] = @question.zone_cliquable_url - @sheet[1, 9] = @question.image_au_clic_url + @sheet[@ligne, 8] = @question.zone_cliquable_url + @sheet[@ligne, 9] = @question.image_au_clic_url end def remplis_champs_glisser_deposer - @sheet[1, 8] = @question.zone_depot_url - @question.reponses.each_with_index { |choix, index| ajoute_reponses(choix, 1, index) } + @sheet[@ligne, 8] = @question.zone_depot_url + @question.reponses.each_with_index { |choix, index| ajoute_reponses(choix, index) } end def remplis_champs_saisie - @sheet[1, 8] = @question.suffix_reponse - @sheet[1, 9] = @question.reponse_placeholder - @sheet[1, 10] = @question.type_saisie + @sheet[@ligne, 8] = @question.suffix_reponse + @sheet[@ligne, 9] = @question.reponse_placeholder + @sheet[@ligne, 10] = @question.type_saisie return unless @question.bonne_reponse - @sheet[1, 11] = @question.bonne_reponse.intitule - @sheet[1, 12] = @question.bonne_reponse.nom_technique + @sheet[@ligne, 11] = @question.bonne_reponse.intitule + @sheet[@ligne, 12] = @question.bonne_reponse.nom_technique end def remplis_champs_qcm - @sheet[1, 8] = @question.type_qcm - @question.choix.each_with_index { |choix, index| ajoute_choix(choix, 1, index) } + @sheet[@ligne, 8] = @question.type_qcm + @question.choix.each_with_index { |choix, index| ajoute_choix(choix, index) } end - def ajoute_choix(choix, ligne, index) + def ajoute_choix(choix, index) columns = %w[intitule nom_technique type_choix audio] columns.each_with_index do |col, i| @sheet[0, 9 + (index * 4) + i] = "choix_#{index + 1}_#{col}" - @sheet[ligne, 9 + (index * 4) + i] = choix.send(col) + @sheet[@ligne, 9 + (index * 4) + i] = choix.send(col) end end - def ajoute_reponses(choix, ligne, index) + def ajoute_reponses(choix, index) columns = %w[nom_technique position_client type_choix illustration] columns.each_with_index do |col, i| @sheet[0, 9 + (index * 4) + i] = "reponse_#{index + 1}_#{col}" - @sheet[ligne, 9 + (index * 4) + i] = choix.send(col) + @sheet[@ligne, 9 + (index * 4) + i] = choix.send(col) end end end diff --git a/app/models/import_export/questions/import_export_donnees.rb b/app/models/import_export/questions/import_export_donnees.rb index 1ca9fd85d..03c3ee1e1 100644 --- a/app/models/import_export/questions/import_export_donnees.rb +++ b/app/models/import_export/questions/import_export_donnees.rb @@ -18,9 +18,9 @@ class ImportExportDonnees 'QuestionSaisie' => HEADERS_COMMUN + HEADERS_SAISIE, 'QuestionSousConsigne' => HEADERS_SOUS_CONSIGNE }.freeze - def initialize(question: nil, type: nil) - @question = question - @type = type || question.type + def initialize(questions: nil, type: nil) + @questions = questions + @type = type end def importe_donnees(file) @@ -30,11 +30,11 @@ def importe_donnees(file) end def exporte_donnees - export = Export.new(@question, HEADERS_ATTENDUS[@type]) + export = Export.new(@questions, HEADERS_ATTENDUS[@type]) { xls: export.to_xls, content_type: export.content_type_xls, - filename: export.nom_du_fichier + filename: export.nom_du_fichier(@type) } end diff --git a/spec/models/import_export/questions/export_spec.rb b/spec/models/import_export/questions/export_spec.rb index db5ae581f..654ea0ba3 100644 --- a/spec/models/import_export/questions/export_spec.rb +++ b/spec/models/import_export/questions/export_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' describe ImportExport::Questions::Export do - subject(:response_service) { described_class.new(question, headers) } + subject(:response_service) { described_class.new([question], headers) } let(:headers) { [] } let(:question) { create(:question) } @@ -154,11 +154,13 @@ end describe '#nom_du_fichier' do + let(:question) { create(:question_qcm) } + it 'genere le nom du fichier' do date = DateTime.current.strftime('%Y%m%d') - nom_du_fichier_attendu = "#{date}-#{question.nom_technique}.xls" + nom_du_fichier_attendu = "#{date}-#{question.type}.xls" - expect(response_service.nom_du_fichier).to eq(nom_du_fichier_attendu) + expect(response_service.nom_du_fichier(question.type)).to eq(nom_du_fichier_attendu) end end diff --git a/spec/models/import_export/questions/import_export_donnees_spec.rb b/spec/models/import_export/questions/import_export_donnees_spec.rb index f56a0333c..3b463c513 100644 --- a/spec/models/import_export/questions/import_export_donnees_spec.rb +++ b/spec/models/import_export/questions/import_export_donnees_spec.rb @@ -5,14 +5,14 @@ describe ImportExport::Questions::ImportExportDonnees do describe '#exporte_donnees' do subject(:service) do - described_class.new(question: question) + described_class.new(questions: [question], type: question.type) end let!(:question) { create(:question_clic_dans_image) } it 'exporte les données' do date = DateTime.current.strftime('%Y%m%d') - nom_du_fichier_attendu = "#{date}-#{question.nom_technique}.xls" + nom_du_fichier_attendu = "#{date}-#{question.type}.xls" expect(service.exporte_donnees[:xls]).not_to be_nil expect(service.exporte_donnees[:content_type]).to eq 'application/vnd.ms-excel' expect(service.exporte_donnees[:filename]).to eq nom_du_fichier_attendu