Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ spec/reports
test/tmp
test/version_tmp
tmp
# RubyMine project files
.idea
2 changes: 2 additions & 0 deletions active_admin_importable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Gem::Specification.new do |gem|
gem.summary = "Add CSV import to Active Admin resources with one line."
gem.homepage = "http://github.com/krhorst/active_admin_importable"

gem.add_runtime_dependency 'rchardet', '~> 1.6'

gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
Expand Down
24 changes: 16 additions & 8 deletions app/models/csv_db.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
require 'csv'
require 'rchardet'

class CsvDb
class << self
def convert_save(target_model, csv_data, &block)
def convert_save(target_model, csv_data, validator, &block)
csv_file = csv_data.read
CSV.parse(csv_file, :headers => true, header_converters: :symbol ) do |row|
encoding = CharDet.detect(csv_file)['encoding']
csv_file.encode!('UTF-8', encoding)

dataset = CSV.parse(csv_file, :headers => true, header_converters: :symbol)
validator.call(dataset) if validator.present?

dataset.each do |row|
data = row.to_hash
if data.present?
if (block_given?)
block.call(target_model, data)
else
target_model.create!(data)
end
end
block.call(target_model, data)
else
target_model.create!(data)
end
end
end
end
end
end
end
37 changes: 16 additions & 21 deletions app/views/admin/csv/upload_csv.html.erb
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
<%= form_for :dump, :url => {:action => "import_csv"}, :html => {:multipart => true} do |f| %>

<table>

<tr>
<td>
<%= label_tag "dump_file", "Select a CSV File" %>
</td>
<td>
<%= f.file_field :file %>
<%= form_for :dump, :url => {:action => 'import_csv'}, :html => {:multipart => true} do |f| %>
<table>
<tr>
<td>
<%= label_tag 'dump_file', 'Select a CSV File' %>
</td>
</tr>
<tr>
<td>
<%= submit_tag 'Submit' %>
</td>
</tr>

</table>


<% end %>
<td>
<%= f.file_field :file %>
</td>
</tr>
<tr>
<td>
<%= submit_tag 'Submit' %>
</td>
</tr>
</table>
<% end %>
23 changes: 16 additions & 7 deletions lib/active_admin_importable/dsl.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
module ActiveAdminImportable
module DSL
def active_admin_importable(&block)
action_item :only => :index do
link_to "Import #{active_admin_config.resource_name.to_s.pluralize}", :action => 'upload_csv'
def active_admin_importable(options={}, &block)
action_item :edit, :only => :index do
link_name = options[:name] || "Import #{active_admin_config.resource_name.to_s.pluralize}"
link_to link_name, :action => 'upload_csv'
end

collection_action :upload_csv do
render "admin/csv/upload_csv"
render 'admin/csv/upload_csv'
end

collection_action :import_csv, :method => :post do
CsvDb.convert_save(active_admin_config.resource_class, params[:dump][:file], &block)
redirect_to :action => :index, :notice => "#{active_admin_config.resource_name.to_s} imported successfully!"
begin
ActiveRecord::Base.transaction do
CsvDb.convert_save(active_admin_config.resource_class, params[:dump][:file], options[:validator], &block)
end
flash[:notice] = "#{active_admin_config.resource_name.to_s.pluralize} imported successfully!"
rescue StandardError => e
flash[:alert] = "Error: #{e.message}"
end

redirect_to :action => :index
end
end
end
end
end
4 changes: 1 addition & 3 deletions lib/active_admin_importable/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

module ActiveAdminImportable
class Engine < Rails::Engine

config.mount_at = '/'

end
end
end
2 changes: 1 addition & 1 deletion lib/active_admin_importable/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ActiveAdminImportable
VERSION = "1.1.2"
VERSION = '1.2.0'
end