rails_admin_import 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rails_admin_import/import.rb +66 -61
  2. metadata +1 -1
@@ -54,77 +54,82 @@ module RailsAdminImport
54
54
  end
55
55
 
56
56
  def run_import(params)
57
- if !params.has_key?(:file)
58
- return results = { :success => [], :error => ["You must select a file."] }
59
- end
60
-
61
- if RailsAdminImport.config.logging
62
- FileUtils.copy(params[:file].tempfile, "#{Rails.root}/log/import/#{Time.now.strftime("%Y-%m-%d-%H-%M-%S")}-import.csv")
63
- logger = Logger.new("#{Rails.root}/log/import/import.log")
64
- end
65
-
66
- text = File.read(params[:file].tempfile)
67
- clean = text.gsub(/\n$/, '')
68
- file_check = CSV.new(clean)
57
+ begin
58
+ if !params.has_key?(:file)
59
+ return results = { :success => [], :error => ["You must select a file."] }
60
+ end
69
61
 
70
- if file_check.readlines.size > RailsAdminImport.config.line_item_limit
71
- return results = { :success => [], :error => ["Please limit upload file to #{RailsAdminImport.config.line_item_limit} line items."] }
72
- end
62
+ if RailsAdminImport.config.logging
63
+ FileUtils.copy(params[:file].tempfile, "#{Rails.root}/log/import/#{Time.now.strftime("%Y-%m-%d-%H-%M-%S")}-import.csv")
64
+ logger = Logger.new("#{Rails.root}/log/import/import.log")
65
+ end
66
+
67
+ text = File.read(params[:file].tempfile)
68
+ clean = text.gsub(/\n$/, '')
69
+ file_check = CSV.new(clean)
73
70
 
74
- map = {}
71
+ if file_check.readlines.size > RailsAdminImport.config.line_item_limit
72
+ return results = { :success => [], :error => ["Please limit upload file to #{RailsAdminImport.config.line_item_limit} line items."] }
73
+ end
75
74
 
76
- file = CSV.new(clean)
77
- file.readline.each_with_index do |key, i|
78
- if self.many_fields.include?(key.to_sym)
79
- map[key.to_sym] ||= []
80
- map[key.to_sym] << i
81
- else
82
- map[key.to_sym] = i
75
+ map = {}
76
+
77
+ file = CSV.new(clean)
78
+ file.readline.each_with_index do |key, i|
79
+ if self.many_fields.include?(key.to_sym)
80
+ map[key.to_sym] ||= []
81
+ map[key.to_sym] << i
82
+ else
83
+ map[key.to_sym] = i
84
+ end
83
85
  end
84
- end
85
-
86
- update = params.has_key?(:update_if_exists) && params[:update_if_exists] ? params[:update_lookup].to_sym : nil
87
-
88
- if update && !map.has_key?(params[:update_lookup].to_sym)
89
- return results = { :success => [], :error => ["Your file must contain a column for the 'Update lookup field' you selected."] }
90
- end
86
+
87
+ update = params.has_key?(:update_if_exists) && params[:update_if_exists] ? params[:update_lookup].to_sym : nil
91
88
 
92
- results = { :success => [], :error => [] }
89
+ if update && !map.has_key?(params[:update_lookup].to_sym)
90
+ return results = { :success => [], :error => ["Your file must contain a column for the 'Update lookup field' you selected."] }
91
+ end
92
+
93
+ results = { :success => [], :error => [] }
94
+
95
+ associated_map = {}
96
+ self.belongs_to_fields.flatten.each do |field|
97
+ associated_map[field] = field.to_s.classify.constantize.all.inject({}) { |hash, c| hash[c.send(params[field]).to_s] = c.id; hash }
98
+ end
99
+ self.many_fields.flatten.each do |field|
100
+ associated_map[field] = field.to_s.classify.constantize.all.inject({}) { |hash, c| hash[c.send(params[field]).to_s] = c; hash }
101
+ end
102
+
103
+ label_method = RailsAdminImport.config(self).label
93
104
 
94
- associated_map = {}
95
- self.belongs_to_fields.flatten.each do |field|
96
- associated_map[field] = field.to_s.classify.constantize.all.inject({}) { |hash, c| hash[c.send(params[field]).to_s] = c.id; hash }
97
- end
98
- self.many_fields.flatten.each do |field|
99
- associated_map[field] = field.to_s.classify.constantize.all.inject({}) { |hash, c| hash[c.send(params[field]).to_s] = c; hash }
100
- end
101
-
102
- label_method = RailsAdminImport.config(self).label
103
-
104
- file.each do |row|
105
- object = self.import_initialize(row, map, update)
106
- object.import_belongs_to_data(associated_map, row, map)
107
- object.import_many_data(associated_map, row, map)
108
- object.before_import_save(row, map)
109
-
110
- object.import_files(row, map)
111
-
112
- verb = object.new_record? ? "Create" : "Update"
113
- if object.errors.empty?
114
- if object.save
115
- logger.info "#{Time.now.to_s}: #{verb}d: #{object.send(label_method)}" if RailsAdminImport.config.logging
116
- results[:success] << "#{verb}d: #{object.send(label_method)}"
105
+ file.each do |row|
106
+ object = self.import_initialize(row, map, update)
107
+ object.import_belongs_to_data(associated_map, row, map)
108
+ object.import_many_data(associated_map, row, map)
109
+ object.before_import_save(row, map)
110
+
111
+ object.import_files(row, map)
112
+
113
+ verb = object.new_record? ? "Create" : "Update"
114
+ if object.errors.empty?
115
+ if object.save
116
+ logger.info "#{Time.now.to_s}: #{verb}d: #{object.send(label_method)}" if RailsAdminImport.config.logging
117
+ results[:success] << "#{verb}d: #{object.send(label_method)}"
118
+ else
119
+ logger.info "#{Time.now.to_s}: Failed to #{verb}: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}." if RailsAdminImport.config.logging
120
+ results[:error] << "Failed to #{verb}: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
121
+ end
117
122
  else
118
- logger.info "#{Time.now.to_s}: Failed to #{verb}: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}." if RailsAdminImport.config.logging
119
- results[:error] << "Failed to #{verb}: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
123
+ logger.info "#{Time.now.to_s}: Errors before save: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}." if RailsAdminImport.config.logging
124
+ results[:error] << "Errors before save: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
120
125
  end
121
- else
122
- logger.info "#{Time.now.to_s}: Errors before save: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}." if RailsAdminImport.config.logging
123
- results[:error] << "Errors before save: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
124
126
  end
127
+
128
+ results
129
+ rescue Exception => e
130
+ logger.info "#{Time.now.to_s}: Unknown exception in import: #{e.inspect}"
131
+ return results = { :success => [], :error => ["Could not upload. Unexpected error: #{e.to_s}"] }
125
132
  end
126
-
127
- results
128
133
  end
129
134
 
130
135
  def import_initialize(row, map, update)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_admin_import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: