discourse/app/models/user_export.rb
Guo Xiang Tan d600e71b3d FIX: Clean up stale UserExport records daily.
* Add tests for `UserExport.remove_old_exports`
2018-06-05 09:43:40 +08:00

31 lines
843 B
Ruby

class UserExport < ActiveRecord::Base
belongs_to :user
def self.remove_old_exports
UserExport.where('created_at < ?', 2.days.ago).find_each do |user_export|
file_name = "#{user_export.file_name}-#{user_export.id}.csv.gz"
file_path = "#{UserExport.base_directory}/#{file_name}"
File.delete(file_path) if File.exist?(file_path)
user_export.destroy!
end
end
def self.base_directory
File.join(Rails.root, "public", "uploads", "csv_exports", RailsMultisite::ConnectionManagement.current_db)
end
end
# == Schema Information
#
# Table name: user_exports
#
# id :integer not null, primary key
# file_name :string not null
# user_id :integer not null
# created_at :datetime not null
# updated_at :datetime not null
# upload_id :integer
#