DEV: refactor cleaning up of user export topics

DEV: refactor cleaning up of user export topics

diff --git a/app/jobs/onceoff/clean_up_user_export_topics.rb b/app/jobs/onceoff/clean_up_user_export_topics.rb
index 34f53fa..9a5b19e 100644
--- a/app/jobs/onceoff/clean_up_user_export_topics.rb
+++ b/app/jobs/onceoff/clean_up_user_export_topics.rb
@@ -3,11 +3,9 @@
 module Jobs
   class CleanUpUserExportTopics < Jobs::Onceoff
     def execute_onceoff(args)
-      translated_keys = []
-      I18n.available_locales.each do |l|
-        translated_keys << I18n.with_locale(l.to_sym) { I18n.t("system_messages.csv_export_succeeded.subject_template") }
-      end
-      translated_keys = translated_keys.uniq
+      translated_keys = I18n.available_locales.map do |l|
+        I18n.with_locale(:"#{l}") { I18n.t("system_messages.csv_export_succeeded.subject_template") }
+      end.uniq
 
       slugs = []
       translated_keys.each do |k|
@@ -16,7 +14,7 @@ module Jobs
       # "[%{export_title}] 資料匯出已完成" gets converted to "%-topic", do not match that slug.
       slugs = slugs.reject { |s| s == "%-topic" }
 
-      topics = Topic.with_deleted.where(<<~SQL, slugs, 2.days.ago)
+      topics = Topic.with_deleted.where(<<~SQL, slugs, UserExport::DESTROY_CREATED_BEFORE)
         slug LIKE ANY(ARRAY[?]) AND
         archetype = 'private_message' AND
         subtype = 'system_message' AND
diff --git a/app/models/user_export.rb b/app/models/user_export.rb
index afb3342..e6687b1 100644
--- a/app/models/user_export.rb
+++ b/app/models/user_export.rb
@@ -7,6 +7,8 @@ class UserExport < ActiveRecord::Base
 
   around_destroy :ignore_missing_post_uploads
 
+  DESTROY_CREATED_BEFORE = 2.days.ago
+
   def ignore_missing_post_uploads
     post_ids = upload.post_uploads.pluck(:post_id)
     yield
@@ -14,7 +16,7 @@ class UserExport < ActiveRecord::Base
   end
 
   def self.remove_old_exports
-    UserExport.where('created_at < ?', 2.days.ago).find_each do |user_export|
+    UserExport.where('created_at < ?', DESTROY_CREATED_BEFORE).find_each do |user_export|
       UserExport.transaction do
         begin
           Post.where(topic_id: user_export.topic_id).find_each { |p| p.destroy! }

GitHub sha: f54a8658

1 Like