diff --git a/app/controllers/export_csv_controller.rb b/app/controllers/export_csv_controller.rb index b108405408e..0db074d1f48 100644 --- a/app/controllers/export_csv_controller.rb +++ b/app/controllers/export_csv_controller.rb @@ -19,8 +19,8 @@ class ExportCsvController < ApplicationController filename = params.fetch(:id) export_id = filename.split('_')[1].split('.')[0] export_initiated_by_user_id = 0 - export_initiated_by_user_id = CsvExportLog.where(id: export_id)[0].user_id unless CsvExportLog.where(id: export_id).empty? - export_csv_path = CsvExportLog.get_download_path(filename) + export_initiated_by_user_id = UserExport.where(id: export_id)[0].user_id unless UserExport.where(id: export_id).empty? + export_csv_path = UserExport.get_download_path(filename) if export_csv_path && export_initiated_by_user_id == current_user.id send_file export_csv_path diff --git a/app/jobs/regular/export_csv_file.rb b/app/jobs/regular/export_csv_file.rb index c0daa07bddc..4f80a7bd6e1 100644 --- a/app/jobs/regular/export_csv_file.rb +++ b/app/jobs/regular/export_csv_file.rb @@ -239,17 +239,17 @@ module Jobs def set_file_path - @file = CsvExportLog.create(export_type: @entity_type, user_id: @current_user.id) + @file = UserExport.create(export_type: @entity_type, user_id: @current_user.id) @file_name = "export_#{@file.id}.csv" - + # ensure directory exists - dir = File.dirname("#{CsvExportLog.base_directory}/#{@file_name}") + dir = File.dirname("#{UserExport.base_directory}/#{@file_name}") FileUtils.mkdir_p(dir) unless Dir.exists?(dir) end def write_csv_file(data, header) # write to CSV file - CSV.open(File.expand_path("#{CsvExportLog.base_directory}/#{@file_name}", __FILE__), "w") do |csv| + CSV.open(File.expand_path("#{UserExport.base_directory}/#{@file_name}", __FILE__), "w") do |csv| csv << header data.each do |value| csv << value @@ -259,7 +259,7 @@ module Jobs def notify_user if @current_user - if @file_name != "" && File.exists?("#{CsvExportLog.base_directory}/#{@file_name}") + if @file_name != "" && File.exists?("#{UserExport.base_directory}/#{@file_name}") SystemMessage.create_from_system_user(@current_user, :csv_export_succeeded, download_link: "#{Discourse.base_url}/export_csv/#{@file_name}", file_name: @file_name) else SystemMessage.create_from_system_user(@current_user, :csv_export_failed) diff --git a/app/jobs/scheduled/clean_up_exports.rb b/app/jobs/scheduled/clean_up_exports.rb index e61a96bd065..b439d7f71c7 100644 --- a/app/jobs/scheduled/clean_up_exports.rb +++ b/app/jobs/scheduled/clean_up_exports.rb @@ -3,7 +3,7 @@ module Jobs every 2.day def execute(args) - CsvExportLog.remove_old_exports # delete exported CSV files older than 2 days + UserExport.remove_old_exports # delete exported CSV files older than 2 days end end end diff --git a/app/models/csv_export_log.rb b/app/models/user_export.rb similarity index 68% rename from app/models/csv_export_log.rb rename to app/models/user_export.rb index c16dfe02219..9b1e9aadd8d 100644 --- a/app/models/csv_export_log.rb +++ b/app/models/user_export.rb @@ -1,7 +1,7 @@ -class CsvExportLog < ActiveRecord::Base +class UserExport < ActiveRecord::Base def self.get_download_path(filename) - path = File.join(CsvExportLog.base_directory, filename) + path = File.join(UserExport.base_directory, filename) if File.exists?(path) return path else @@ -10,15 +10,15 @@ class CsvExportLog < ActiveRecord::Base end def self.remove_old_exports - expired_exports = CsvExportLog.where('created_at < ?', 2.days.ago).to_a + expired_exports = UserExport.where('created_at < ?', 2.days.ago).to_a expired_exports.map do |expired_export| file_name = "export_#{expired_export.id}.csv" - file_path = "#{CsvExportLog.base_directory}/#{file_name}" - + file_path = "#{UserExport.base_directory}/#{file_name}" + if File.exist?(file_path) File.delete(file_path) end - CsvExportLog.find(expired_export.id).destroy + UserExport.find(expired_export.id).destroy end end @@ -30,7 +30,7 @@ end # == Schema Information # -# Table name: csv_export_logs +# Table name: user_exports # # id :integer not null, primary key # export_type :string(255) not null diff --git a/db/migrate/20141228151019_rename_csv_export_logs_to_user_exports.rb b/db/migrate/20141228151019_rename_csv_export_logs_to_user_exports.rb new file mode 100644 index 00000000000..12d15bb5e1b --- /dev/null +++ b/db/migrate/20141228151019_rename_csv_export_logs_to_user_exports.rb @@ -0,0 +1,5 @@ +class RenameCsvExportLogsToUserExports < ActiveRecord::Migration + def change + rename_table :csv_export_logs, :user_exports + end +end diff --git a/spec/controllers/export_csv_controller_spec.rb b/spec/controllers/export_csv_controller_spec.rb index 4de76e21c92..12b70046e38 100644 --- a/spec/controllers/export_csv_controller_spec.rb +++ b/spec/controllers/export_csv_controller_spec.rb @@ -22,11 +22,11 @@ describe ExportCsvController do describe ".download" do it "uses send_file to transmit the export file" do - file = CsvExportLog.create(export_type: "user", user_id: @user.id) + file = UserExport.create(export_type: "user", user_id: @user.id) file_name = "export_#{file.id}.csv" controller.stubs(:render) - export = CsvExportLog.new() - CsvExportLog.expects(:get_download_path).with(file_name).returns(export) + export = UserExport.new() + UserExport.expects(:get_download_path).with(file_name).returns(export) subject.expects(:send_file).with(export) get :show, id: file_name response.should be_success @@ -38,7 +38,7 @@ describe ExportCsvController do end it "returns 404 when the export file does not exist" do - CsvExportLog.expects(:get_download_path).returns(nil) + UserExport.expects(:get_download_path).returns(nil) get :show, id: export_filename response.should be_not_found end @@ -59,18 +59,18 @@ describe ExportCsvController do describe ".download" do it "uses send_file to transmit the export file" do - file = CsvExportLog.create(export_type: "admin", user_id: @admin.id) + file = UserExport.create(export_type: "admin", user_id: @admin.id) file_name = "export_#{file.id}.csv" controller.stubs(:render) - export = CsvExportLog.new() - CsvExportLog.expects(:get_download_path).with(file_name).returns(export) + export = UserExport.new() + UserExport.expects(:get_download_path).with(file_name).returns(export) subject.expects(:send_file).with(export) get :show, id: file_name response.should be_success end it "returns 404 when the export file does not exist" do - CsvExportLog.expects(:get_download_path).returns(nil) + UserExport.expects(:get_download_path).returns(nil) get :show, id: export_filename response.should be_not_found end