Controller use new export model
This commit is contained in:
parent
745086cbb5
commit
60538c9c28
3 changed files with 94 additions and 13 deletions
|
@ -187,23 +187,28 @@ module Instructeurs
|
|||
end
|
||||
|
||||
def download_export
|
||||
export_format = params[:export_format]
|
||||
notice_message = "Nous générons cet export. Lorsque celui-ci sera disponible, vous recevrez une notification par email accompagnée d'un lien de téléchargement."
|
||||
if procedure.should_generate_export?(export_format)
|
||||
procedure.queue_export(current_instructeur, export_format)
|
||||
flash.notice = notice_message
|
||||
format = params[:export_format]
|
||||
groupe_instructeurs = current_instructeur
|
||||
.groupe_instructeurs
|
||||
.where(procedure: procedure)
|
||||
|
||||
export = Export.find_or_create_export(format, groupe_instructeurs)
|
||||
|
||||
if export.ready?
|
||||
redirect_to export.file.service_url
|
||||
else
|
||||
respond_to do |format|
|
||||
notice_message = "Nous générons cet export. Veuillez revenir dans quelques minutes pour le télécharger."
|
||||
format.js do
|
||||
@procedure = procedure
|
||||
assign_exports
|
||||
flash.notice = notice_message
|
||||
end
|
||||
|
||||
format.html do
|
||||
redirect_to instructeur_procedure_url(procedure), notice: notice_message
|
||||
end
|
||||
format.all { redirect_to procedure }
|
||||
end
|
||||
elsif procedure.export_queued?(export_format)
|
||||
flash.notice = notice_message
|
||||
redirect_to procedure
|
||||
else
|
||||
redirect_to url_for(procedure.export_file(export_format))
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -7,8 +7,8 @@
|
|||
- [[xlsx_export, :xlsx], [csv_export, :csv], [ods_export, :ods]].each do |(export, format)|
|
||||
%li
|
||||
- if export.nil?
|
||||
= link_to "Exporter au format .#{format}", download_export_instructeur_procedure_path(procedure, export_format: format), remote: true
|
||||
= link_to "Demander un export au format .#{format}", download_export_instructeur_procedure_path(procedure, export_format: format), remote: true
|
||||
- elsif export.ready?
|
||||
= link_to "Au format .#{format}", url_for(export.file), target: "_blank", rel: "noopener"
|
||||
= link_to "Télécharger l'export au format .#{format}", url_for(export.file), target: "_blank", rel: "noopener"
|
||||
- else
|
||||
L'export au format .#{format} est en cours de préparation
|
||||
|
|
|
@ -431,4 +431,80 @@ describe Instructeurs::ProceduresController, type: :controller do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#download_export' do
|
||||
let(:instructeur) { create(:instructeur) }
|
||||
let!(:procedure) { create(:procedure) }
|
||||
let!(:gi_0) { procedure.defaut_groupe_instructeur }
|
||||
let!(:gi_1) { GroupeInstructeur.create(label: 'gi_1', procedure: procedure, instructeurs: [instructeur]) }
|
||||
|
||||
before { sign_in(instructeur.user) }
|
||||
|
||||
subject do
|
||||
get :download_export, params: { export_format: :csv, procedure_id: procedure.id }
|
||||
end
|
||||
|
||||
context 'when the export is does not exist' do
|
||||
it 'displays an notice' do
|
||||
is_expected.to redirect_to(instructeur_procedure_url(procedure))
|
||||
expect(flash.notice).to be_present
|
||||
end
|
||||
|
||||
it { expect { subject }.to change(Export, :count).by(1) }
|
||||
end
|
||||
|
||||
context 'when the export is not ready' do
|
||||
before do
|
||||
Export.create(format: :csv, groupe_instructeurs: [gi_1])
|
||||
end
|
||||
|
||||
it 'displays an notice' do
|
||||
is_expected.to redirect_to(instructeur_procedure_url(procedure))
|
||||
expect(flash.notice).to be_present
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the export is ready' do
|
||||
let!(:export) do
|
||||
Export.create(format: :csv, groupe_instructeurs: [gi_1])
|
||||
end
|
||||
|
||||
before do
|
||||
export.file.attach(io: StringIO.new('export'), filename: 'file.csv')
|
||||
end
|
||||
|
||||
it 'displays the download link' do
|
||||
subject
|
||||
expect(response.headers['Location']).to start_with("http://test.host/rails/active_storage/disk")
|
||||
end
|
||||
end
|
||||
|
||||
context 'when another export is ready' do
|
||||
let!(:export) do
|
||||
Export.create(format: :csv, groupe_instructeurs: [gi_0, gi_1])
|
||||
end
|
||||
|
||||
before do
|
||||
export.file.attach(io: StringIO.new('export'), filename: 'file.csv')
|
||||
end
|
||||
|
||||
it 'displays an notice' do
|
||||
is_expected.to redirect_to(instructeur_procedure_url(procedure))
|
||||
expect(flash.notice).to be_present
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the js format is used' do
|
||||
before do
|
||||
post :download_export,
|
||||
params: { export_format: :csv, procedure_id: procedure.id },
|
||||
format: :js
|
||||
end
|
||||
|
||||
it "responses in the correct format" do
|
||||
expect(response.content_type).to eq "text/javascript"
|
||||
expect(response).to have_http_status(:ok)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue