2015-11-13 13:10:00 +00:00
|
|
|
# desc "Explaining what the task does"
|
|
|
|
# task :universal_table do
|
|
|
|
# # Task goes here
|
|
|
|
# end
|
2017-04-13 10:50:32 +00:00
|
|
|
namespace :universal_table_tasks do
|
|
|
|
task :prepare_download,[:utable_id, :url] => :environment do |task,args|
|
|
|
|
id = args.utable_id
|
|
|
|
I18n.locale = :zh_tw
|
|
|
|
table = UTable.find(id)
|
|
|
|
ac = ActionController::Base.new()
|
|
|
|
url = "http://#{args.url}"
|
|
|
|
xlsx = ac.render_to_string handlers: [:axlsx], formats: [:xlsx], template: "utable_export/export", locals: {table: table, site_in_use_locales: Site.first.in_use_locales, url: url}
|
|
|
|
dirname = "public/uploads/utable_export/#{id}"
|
2023-04-08 14:42:56 +00:00
|
|
|
FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
|
2017-04-13 10:50:32 +00:00
|
|
|
f = "#{dirname}/#{table.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'')}.xlsx"
|
2023-04-08 14:42:56 +00:00
|
|
|
if File.exist?(f)
|
2017-04-13 10:50:32 +00:00
|
|
|
File.delete(f)
|
|
|
|
end
|
|
|
|
file = File.open(f, "w")
|
|
|
|
xlsx.force_encoding("utf-8")
|
|
|
|
file.write(xlsx)
|
|
|
|
end
|
|
|
|
end
|