28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/map_fields.rb', line 28
def map_fields
default_options = {
:file_field => 'file',
:params => []
}
options = default_options.merge(
self.class.read_inheritable_attribute("map_fields_options_#{params[:action]}")
)
if session[:map_fields].nil? || params[options[:file_field]]
session[:map_fields] = {}
if params[options[:file_field]].blank?
@map_fields_error = MissingFileContentsError
return
end
file_field = params[options[:file_field]]
temp_path = File.join(Dir::tmpdir, "map_fields_#{Time.now.to_i}_#{$$}")
File.open(temp_path, 'wb') do |f|
f.write file_field.read
end
session[:map_fields][:file] = temp_path
end
if params[:fields].nil?
@rows = []
parser_options = session[:parser_options] = params[:parser_options].symbolize_keys
CsvParser.foreach(temp_path, parser_options) do |row|
@rows << row
break if @rows.size == 5
end
expected_fields = self.class.read_inheritable_attribute("map_fields_fields_#{params[:action]}")
@fields = ([nil] + expected_fields).inject([]){ |o, e| o << [e, o.size]}
@parameters = []
options[:params].each do |param|
@parameters += ParamsParser.parse(params, param)
end
else
if @set
@mapped_fields = MappedFields.new(session[:map_fields][:file],
@set.fields,
@set.ignore_first_row,
@set.parser_options)
else
if session[:map_fields][:file].nil? || params[:fields].nil?
session[:map_fields] = nil
@map_fields_error = InconsistentStateError
else
@mapped_fields = MappedFields.new(session[:map_fields][:file],
params[:fields],
params[:ignore_first_row],
session[:parser_options])
end
end
end
end
|