universal_table/lib/tasks/universal_table_tasks.rake

27 lines
998 B
Ruby
Raw Normal View History

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()
2024-07-21 07:22:24 +00:00
host_url = Site.first.root_url
if host_url == "http://"
host_url = "http://#{args.url}"
end
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: host_url}
2017-04-13 10:50:32 +00:00
dirname = "public/uploads/utable_export/#{id}"
FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
2017-04-13 10:50:32 +00:00
f = "#{dirname}/#{table.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'')}.xlsx"
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