property_hire/lib/tasks/property_hire_tasks.rake

28 lines
1.0 KiB
Ruby

# desc "Explaining what the task does"
# task :property_hire do
# # Task goes here
# end
namespace :property_hire_tasks do
task :prepare_download,[:property_id, :url] => :environment do |task,args|
id = args.property_id
I18n.locale = :zh_tw
property = Property.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: "property_hire_export/export", locals: {property: property, site_in_use_locales: Site.first.in_use_locales, url: host_url}
dirname = "public/uploads/reservation_export/#{id}"
FileUtils.mkdir_p(dirname) unless File.exist?(dirname)
f = "#{dirname}/#{property.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