Skip to content
Browse files

Adjustments on import behavior: do file import only on new records, a…

…dd update functionality.
  • Loading branch information...
1 parent 2225303 commit 8c5abe7fe953f5b329dae125c4a08402a4986c6f Steph Skardal committed Mar 1, 2012
Showing with 35 additions and 11 deletions.
  1. +15 −4 app/views/rails_admin/main/import.html.erb
  2. +20 −7 lib/rails_admin_import/import.rb
View
19 app/views/rails_admin/main/import.html.erb
@@ -72,20 +72,31 @@
<small>Additional application specific fields.</small>
</td>
<% end -%>
-
-
</tr>
</table>
<%= form_tag rails_admin.import_url(@abstract_model.to_param), :multipart => true do |f| -%>
<%= file_field_tag :file %>
+
+ <p>
+ <%= check_box_tag :update_if_exists %> Update if Exists<br />
+ Update lookup field
+ <select name="update_lookup">
+ <% @abstract_model.model.new.attributes.keys.each do |key| -%>
+ <option value="<%= key %>"><%= key %></option>
+ <% end -%>
+ </select>
+ </p>
+
<% [@abstract_model.model.belongs_to_fields, @abstract_model.model.many_fields].flatten.each do |field| -%>
+ <div style="display:inline-block; width: 45%;background:#cecece;margin: 5px;padding: 5px;">
<label style="width:200px;"><%= field %> mapping</label>&nbsp;&nbsp;
<select name="<%= field %>">
<% field.to_s.classify.constantize.new.attributes.keys.each do |key| -%>
<option value="<%= key %>"><%= key %></option>
<% end -%>
- <select><br /><br />
- <% end -%>
+ <select>
+ </div>
+ <% end -%><br />
<%= submit_tag "Upload", :disable_with => "Uploading..." %>
<% end -%>
View
27 lib/rails_admin_import/import.rb
@@ -80,20 +80,23 @@ def run_import(params)
end
label_method = RailsAdminImport.config(self).label
+
+ update = params.has_key?(:update_if_exists) && params[:update_if_exists] ? params[:update_lookup].to_sym : nil
file.each do |row|
- object = self.import_new(row, map)
+ object = self.import_initialize(row, map, update)
object.import_belongs_to_data(associated_map, row, map)
object.import_many_data(associated_map, row, map)
object.before_import_save(row, map)
object.import_files(row, map)
+ verb = object.new_record? ? "Create" : "Update"
if object.errors.empty?
if object.save
- results[:success] << "Created: #{object.send(label_method)}"
+ results[:success] << "#{verb}d: #{object.send(label_method)}"
else
- results[:error] << "Failed to create: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
+ results[:error] << "Failed to #{verb}: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
end
else
results[:error] << "Errors before save: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
@@ -103,12 +106,22 @@ def run_import(params)
results
end
- def import_new(row, map)
+ def import_initialize(row, map, update)
new_attrs = {}
self.import_fields.each do |key|
new_attrs[key] = row[map[key]] if map[key]
end
- self.new(new_attrs)
+
+ item = nil
+ if update.present?
+ item = self.send("find_by_#{update}", row[map[update]])
+ end
+
+ if item.nil?
+ item = self.new(new_attrs)
+ end
+
+ item
end
end
@@ -122,7 +135,7 @@ def import_display
end
def import_files(row, map)
- if self.valid?
+ if self.new_record? && self.valid?
self.class.file_fields.each do |key|
if map[key] && !row[map[key]].nil?
begin
@@ -136,7 +149,7 @@ def import_files(row, map)
end
end
end
- end
+ end
end
def import_belongs_to_data(associated_map, row, map)

0 comments on commit 8c5abe7

Please sign in to comment.
Something went wrong with that request. Please try again.