Generate table.csv

This commit is contained in:
Jure Grabnar 2018-08-29 00:31:11 +02:00
parent 01d48cf679
commit 93bd461a83
2 changed files with 13 additions and 2 deletions

View file

@ -128,4 +128,15 @@ class Table < ApplicationRecord
Table.connection.execute(sql)
end
end
def to_csv
require 'csv'
data = JSON.parse(contents)['data']
CSV.generate do |csv|
data.each do |row|
csv << row
end
end
end
end

View file

@ -223,7 +223,7 @@ class ZipExport < ApplicationRecord
file = FileUtils.touch(
"#{directory}/#{handle_name(table_name)}_#{i}_Step#{step_asset.step.position+1}}.csv"
).first
File.open(file, 'wb') { |f| f.write(table.contents) }
File.open(file, 'wb') { |f| f.write(table.to_csv) }
end
end
@ -233,7 +233,7 @@ class ZipExport < ApplicationRecord
table_name = table.name.presence || 'Table'
table_name += i.to_s
file = FileUtils.touch("#{directory}/#{table_name}.csv").first
File.open(file, 'wb') { |f| f.write(table.contents) }
File.open(file, 'wb') { |f| f.write(table.to_csv) }
end
end