diff --git a/.gitignore b/.gitignore index d87d4be..4d24529 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,5 @@ spec/reports test/tmp test/version_tmp tmp +# RubyMine project files +.idea \ No newline at end of file diff --git a/active_admin_importable.gemspec b/active_admin_importable.gemspec index 3b8e2ca..382e409 100644 --- a/active_admin_importable.gemspec +++ b/active_admin_importable.gemspec @@ -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)/}) diff --git a/app/models/csv_db.rb b/app/models/csv_db.rb index f15b4d1..3a88ec4 100644 --- a/app/models/csv_db.rb +++ b/app/models/csv_db.rb @@ -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 \ No newline at end of file +end diff --git a/app/views/admin/csv/upload_csv.html.erb b/app/views/admin/csv/upload_csv.html.erb index 97a9659..80259b5 100644 --- a/app/views/admin/csv/upload_csv.html.erb +++ b/app/views/admin/csv/upload_csv.html.erb @@ -1,22 +1,17 @@ -<%= form_for :dump, :url => {:action => "import_csv"}, :html => {:multipart => true} do |f| %> - - - - - - + + + + +
- <%= label_tag "dump_file", "Select a CSV File" %> - - <%= f.file_field :file %> +<%= form_for :dump, :url => {:action => 'import_csv'}, :html => {:multipart => true} do |f| %> + + + - - - - - -
+ <%= label_tag 'dump_file', 'Select a CSV File' %>
- <%= submit_tag 'Submit' %> -
- - -<% end %> \ No newline at end of file +
+ <%= f.file_field :file %> +
+ <%= submit_tag 'Submit' %> +
+<% end %> diff --git a/lib/active_admin_importable/dsl.rb b/lib/active_admin_importable/dsl.rb index ea79938..45b6e6d 100644 --- a/lib/active_admin_importable/dsl.rb +++ b/lib/active_admin_importable/dsl.rb @@ -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 \ No newline at end of file +end diff --git a/lib/active_admin_importable/engine.rb b/lib/active_admin_importable/engine.rb index 05f9246..24a8dd5 100644 --- a/lib/active_admin_importable/engine.rb +++ b/lib/active_admin_importable/engine.rb @@ -3,8 +3,6 @@ module ActiveAdminImportable class Engine < Rails::Engine - config.mount_at = '/' - end -end \ No newline at end of file +end diff --git a/lib/active_admin_importable/version.rb b/lib/active_admin_importable/version.rb index 1b77c53..3d1c8dc 100644 --- a/lib/active_admin_importable/version.rb +++ b/lib/active_admin_importable/version.rb @@ -1,3 +1,3 @@ module ActiveAdminImportable - VERSION = "1.1.2" + VERSION = '1.2.0' end