DEV: should ignore missing post uploads when a user export destroyed

DEV: should ignore missing post uploads when a user export destroyed

diff --git a/app/models/user_export.rb b/app/models/user_export.rb
index 4884b84..e486fa0 100644
--- a/app/models/user_export.rb
+++ b/app/models/user_export.rb
@@ -4,6 +4,14 @@ class UserExport < ActiveRecord::Base
   belongs_to :user
   belongs_to :upload, dependent: :destroy
 
+  around_destroy :ignore_missing_post_uploads
+
+  def ignore_missing_post_uploads
+    post_ids = upload.post_uploads.pluck(:post_id)
+    yield
+    post_ids.each { |post_id| PostCustomField.create!(post_id: post_id, name: Post::MISSING_UPLOADS_IGNORED, value: "t") }
+  end
+
   def self.remove_old_exports
     UserExport.where('created_at < ?', 2.days.ago).find_each do |user_export|
       user_export.destroy!
diff --git a/spec/models/user_export_spec.rb b/spec/models/user_export_spec.rb
index cadc75d..30bc80e 100644
--- a/spec/models/user_export_spec.rb
+++ b/spec/models/user_export_spec.rb
@@ -33,4 +33,22 @@ RSpec.describe UserExport do
       expect(Upload.exists?(id: csv_file_2.id)).to eq(true)
     end
   end
+
+  describe '#destroy!' do
+    it 'should create post custom field for ignored missing uploads' do
+      upload = Fabricate(:upload, created_at: 3.days.ago)
+      export = UserExport.create!(
+        file_name: "test",
+        user: user,
+        upload_id: upload.id,
+        created_at: 3.days.ago
+      )
+      post = Fabricate(:post, raw: "![#{upload.original_filename}](#{upload.short_url})")
+      post.link_post_uploads
+
+      export.destroy!
+
+      expect(PostCustomField.exists?(post_id: post.id, name: Post::MISSING_UPLOADS_IGNORED)).to eq(true)
+    end
+  end
 end

GitHub sha: 793915fe

2 Likes

REVERT: DEV: should ignore missing post uploads when a user export destroyed