Rename CsvExportLog to UserExport

This commit is contained in:
Arpit Jalan 2014-12-28 21:43:49 +05:30
parent 8d03ff6f82
commit 68e66f3a25
6 changed files with 28 additions and 23 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,5 @@
class RenameCsvExportLogsToUserExports < ActiveRecord::Migration
def change
rename_table :csv_export_logs, :user_exports
end
end

View File

@ -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