active_admin_importable 1.0.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +17 -1
- data/app/models/csv_db.rb +7 -3
- data/lib/active_admin_importable/dsl.rb +2 -2
- data/lib/active_admin_importable/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -6,7 +6,7 @@ CSV imports for Active Admin with one line of code.
|
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
gem 'active_admin_importable'
|
9
|
+
gem 'active_admin_importable'
|
10
10
|
|
11
11
|
And then execute:
|
12
12
|
|
@@ -20,10 +20,26 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
Add the following line into your active admin resource:
|
22
22
|
|
23
|
+
|
23
24
|
active_admin_importable
|
24
25
|
|
25
26
|
The Import button should now appear. Click it and upload a CSV file with a header row corresponding to your model attributes. Press submit. Profit.
|
26
27
|
|
28
|
+
## Custom Import Behavior
|
29
|
+
|
30
|
+
Need to do something special with the import? active_admin_importable accepts an optional block that will be called on each row, replacing the default functionality ( calling create! on the associated model). The associated model and a hash of the current row will get passed into the block. For example:
|
31
|
+
|
32
|
+
```
|
33
|
+
ActiveAdmin.register Product do
|
34
|
+
active_admin_importable do |model, hash|
|
35
|
+
store = Store.find_by_name(hash[:store_name])
|
36
|
+
hash[:store_id] = store.id
|
37
|
+
hash.delete(:store_name)
|
38
|
+
model.create!(hash)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
27
43
|
## Contributing
|
28
44
|
|
29
45
|
1. Fork it
|
data/app/models/csv_db.rb
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
require 'csv'
|
2
2
|
class CsvDb
|
3
3
|
class << self
|
4
|
-
def convert_save(model_name, csv_data)
|
4
|
+
def convert_save(model_name, csv_data, &block)
|
5
5
|
csv_file = csv_data.read
|
6
|
+
target_model = model_name.pluralize.classify.constantize
|
6
7
|
CSV.parse(csv_file, :headers => true, header_converters: :symbol ) do |row|
|
7
|
-
|
8
|
-
|
8
|
+
if(block_given?)
|
9
|
+
block.call(target_model, row.to_hash)
|
10
|
+
else
|
11
|
+
target_model.create!(row.to_hash)
|
12
|
+
end
|
9
13
|
end
|
10
14
|
end
|
11
15
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module ActiveAdminImportable
|
2
2
|
module DSL
|
3
|
-
def active_admin_importable
|
3
|
+
def active_admin_importable(&block)
|
4
4
|
action_item :only => :index do
|
5
5
|
link_to "Import #{active_admin_config.resource_name.to_s.pluralize}", :action => 'upload_csv'
|
6
6
|
end
|
@@ -10,7 +10,7 @@ module ActiveAdminImportable
|
|
10
10
|
end
|
11
11
|
|
12
12
|
collection_action :import_csv, :method => :post do
|
13
|
-
CsvDb.convert_save(active_admin_config.resource_name.to_s, params[:dump][:file])
|
13
|
+
CsvDb.convert_save(active_admin_config.resource_name.to_s, params[:dump][:file], &block)
|
14
14
|
redirect_to :action => :index, :notice => "#{active_admin_config.resource_name.to_s} imported successfully!"
|
15
15
|
end
|
16
16
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_admin_importable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-04-17 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: CSV import for Active Admin
|
15
15
|
email:
|