Merge pull request #533 from ZmagoD/zd_SCI_1045_v2

sets file duration to 7 days
This commit is contained in:
Zmago Devetak 2017-04-04 13:25:14 +02:00 committed by GitHub
commit 0aafab5eb2
3 changed files with 12 additions and 6 deletions

View file

@ -51,15 +51,17 @@ class ZipExport < ActiveRecord::Base
private
def fill_content(dir, data, type, options = {})
generate_csv(dir, data, options) if type == :csv
generate_papertrail_csv(dir, data, options) if type == :papertrail
end
def generate_csv(tmp_dir, data, options = {})
def generate_papertrail_csv(tmp_dir, data, options = {})
attributes = options.fetch(:attributes) { :attributes_missing }
file = FileUtils.touch("#{tmp_dir}/export.csv").first
records = PaperTrail::Version.where(data)
.order(created_at: :desc)
CSV.open(file, 'wb') do |csv|
csv << attributes
data.each do |entity|
records.find_each do |entity|
csv << entity.audit_record.values_at(*attributes.map(&:to_sym))
end
end

View file

@ -223,6 +223,8 @@ class Constants
href src width height alt cite datetime title class name xml:lang abbr style
).freeze
EXPORTABLE_ZIP_EXPIRATION_DAYS = 7
# Very basic regex to check for validity of emails
BASIC_EMAIL_REGEX = URI::MailTo::EMAIL_REGEXP

View file

@ -1,7 +1,9 @@
namespace :exportable_items do
desc 'Removes exportable zip files if older than 3 days'
desc 'Removes exportable zip files'
task cleanup: :environment do
ZipExport.where('created_at < ?', 3.days.ago).destroy_all
puts "All exportable zip files older than '#{3.days.ago}' have been removed"
num = Constants::EXPORTABLE_ZIP_EXPIRATION_DAYS
ZipExport.where('created_at < ?', num.days.ago).destroy_all
puts "All exportable zip files older than " \
"'#{num.days.ago}' have been removed"
end
end