From 8bbac0e3ab40837b4049ff66b1cc028c24edc452 Mon Sep 17 00:00:00 2001 From: zmagod Date: Fri, 31 Mar 2017 17:49:36 +0200 Subject: [PATCH 1/3] sets file duration to 7 days --- lib/tasks/exportable_items.rake | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/tasks/exportable_items.rake b/lib/tasks/exportable_items.rake index 2e307450f..96ea3c060 100644 --- a/lib/tasks/exportable_items.rake +++ b/lib/tasks/exportable_items.rake @@ -1,7 +1,7 @@ namespace :exportable_items do - desc 'Removes exportable zip files if older than 3 days' + desc 'Removes exportable zip files if older than 7 days' 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" + ZipExport.where('created_at < ?', 7.days.ago).destroy_all + puts "All exportable zip files older than '#{7.days.ago}' have been removed" end end From e1ee53eeb5d8f9d5d0e45c4a0cd1d5d2c39c957b Mon Sep 17 00:00:00 2001 From: zmagod Date: Mon, 3 Apr 2017 13:37:54 +0200 Subject: [PATCH 2/3] generates query in background worker --- app/models/zip_export.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/models/zip_export.rb b/app/models/zip_export.rb index f6215808f..a1acefca0 100644 --- a/app/models/zip_export.rb +++ b/app/models/zip_export.rb @@ -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 From e016bda9d229a3edd62f63b80197d5da49a5e407 Mon Sep 17 00:00:00 2001 From: zmagod Date: Tue, 4 Apr 2017 10:16:54 +0200 Subject: [PATCH 3/3] adds expired days in constant --- config/initializers/constants.rb | 2 ++ lib/tasks/exportable_items.rake | 8 +++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index 468484f47..e15ced970 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -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 diff --git a/lib/tasks/exportable_items.rake b/lib/tasks/exportable_items.rake index 96ea3c060..c1775d87e 100644 --- a/lib/tasks/exportable_items.rake +++ b/lib/tasks/exportable_items.rake @@ -1,7 +1,9 @@ namespace :exportable_items do - desc 'Removes exportable zip files if older than 7 days' + desc 'Removes exportable zip files' task cleanup: :environment do - ZipExport.where('created_at < ?', 7.days.ago).destroy_all - puts "All exportable zip files older than '#{7.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