From 44e1915d4f0e20cf445196ccc7e1d279c75ef0ce Mon Sep 17 00:00:00 2001 From: Oswaldo Ferreira Date: Tue, 4 Jun 2019 20:50:57 -0300 Subject: Persist tmp snippet uploads It persist temporary personal snippets under user/:id namespaces temporarily while creating a upload record to track it. If an user gets removed while it's still a tmp upload, it also gets removed. If the tmp upload is sent, the upload gets moved to personal_snippets/:id as before. The upload record also gets updated to the new model type as well. --- app/uploaders/file_mover.rb | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'app/uploaders/file_mover.rb') diff --git a/app/uploaders/file_mover.rb b/app/uploaders/file_mover.rb index 236b7ed2b3d..dcf1e8792ad 100644 --- a/app/uploaders/file_mover.rb +++ b/app/uploaders/file_mover.rb @@ -1,12 +1,13 @@ # frozen_string_literal: true class FileMover - attr_reader :secret, :file_name, :model, :update_field + attr_reader :secret, :file_name, :from_model, :to_model, :update_field - def initialize(file_path, model, update_field = :description) + def initialize(file_path, update_field = :description, from_model:, to_model:) @secret = File.split(File.dirname(file_path)).last @file_name = File.basename(file_path) - @model = model + @from_model = from_model + @to_model = to_model @update_field = update_field end @@ -16,7 +17,7 @@ class FileMover move if update_markdown - uploader.record_upload + update_upload_model uploader.schedule_background_upload end end @@ -35,14 +36,20 @@ class FileMover end def update_markdown - updated_text = model.read_attribute(update_field) - .gsub(temp_file_uploader.markdown_link, uploader.markdown_link) - model.update_attribute(update_field, updated_text) + updated_text = to_model.read_attribute(update_field) + .gsub(temp_file_uploader.markdown_link, uploader.markdown_link) + to_model.update_attribute(update_field, updated_text) rescue revert false end + def update_upload_model + return unless upload = temp_file_uploader.upload + + upload.update!(model_id: to_model.id, model_type: to_model.type) + end + def temp_file_path return @temp_file_path if @temp_file_path @@ -60,15 +67,15 @@ class FileMover end def uploader - @uploader ||= PersonalFileUploader.new(model, secret: secret) + @uploader ||= PersonalFileUploader.new(to_model, secret: secret) end def temp_file_uploader - @temp_file_uploader ||= PersonalFileUploader.new(nil, secret: secret) + @temp_file_uploader ||= PersonalFileUploader.new(from_model, secret: secret) end def revert - Rails.logger.warn("Markdown not updated, file move reverted for #{model}") + Rails.logger.warn("Markdown not updated, file move reverted for #{to_model}") FileUtils.move(file_path, temp_file_path) end -- cgit v1.2.1