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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions app/models/csv_db.rb
Original file line number Diff line number Diff line change
@@ -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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice use of options with merge of default.

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?)
Expand Down
4 changes: 2 additions & 2 deletions lib/active_admin_importable/dsl.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
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