Skip to content

Commit e37cbc8

Browse files
committed
migrate ProFormA uploader from carrierwave to ActiveStorage
1 parent 5eeeaf2 commit e37cbc8

File tree

4 files changed

+20
-55
lines changed

4 files changed

+20
-55
lines changed

app/controllers/exercises_controller.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,8 @@ def import_start
161161

162162
uuid = ProformaService::UuidFromZip.call(zip: zip_file)
163163
exists, updatable = uuid_check(user: current_user, uuid:).values_at(:uuid_found, :update_right)
164-
165-
uploader = ProformaZipUploader.new
166-
uploader.cache!(params[:file])
164+
key = SecureRandom.hex
165+
ActiveStorage::Blob.create_and_upload!(key:, io: zip_file, filename: zip_file.original_filename)
167166

168167
message = if exists && updatable
169168
t('.exercise_exists_and_is_updatable')
@@ -177,7 +176,7 @@ def import_start
177176
status: 'success',
178177
message:,
179178
actions: render_to_string(partial: 'import_actions',
180-
locals: {exercise: @exercise, imported: false, exists:, updatable:, file_id: uploader.cache_name}),
179+
locals: {exercise: @exercise, imported: false, exists:, updatable:, file_id: key}),
181180
}
182181
rescue ProformaXML::InvalidZip => e
183182
render json: {
@@ -187,16 +186,16 @@ def import_start
187186
end
188187

189188
def import_confirm
190-
uploader = ProformaZipUploader.new
191-
uploader.retrieve_from_cache!(params[:file_id])
192-
exercise = ::ProformaService::Import.call(zip: uploader.file, user: current_user)
193-
exercise.save!
189+
ActiveStorage::Blob.find_by(key: params[:file_id]).open do |zip|
190+
exercise = ::ProformaService::Import.call(zip:, user: current_user)
191+
exercise.save!
194192

195-
render json: {
196-
status: 'success',
197-
message: t('.success'),
198-
actions: render_to_string(partial: 'import_actions', locals: {exercise:, imported: true}),
199-
}
193+
render json: {
194+
status: 'success',
195+
message: t('.success'),
196+
actions: render_to_string(partial: 'import_actions', locals: {exercise:, imported: true}),
197+
}
198+
end
200199
rescue ProformaXML::ProformaError, ActiveRecord::RecordInvalid => e
201200
render json: {
202201
status: 'failure',

app/uploaders/proforma_zip_uploader.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

spec/controllers/exercises_controller_spec.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -589,21 +589,21 @@
589589
describe 'POST #import_start' do
590590
let(:valid_file) { fixture_file_upload('proforma_import/testfile.zip', 'application/zip') }
591591
let(:invalid_file) { 'invalid_file' }
592-
let(:mock_uploader) { instance_double(ProformaZipUploader) }
593592
let(:uuid) { 'mocked-uuid' }
594593
let(:post_request) { post :import_start, params: {file: file} }
595594
let(:file) { valid_file }
596595

597596
before do
598597
allow(controller).to receive(:current_user).and_return(user)
599-
allow(ProformaZipUploader).to receive(:new).and_return(mock_uploader)
600598
end
601599

602600
context 'when the file is valid' do
603601
before do
604602
allow(ProformaService::UuidFromZip).to receive(:call).and_return(uuid)
605-
allow(mock_uploader).to receive(:cache!)
606-
allow(mock_uploader).to receive(:cache_name).and_return('mocked-cache-name')
603+
end
604+
605+
it 'saves the file to the server' do
606+
expect { post_request }.to change(ActiveStorage::Blob, :count).by(1)
607607
end
608608

609609
context 'when the exercise exists and is updatable' do
@@ -658,6 +658,10 @@
658658
context 'when the file is invalid' do
659659
let(:file) { invalid_file }
660660

661+
it 'does not save the file to the server' do
662+
expect { post_request }.not_to change(ActiveStorage::Blob, :count)
663+
end
664+
661665
it 'renders failure JSON with correct error' do
662666
post_request
663667

@@ -685,16 +689,10 @@
685689

686690
describe 'POST #import_confirm' do
687691
let(:file_id) { 'file_id' }
688-
let(:mock_uploader) { instance_double(ProformaZipUploader, file: 'mocked_file') }
689692
let(:post_request) { post :import_confirm, params: {file_id: file_id} }
690693

691-
before do
692-
allow(ProformaZipUploader).to receive(:new).and_return(mock_uploader)
693-
end
694-
695694
context 'when the import is successful' do
696695
before do
697-
allow(mock_uploader).to receive(:retrieve_from_cache!).with(file_id)
698696
allow(ProformaService::Import).to receive(:call).with(zip: 'mocked_file', user: user).and_return(exercise)
699697
allow(exercise).to receive(:save!).and_return(true)
700698
end
@@ -711,7 +709,6 @@
711709

712710
context 'when ProformaError or validation error occurs' do
713711
before do
714-
allow(mock_uploader).to receive(:retrieve_from_cache!).with(file_id)
715712
allow(ProformaService::Import).to receive(:call).and_raise(ProformaXML::ProformaError, 'Proforma error')
716713
end
717714

@@ -727,7 +724,6 @@
727724

728725
context 'when StandardError occurs' do
729726
before do
730-
allow(mock_uploader).to receive(:retrieve_from_cache!).and_raise(StandardError, 'Unexpected error')
731727
allow(Sentry).to receive(:capture_exception)
732728
end
733729

spec/uploaders/proforma_zip_uploader_spec.rb

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)