diff --git a/app/models/table.rb b/app/models/table.rb index 02dd8c138..c48526123 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -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 diff --git a/app/models/zip_export.rb b/app/models/zip_export.rb index 3bd4199b2..a47c3e013 100644 --- a/app/models/zip_export.rb +++ b/app/models/zip_export.rb @@ -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