From ca91c3d16f9c4d812bf5befd21de58e62e5a60a4 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 31 Dec 2015 00:07:43 +0100 Subject: [PATCH] Create csv from csv content --- back-office/app/controllers/items_controller.rb | 12 ++++++++++++ back-office/app/helpers/items_helper.rb | 14 +++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/back-office/app/controllers/items_controller.rb b/back-office/app/controllers/items_controller.rb index 7e1e3d8..8332681 100644 --- a/back-office/app/controllers/items_controller.rb +++ b/back-office/app/controllers/items_controller.rb @@ -43,6 +43,18 @@ class ItemsController < ApplicationController ["Import items", "import_items"], ["Update prices", "update_prices"] ] + @csv_content = params[:csv_content] + + @error_message = "" + @error_message = "Format of CSV is wrong (CSV content is empty)" if params[:csv_content] == "" + + csv_file = ItemsHelper::create_csv(CSV.parse(params[:csv_content])) + begin + + ensure + csv_file.unlink + end + render :template => "items/export_import" end end diff --git a/back-office/app/helpers/items_helper.rb b/back-office/app/helpers/items_helper.rb index f2092c6..a330049 100644 --- a/back-office/app/helpers/items_helper.rb +++ b/back-office/app/helpers/items_helper.rb @@ -1,5 +1,13 @@ module ItemsHelper + def self.create_csv(data) + file = Tempfile.new([Rails.root.join('tmp/').to_s, ".csv"], "") + csv = CSV.new(file) + data.each do |row| + csv << row + end - - -end \ No newline at end of file + file.rewind + file.close + file + end +end