Skip to content

Commit

Permalink
✨ Zip les fichers excel
Browse files Browse the repository at this point in the history
  • Loading branch information
Guitguitou committed Nov 14, 2024
1 parent d626c03 commit 7d2404e
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 35 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ gem 'recipient_interceptor'
gem 'redcarpet'
gem 'redis'
gem 'rollbar'
gem 'rubyzip'
gem 'sidekiq', '< 8'
gem 'sidekiq-scheduler'
gem 'spreadsheet'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ GEM
ruby-progressbar (1.13.0)
ruby-rc4 (0.1.5)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
rufus-scheduler (3.9.2)
fugit (~> 1.1, >= 1.11.1)
sass-rails (6.0.0)
Expand Down Expand Up @@ -714,6 +715,7 @@ DEPENDENCIES
rubocop-packaging
rubocop-rails
rubocop-rspec
rubyzip
sass-rails (>= 6)
shoulda-matchers
sidekiq (< 8)
Expand Down
31 changes: 21 additions & 10 deletions app/admin/questionnaires.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'zip'

ActiveAdmin.register Questionnaire do
menu parent: 'Parcours', if: proc { can? :manage, Compte }

Expand All @@ -11,8 +13,7 @@
member_action :export_questions, method: :get

action_item :export_questions, only: :show do
link_to 'Exporter les questions en XLS',
export_questions_admin_questionnaire_path(resource),
link_to 'Exporter les questions en XLS', export_questions_admin_questionnaire_path(resource),
format: :xls
end

Expand Down Expand Up @@ -55,15 +56,25 @@ def find_resource

def export_questions
questionnaire = find_resource
questions_par_type = questionnaire.questions_par_type
compressed_filestream = generate_zip_file(questionnaire.questions_par_type)

questions_par_type.each do |type, questions|
export = ImportExport::Questions::ImportExportDonnees.new(questions: questions,
type: type).exporte_donnees
send_data export[:xls],
content_type: export[:content_type],
filename: export[:filename]
end
send_data compressed_filestream.read,
filename: "questionnaire_#{questionnaire.id}_export.zip",
type: 'application/zip'
end

private

def generate_zip_file(questions_par_type)
Zip::OutputStream.write_buffer do |zip|
questions_par_type.each do |type, questions|
export =
ImportExport::Questions::ImportExportDonnees.new(questions: questions,
type: type).exporte_donnees
zip.put_next_entry("#{type}_questions_export.xls")
zip.write export[:xls]
end
end.tap(&:rewind)
end
end
end
5 changes: 2 additions & 3 deletions app/admin/questions_clic_dans_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@
end

action_item :importer_question, only: :index do
link_to 'Importer question clic dans image',
link_to 'Importer questions clic dans image',
admin_import_xls_path(type: 'QuestionClicDansImage')
end

action_item :exporter_question, only: :show do
link_to 'Exporter le contenu de la question',
admin_question_export_xls_path(question_id: params[:id])
link_to 'Exporter la question en XLS', admin_question_export_xls_path(question_id: params[:id])
end

show do
Expand Down
5 changes: 2 additions & 3 deletions app/admin/questions_glisser_deposer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
form partial: 'form'

action_item :importer_question, only: :index do
link_to 'Importer question glisser déposer',
link_to 'Importer questions glisser déposer',
admin_import_xls_path(type: 'QuestionGlisserDeposer')
end

action_item :exporter_question, only: :show do
link_to 'Exporter le contenu de la question',
admin_question_export_xls_path(question_id: params[:id])
link_to 'Exporter la question en XLS', admin_question_export_xls_path(question_id: params[:id])
end

index do
Expand Down
6 changes: 2 additions & 4 deletions app/admin/questions_qcm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
form partial: 'form'

action_item :importer_question, only: :index do
link_to 'Importer question qcm',
admin_import_xls_path(type: 'QuestionQcm')
link_to 'Importer questions QCM', admin_import_xls_path(type: 'QuestionQcm')
end

action_item :exporter_question, only: :show do
link_to 'Exporter le contenu de la question',
admin_question_export_xls_path(question_id: params[:id])
link_to 'Exporter la question en XLS', admin_question_export_xls_path(question_id: params[:id])
end

index do
Expand Down
6 changes: 2 additions & 4 deletions app/admin/questions_saisie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
filter :nom_technique

action_item :importer_question, only: :index do
link_to 'Importer question saisie',
admin_import_xls_path(type: 'QuestionSaisie')
link_to 'Importer questions saisies', admin_import_xls_path(type: 'QuestionSaisie')
end

action_item :exporter_question, only: :show do
link_to 'Exporter le contenu de la question',
admin_question_export_xls_path(question_id: params[:id])
link_to 'Exporter la question en XLS', admin_question_export_xls_path(question_id: params[:id])
end

form partial: 'form'
Expand Down
6 changes: 2 additions & 4 deletions app/admin/questions_sous_consigne.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
filter :nom_technique

action_item :importer_question, only: :index do
link_to 'Importer question sous consigne',
admin_import_xls_path(type: 'QuestionSousConsigne')
link_to 'Importer questions sous consigne', admin_import_xls_path(type: 'QuestionSousConsigne')
end

action_item :exporter_question, only: :show do
link_to 'Exporter le contenu de la question',
admin_question_export_xls_path(question_id: params[:id])
link_to 'Exporter la question en XLS', admin_question_export_xls_path(question_id: params[:id])
end

form do |f|
Expand Down
6 changes: 3 additions & 3 deletions spec/features/admin/question_clic_dans_image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
end

it "affiche le bouton d'export" do
expect(page).to have_link 'Exporter le contenu de la question'
expect(page).to have_link 'Exporter la question en XLS'
within('.action-items-sidebar') do
click_on 'Exporter le contenu de la question'
click_on 'Exporter la question en XLS'
end
expect(page.response_headers['Content-Type']).to eq 'application/vnd.ms-excel'
end
Expand All @@ -48,7 +48,7 @@

it "redirige vers le formulaire d'importation de question" do
within('.action-items-sidebar') do
click_on 'Importer question clic dans image'
click_on 'Importer questions clic dans image'
end
expect(page).to have_content 'Importer une ou plusieurs questions'
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/question_glisser_deposer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

it "redirige vers le formulaire d'importation de question" do
within('.action-items-sidebar') do
click_on 'Importer question glisser déposer'
click_on 'Importer questions glisser déposer'
end
expect(page).to have_content 'Importer une ou plusieurs questions'
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/question_qcm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

it "redirige vers le formulaire d'importation de question" do
within('.action-items-sidebar') do
click_on 'Importer question qcm'
click_on 'Importer questions QCM'
end
expect(page).to have_content 'Importer une ou plusieurs questions'
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/question_saisie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

it "redirige vers le formulaire d'importation de question" do
within('.action-items-sidebar') do
click_on 'Importer question saisie'
click_on 'Importer questions saisie'
end
expect(page).to have_content 'Importer une ou plusieurs questions'
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin/question_sous_consigne_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

it "redirige vers le formulaire d'importation de question" do
within('.action-items-sidebar') do
click_on 'Importer question sous consigne'
click_on 'Importer questions sous consigne'
end
expect(page).to have_content 'Importer une ou plusieurs questions'
end
Expand Down

0 comments on commit 7d2404e

Please sign in to comment.