# desc "Explaining what the task does" # task :universal_table do # # Task goes here # end 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() 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} dirname = "public/uploads/utable_export/#{id}" FileUtils.mkdir_p(dirname) unless File.exist?(dirname) f = "#{dirname}/#{table.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'')}.xlsx" if File.exist?(f) File.delete(f) end file = File.open(f, "w") xlsx.force_encoding("utf-8") file.write(xlsx) end end