Skip to content

Commit

Permalink
✨ Change l'export afin d'exporter unz ou plusieurs à la fois
Browse files Browse the repository at this point in the history
  • Loading branch information
Guitguitou committed Nov 13, 2024
1 parent 0cddd3f commit 228b6a0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 39 deletions.
4 changes: 3 additions & 1 deletion app/admin/questions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
60 changes: 32 additions & 28 deletions app/models/import_export/questions/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions app/models/import_export/questions/import_export_donnees.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
8 changes: 5 additions & 3 deletions spec/models/import_export/questions/export_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 228b6a0

Please sign in to comment.