2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-12-29 00:13:49 +08:00
|
|
|
class UserExport < ActiveRecord::Base
|
2018-06-05 09:41:40 +08:00
|
|
|
belongs_to :user
|
2019-04-03 16:01:19 +08:00
|
|
|
belongs_to :upload, dependent: :destroy
|
2019-05-28 19:08:41 +08:00
|
|
|
belongs_to :topic, dependent: :destroy
|
2014-12-24 17:11:41 +08:00
|
|
|
|
2022-06-09 07:24:30 +08:00
|
|
|
has_many :upload_references, as: :target, dependent: :destroy
|
|
|
|
|
|
|
|
after_save do
|
|
|
|
if saved_change_to_upload_id?
|
|
|
|
UploadReference.ensure_exist!(upload_ids: [self.upload_id], target: self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-30 12:54:45 +08:00
|
|
|
DESTROY_CREATED_BEFORE = 2.days.ago
|
|
|
|
|
2014-12-24 17:11:41 +08:00
|
|
|
def self.remove_old_exports
|
2019-05-30 12:54:45 +08:00
|
|
|
UserExport
|
|
|
|
.where("created_at < ?", DESTROY_CREATED_BEFORE)
|
|
|
|
.find_each do |user_export|
|
2019-05-28 19:08:41 +08:00
|
|
|
UserExport.transaction do
|
|
|
|
begin
|
|
|
|
Post.where(topic_id: user_export.topic_id).find_each { |p| p.destroy! }
|
|
|
|
user_export.destroy!
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.warn(
|
|
|
|
"Failed to remove user_export record with id #{user_export.id}: #{e.message}\n#{e.backtrace.join("\n")}",
|
|
|
|
)
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
2019-05-28 19:08:41 +08:00
|
|
|
end
|
|
|
|
end
|
2014-12-24 17:11:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.base_directory
|
|
|
|
File.join(
|
|
|
|
Rails.root,
|
|
|
|
"public",
|
|
|
|
"uploads",
|
|
|
|
"csv_exports",
|
|
|
|
RailsMultisite::ConnectionManagement.current_db,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
2014-12-29 00:13:49 +08:00
|
|
|
# Table name: user_exports
|
2014-12-24 17:11:41 +08:00
|
|
|
#
|
2015-02-04 13:34:25 +08:00
|
|
|
# id :integer not null, primary key
|
2019-01-12 03:29:56 +08:00
|
|
|
# file_name :string not null
|
2015-02-04 13:34:25 +08:00
|
|
|
# user_id :integer not null
|
2019-01-12 03:29:56 +08:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2018-04-19 19:30:31 +08:00
|
|
|
# upload_id :integer
|
2019-05-28 19:08:41 +08:00
|
|
|
# topic_id :integer
|
2014-12-24 17:11:41 +08:00
|
|
|
#
|