From 8960fccca9ad81b6593cccabdec4278c710360f9 Mon Sep 17 00:00:00 2001 From: Karl Wilbur Date: Sat, 23 Mar 2013 23:09:14 -0400 Subject: [PATCH 1/3] Added targert model check for 'import_from_hash' method. --- app/models/csv_db.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/csv_db.rb b/app/models/csv_db.rb index 988c4e4..d085f6a 100644 --- a/app/models/csv_db.rb +++ b/app/models/csv_db.rb @@ -5,7 +5,11 @@ def convert_save(model_name, csv_data) csv_file = csv_data.read CSV.parse(csv_file, :headers => true, header_converters: :symbol ) do |row| target_model = model_name.classify.constantize - target_model.create!(row.to_hash) + if( target_model.respond_to? 'import_from_hash' ) + target_model.import_from_hash( row.to_hash ) + else + target_model.create!(row.to_hash) + end end end end From be9dcc923f371331f533ad3e053d07f18f510d36 Mon Sep 17 00:00:00 2001 From: Karl Wilbur Date: Fri, 4 Sep 2015 23:42:31 -0400 Subject: [PATCH 2/3] Allow for CSV.parse options to be passed into to active_admin_importable. --- app/models/csv_db.rb | 5 +++-- lib/active_admin_importable/dsl.rb | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/models/csv_db.rb b/app/models/csv_db.rb index f15b4d1..4c2fee7 100644 --- a/app/models/csv_db.rb +++ b/app/models/csv_db.rb @@ -1,9 +1,10 @@ require 'csv' class CsvDb class << self - def convert_save(target_model, csv_data, &block) + def convert_save(target_model, csv_data, options = {}, &block) + options = {:headers => true, header_converters: :symbol}.merge(options) csv_file = csv_data.read - CSV.parse(csv_file, :headers => true, header_converters: :symbol ) do |row| + CSV.parse(csv_file, options) do |row| data = row.to_hash if data.present? if (block_given?) diff --git a/lib/active_admin_importable/dsl.rb b/lib/active_admin_importable/dsl.rb index ea79938..d35de54 100644 --- a/lib/active_admin_importable/dsl.rb +++ b/lib/active_admin_importable/dsl.rb @@ -1,6 +1,6 @@ module ActiveAdminImportable module DSL - def active_admin_importable(&block) + def active_admin_importable(opts = {}, &block) action_item :only => :index do link_to "Import #{active_admin_config.resource_name.to_s.pluralize}", :action => 'upload_csv' end @@ -10,7 +10,7 @@ def active_admin_importable(&block) end collection_action :import_csv, :method => :post do - CsvDb.convert_save(active_admin_config.resource_class, params[:dump][:file], &block) + CsvDb.convert_save(active_admin_config.resource_class, params[:dump][:file], opts, &block) redirect_to :action => :index, :notice => "#{active_admin_config.resource_name.to_s} imported successfully!" end end From 6f0d935e745f3ec9b6bca160ed72f7453551073f Mon Sep 17 00:00:00 2001 From: Karl Wilbur Date: Fri, 4 Sep 2015 23:52:46 -0400 Subject: [PATCH 3/3] Updated README with example. Bumped version number (1.2.0). --- README.md | 14 ++++++++++++++ lib/active_admin_importable/version.rb | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 97d4cf3..72e466d 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,20 @@ ActiveAdmin.register Product do end ``` +## Parsing Options + +ActiveAdminImportable uses [CSV.parse](http://ruby-doc.org/stdlib-2.2.2/libdoc/csv/rdoc/CSV.html#method-c-parse) to parse your uploaded CSV. You can pass in any standard options via the optional param to active_admin_importable: + +``` +ActiveAdmin.register Product do + active_admin_importable( + col_sep: ':', + quote_char: "'", + header_converters: ->(h) { h.underscore.to_sym } + ) +end +``` + ## Contributing 1. Fork it diff --git a/lib/active_admin_importable/version.rb b/lib/active_admin_importable/version.rb index 1b77c53..40927ff 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