Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

copy file to Rails tmp-Folder before mapping

  • Loading branch information...
commit ba04965374d0ec29edf29c048fcb53e31db0a8c5 1 parent edf658b
@masche842 authored
View
8 README
@@ -20,5 +20,13 @@ routes.rb
post 'import', :on => :collection
end
+Include it in your myresource-controller:
+
+ require 'csv_mapper'
+
+ include CsvMapper
+
+
+== Usage
call /myresources/import!
View
2  app/views/controller_actions/mapper.html.erb
@@ -3,7 +3,7 @@
<p>Bitte Felder mappen</p>
<%= form_tag nil, :id => 'map_fields_form', :method => :post do -%>
- <%= hidden_field_tag :file_path, @mapper.file_path %>
+ <%= hidden_field_tag :filename, @mapper.filename %>
<div class="map_fields">
<table cellspacing="0">
<thead>
View
1  lib/csv_mapper.rb
@@ -3,6 +3,7 @@
require 'csv_mapper/engine'
require 'csv_mapper/importer'
require 'csv_mapper/reader'
+require 'csv_mapper/file_handler'
module CsvMapper
View
8 lib/csv_mapper/controller_actions.rb
@@ -23,10 +23,11 @@ def import
resource_name = self.class.name.gsub(/Controller/, '').singularize
resource_class = resource_name.constantize
if request.post?
- @mapper = CsvMapper::Importer.new(params, self.class.read_inheritable_attribute(:map_fields_options))
+ # already mapped
if params[:fields]
save_errors = []
- @mapper.data.each do |row|
+ reader = CsvMapper::Reader.new(params)
+ reader.each do |row|
resource = resource_class.new(row)
unless resource.save
save_errors.push resource.errors
@@ -34,12 +35,15 @@ def import
end
if save_errors.empty?
flash[:notice] = 'Daten erfolgreich importiert!'
+ reader.remove_file
redirect_to :action => :index
else
flash[:warning] = save_errors
render 'controller_actions/import'
end
+ #no mapping yet
else
+ @mapper = CsvMapper::Importer.new(params, self.class.read_inheritable_attribute(:map_fields_options))
render 'controller_actions/mapper'
end
else
View
35 lib/csv_mapper/file_handler.rb
@@ -0,0 +1,35 @@
+class CsvMapper::FileHandler
+
+ attr_reader :path, :filename
+
+ def initialize()
+ @path = File.join(Rails.root.to_s, "tmp")
+ end
+
+ def save_temp_file(tempfile)
+ @filename = unique_filename
+ FileUtils.copy_file(tempfile.path, file_path)
+ File.exists?(file_path)
+ end
+
+ def load_file(filename)
+ @filename = filename
+ File.exist?(file_path)
+ end
+
+ def file_path
+ File.join(@path, @filename)
+ end
+
+ def remove_file
+ File.delete(file_path)
+ end
+
+private
+
+ def unique_filename
+ t = Time.now.strftime("%Y%m%d")
+ "#{t}-#{$$}-#{rand(0x100000000000000).to_s(36)}"
+ end
+
+end
View
19 lib/csv_mapper/importer.rb
@@ -1,27 +1,18 @@
require 'fastercsv'
class CsvMapper::Importer
- attr_accessor :map_fields, :raw_data, :file_path
+ attr_accessor :map_fields, :raw_data, :filename
def initialize(params, options)
@params = params
- temp_file = params[options[:file_field]]
- @file_path = params[:file_path] || ( temp_file.path unless temp_file.nil? )
-
- if @file_path
- @raw_data = FasterCSV.open(@file_path, CsvMapper.options)
+ file_handler = CsvMapper::FileHandler.new()
+ if file_handler.save_temp_file(params[options[:file_field]])
+ @filename = file_handler.filename
+ @raw_data = FasterCSV.open(file_handler.file_path, CsvMapper.options)
@map_fields = options[:mapping]
else
raise CsvMapper::MissingFileContentsError
end
end
-
- def data
- CsvMapper::Reader.new(
- @file_path,
- @params[:fields],
- @params[:ignore_first_row]
- )
- end
end
View
19 lib/csv_mapper/reader.rb
@@ -2,11 +2,14 @@
class CsvMapper::Reader
- def initialize(file, mapping, ignore_first_row)
- @file = file
- @ignore_first_row = ignore_first_row
+ def initialize(params)
+ @file_handler = CsvMapper::FileHandler.new
+ @file_handler.load_file(params[:filename])
+
+ @file_path = @file_handler.file_path
+ @ignore_first_row = params[:ignore_first_row]
@mapping = {}
- mapping.each do |k, v|
+ params[:fields].each do |k, v|
unless v.empty?
@mapping[v.downcase.to_sym] = k.to_i - 1
end
@@ -15,7 +18,7 @@ def initialize(file, mapping, ignore_first_row)
def each
row_number = 1
- FasterCSV.foreach(@file, CsvMapper.options) do |csv_row|
+ FasterCSV.foreach(@file_path, CsvMapper.options) do |csv_row|
unless row_number == 1 && @ignore_first_row
row = {}
@mapping.each do |k, v|
@@ -27,5 +30,9 @@ def each
row_number += 1
end
end
-
+
+ def remove_file
+ @file_handler.remove_file
+ end
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.