Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ Utilise la nouvelle classe export_xls pour l'export question #1726

Merged
merged 2 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/admin/questions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

member_action :export_xls, method: :get do
question = Question.find(params[:id])
export = ImportExportQuestion.new(question).exporte_donnees
export = ImportExport.new(question).exporte_donnees
send_data export[:xls],
content_type: export[:content_type],
filename: export[:filename]
Expand All @@ -18,17 +18,17 @@ def import_xls
question = recupere_question
flash[:success] = I18n.t('.layouts.succes.import_question')
redirect_to redirection_apres_import(question)
rescue ImportQuestion::Error => e
rescue Question::Import::Error => e
erreur_import(e)
rescue ImportXls::Error => e
raise ImportExportQuestion::Error, e.message
rescue ImportExport::ImportXls::Error => e
raise Question::ImportExport::Error, e.message
end

private

def recupere_question
question = Question.new(type: params[:type])
ImportExportQuestion.new(question).importe_donnees(params[:file_xls])
Question::ImportExport.new(question).importe_donnees(params[:file_xls])
end

def erreur_import(error)
Expand Down
120 changes: 0 additions & 120 deletions app/models/export_question.rb

This file was deleted.

32 changes: 0 additions & 32 deletions app/models/export_xls.rb

This file was deleted.

40 changes: 40 additions & 0 deletions app/models/import_export/export_xls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module ImportExport
class ExportXls
attr_reader :workbook, :sheet

WORKSHEET = 'Données'

def initialize(entetes: [])
@entetes = entetes
@workbook = Spreadsheet::Workbook.new
initialise_sheet
end

def content_type_xls
'application/vnd.ms-excel'
end

def genere_fichier(titre)
date = DateTime.current.strftime('%Y%m%d')
"#{date}-#{titre}.xls"
end

def initialise_sheet
@sheet = @workbook.create_worksheet(name: WORKSHEET)
format_premiere_ligne = Spreadsheet::Format.new(weight: :bold)
@sheet.row(0).default_format = format_premiere_ligne
@entetes.each_with_index do |entete, colonne|
@sheet[0, colonne] = entete[:titre]
@sheet.column(colonne).width = entete[:taille]
end
end

def retourne_le_contenu_du_xls
file_contents = StringIO.new
@sheet.workbook.write file_contents
file_contents.string
end
end
end
64 changes: 64 additions & 0 deletions app/models/import_export/import_xls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

module ImportExport
class ImportXls
class Error < StandardError; end

def initialize(type, headers)
@type = type
@headers_attendus = headers
end

def valide_headers
return if headers_valides?

raise Error, message_erreur_headers
end

def message_erreur_headers
I18n.t('.layouts.erreurs.import_question.mauvais_format',
headers: headers_attendus_to_s)
end

def headers_valides?
headers_serialises = @headers.map do |header|
header.parameterize.underscore.to_sym if header
end
headers_serialises[0, @headers_attendus.length] == @headers_attendus
end

def headers_attendus_to_s
@headers_attendus.map do |h|
h.to_s.tr('_', ' ')
end.join(', ')
end

def recupere_data(file)
sheet = Spreadsheet.open(file.path).worksheet(0)
@data = sheet.rows[1]
@headers = sheet.rows[0]
end

def telecharge_fichier(url)
@current_download = url
return unless url

fichier = Down.download(url)
content_type = Marcel::MimeType.for(fichier.path, name: fichier.original_filename)

{ io: fichier,
filename: fichier.original_filename,
content_type: content_type }
rescue Down::Error
raise Error, message_erreur_telechargement(@current_download)
end

def message_erreur_telechargement(current_download)
"Impossible de télécharger un fichier depuis l'url : #{current_download}"
end

def attache_fichier(attachment, url)
attachment.attach(telecharge_fichier(url)) if url.present?
end
end
end
42 changes: 0 additions & 42 deletions app/models/import_export_question.rb

This file was deleted.

Loading