2019-05-03 08:17:27 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-12-28 21:43:49 +05:30
|
|
|
class UserExport < ActiveRecord::Base
|
2018-06-05 09:41:40 +08:00
|
|
|
belongs_to :user
|
2019-04-03 13:31:19 +05:30
|
|
|
belongs_to :upload, dependent: :destroy
|
2019-05-28 16:38:41 +05:30
|
|
|
belongs_to :topic, dependent: :destroy
|
2014-12-24 14:41:41 +05:30
|
|
|
|
2019-05-30 10:24:45 +05:30
|
|
|
DESTROY_CREATED_BEFORE = 2.days.ago
|
|
|
|
|
2014-12-24 14:41:41 +05:30
|
|
|
def self.remove_old_exports
|
2019-05-30 10:24:45 +05:30
|
|
|
UserExport.where('created_at < ?', DESTROY_CREATED_BEFORE).find_each do |user_export|
|
2019-05-28 16:38:41 +05:30
|
|
|
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")}")
|
|
|
|
end
|
|
|
|
end
|
2014-12-24 14:41:41 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.base_directory
|
|
|
|
File.join(Rails.root, "public", "uploads", "csv_exports", RailsMultisite::ConnectionManagement.current_db)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
2014-12-28 21:43:49 +05:30
|
|
|
# Table name: user_exports
|
2014-12-24 14:41:41 +05:30
|
|
|
#
|
2015-02-04 16:34:25 +11:00
|
|
|
# id :integer not null, primary key
|
2019-01-11 14:29:56 -05:00
|
|
|
# file_name :string not null
|
2015-02-04 16:34:25 +11:00
|
|
|
# user_id :integer not null
|
2019-01-11 14:29:56 -05:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2018-04-19 17:00:31 +05:30
|
|
|
# upload_id :integer
|
2019-05-28 16:38:41 +05:30
|
|
|
# topic_id :integer
|
2014-12-24 14:41:41 +05:30
|
|
|
#
|