active_admin_importer 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +48 -14
- data/lib/active_admin_importer.rb +20 -0
- data/lib/active_admin_importer/definition.rb +63 -0
- data/lib/active_admin_importer/dsl.rb +9 -15
- data/lib/active_admin_importer/engine.rb +0 -5
- data/lib/active_admin_importer/import.rb +34 -14
- data/lib/active_admin_importer/registry.rb +11 -0
- data/lib/active_admin_importer/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63ec9935374bfd592cc9c2035ce8304660ca4e1b
|
4
|
+
data.tar.gz: 5674df2b7741e145b0b6d128397170cfb64ed11e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be21e9b80705a71d7fcdda60bf34cdf0057b9ffe06a9e45301e19f41b6fb0349f6a0a14dfbd7f05a187d1e2fb136a491723714b7be2c10fc4ea42ea34594a759
|
7
|
+
data.tar.gz: 4d08eaf02625782de3537837cf5357091835f4129064cd044c06ab099df9e2247c6be3a34e9289c91018b86f9918c8d47c31b7c2fba012c4730a5be548007157
|
data/README.md
CHANGED
@@ -12,27 +12,61 @@ Simple CSV imports for active_admin, that (shouldn't) destroy your servers memor
|
|
12
12
|
|
13
13
|
``` ruby
|
14
14
|
::ActiveAdmin.register Product do
|
15
|
-
define_import_for :products do
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
define_import_for :products do
|
16
|
+
each_row do |params, import|
|
17
|
+
product_attributes = params.slice(*[:name, :price])
|
18
|
+
product_attributes[:user_id] = import.controller.current_user.id
|
19
|
+
import.model.create!(product_attributes)
|
20
|
+
end
|
19
21
|
end
|
20
22
|
|
21
23
|
#Example: require headers to be present
|
22
|
-
define_import_for :products_on_sale
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
define_import_for :products_on_sale do
|
25
|
+
required_headers "sale_price", "price"
|
26
|
+
|
27
|
+
each_row do |params, import|
|
28
|
+
product_attributes = params.slice(*[:name, :price, :sale_price])
|
29
|
+
product_attributes[:user_id] = import.controller.current_user.id
|
30
|
+
import.model.create!(product_attributes)
|
31
|
+
end
|
26
32
|
end
|
27
33
|
|
28
34
|
# Example: using a custom view
|
29
35
|
# assume the view has a dropdown which allows you to select a particular store during import
|
30
|
-
define_import_for :products_for_store
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
+
define_import_for :products_for_store do
|
37
|
+
view "myactiveadmin/products/upload_products_for_store_csv"
|
38
|
+
|
39
|
+
each_row do |params, import|
|
40
|
+
product_attributes = params.slice(*[:name, :price, :sale_price])
|
41
|
+
product_attributes[:user_id] = import.controller.current_user.id
|
42
|
+
product_attributes[:store_id] = import.controller.params["store_id"]
|
43
|
+
import.model.create!(product_attributes)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Example: using a Trax::Core transformer class,
|
48
|
+
# or transformer that responds to to_hash
|
49
|
+
define_import_for :products_for_store do
|
50
|
+
view "myactiveadmin/products/upload_products_for_store_csv"
|
51
|
+
transformer ::ProductForStoreTransformer
|
52
|
+
|
53
|
+
each_row do |params, import|
|
54
|
+
product_attributes[:user_id] = import.controller.current_user.id
|
55
|
+
product_attributes[:store_id] = import.controller.params["store_id"]
|
56
|
+
import.model.create!(product_attributes)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Example: using transform as callback
|
61
|
+
define_import_for :products_for_store do
|
62
|
+
view "myactiveadmin/products/upload_products_for_store_csv"
|
63
|
+
transform { |row| ::ProductForStoreTransformer.new(row).to_hash }
|
64
|
+
|
65
|
+
each_row do |params, import|
|
66
|
+
product_attributes[:user_id] = import.controller.current_user.id
|
67
|
+
product_attributes[:store_id] = import.controller.params["store_id"]
|
68
|
+
import.model.create!(product_attributes)
|
69
|
+
end
|
36
70
|
end
|
37
71
|
end
|
38
72
|
```
|
@@ -1,14 +1,17 @@
|
|
1
1
|
require "active_admin_importer/version"
|
2
2
|
require "active_support/all"
|
3
3
|
require "active_admin_importer/engine"
|
4
|
+
require "pry"
|
4
5
|
|
5
6
|
module ActiveAdminImporter
|
6
7
|
extend ::ActiveSupport::Autoload
|
7
8
|
|
9
|
+
autoload :Definition
|
8
10
|
autoload :DSL
|
9
11
|
autoload :Engine
|
10
12
|
autoload :CsvFile
|
11
13
|
autoload :Import
|
14
|
+
autoload :Registry
|
12
15
|
|
13
16
|
def self.import(csv_file, **options, &block)
|
14
17
|
io = csv_file.is_a?(::ActionDispatch::Http::UploadedFile) ? csv_file.tempfile : csv_file
|
@@ -22,4 +25,21 @@ module ActiveAdminImporter
|
|
22
25
|
_import.run if _import.valid?
|
23
26
|
_import
|
24
27
|
end
|
28
|
+
|
29
|
+
def self.[](val)
|
30
|
+
registry[val]
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.registry
|
34
|
+
@registry ||= ::ActiveAdminImporter::Registry.new
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.register(definition)
|
38
|
+
@registry[definition.key] = definition
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
unless ENV["SKIP_ACTIVE_ADMIN_REQUIRE"]
|
43
|
+
require 'activeadmin' unless defined?(::ActiveAdmin)
|
44
|
+
::ActiveAdmin::DSL.send(:include, ActiveAdminImporter::DSL)
|
25
45
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module ActiveAdminImporter
|
2
|
+
class Definition
|
3
|
+
def initialize(_name=:records, _controller, &block)
|
4
|
+
@_name = _name
|
5
|
+
@_action = "import_#{@_name}"
|
6
|
+
@_form_action = "upload_#{@_name}"
|
7
|
+
@_view = "admin/csv/upload"
|
8
|
+
@_required_headers ||= []
|
9
|
+
@_controller = _controller
|
10
|
+
@_model = @_controller.resource_class
|
11
|
+
@_each_row = lambda{ |params| @_model.create!(params) }
|
12
|
+
self.instance_eval(&block) if block_given?
|
13
|
+
end
|
14
|
+
|
15
|
+
def [](val)
|
16
|
+
self.instance_variable_get(:"@_#{val}")
|
17
|
+
end
|
18
|
+
|
19
|
+
def action(val)
|
20
|
+
@_action = val
|
21
|
+
end
|
22
|
+
|
23
|
+
def after(&block)
|
24
|
+
@_after = block
|
25
|
+
end
|
26
|
+
|
27
|
+
def before(&block)
|
28
|
+
@_before = block
|
29
|
+
end
|
30
|
+
|
31
|
+
def key
|
32
|
+
"#{self[:controller].name.underscore}/#{self[:name]}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def model(val)
|
36
|
+
@_model = val
|
37
|
+
end
|
38
|
+
|
39
|
+
def each_row(&block)
|
40
|
+
@_each_row = block
|
41
|
+
end
|
42
|
+
|
43
|
+
def form_action(val)
|
44
|
+
@_form_action = val
|
45
|
+
end
|
46
|
+
|
47
|
+
def required_headers(*_values)
|
48
|
+
@_required_headers = _values
|
49
|
+
end
|
50
|
+
|
51
|
+
def transformer(val)
|
52
|
+
@_transformer = val
|
53
|
+
end
|
54
|
+
|
55
|
+
def transform(&block)
|
56
|
+
@_transform = block
|
57
|
+
end
|
58
|
+
|
59
|
+
def view(value)
|
60
|
+
@_view = value
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -1,29 +1,23 @@
|
|
1
1
|
module ActiveAdminImporter
|
2
2
|
module DSL
|
3
|
-
def define_import_for(name='records',
|
4
|
-
|
5
|
-
options[:form_action] ||= "upload_#{name}"
|
6
|
-
options[:view] ||= "admin/csv/upload"
|
7
|
-
options[:required_headers] ||= []
|
3
|
+
def define_import_for(name='records', &block)
|
4
|
+
definition = ::ActiveAdminImporter::Definition.new(name, self.controller, &block)
|
8
5
|
|
9
6
|
action_item :edit, :only => :index do
|
10
|
-
link_to
|
7
|
+
link_to definition[:action].titleize, :action => definition[:form_action]
|
11
8
|
end
|
12
9
|
|
13
|
-
collection_action(
|
14
|
-
render
|
10
|
+
collection_action(definition[:form_action]) do
|
11
|
+
render definition[:view], :locals => { :target_action => definition[:action] }
|
15
12
|
end
|
16
13
|
|
17
|
-
collection_action(
|
14
|
+
collection_action(definition[:action], :method => :post) do
|
18
15
|
parent if parent?
|
19
|
-
import = ::ActiveAdminImporter.import(params[:dump][:file],
|
20
|
-
:model => active_admin_config.resource_class,
|
21
|
-
:controller => self,
|
22
|
-
:required_headers => options[:required_headers],
|
23
|
-
&block)
|
24
|
-
|
16
|
+
import = ::ActiveAdminImporter.import(params[:dump][:file], :controller => self, :definition => definition)
|
25
17
|
redirect_to collection_path(), alert: import.result
|
26
18
|
end
|
19
|
+
|
20
|
+
::ActiveAdminImporter.register(definition)
|
27
21
|
end
|
28
22
|
end
|
29
23
|
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module ActiveAdminImporter
|
2
2
|
class Import
|
3
|
-
attr_reader :current_row, :csv_file
|
3
|
+
attr_reader :controller, :current_row, :csv_file, :definition, :model
|
4
4
|
|
5
|
-
def initialize(csv_file,
|
5
|
+
def initialize(csv_file, definition:, controller:)
|
6
6
|
@csv_file = ::ActiveAdminImporter::CsvFile.new(csv_file)
|
7
7
|
@controller = controller
|
8
|
-
@
|
9
|
-
@
|
8
|
+
@definition = definition
|
9
|
+
@model = definition[:model]
|
10
|
+
@required_headers = definition[:required_headers]
|
10
11
|
@current_row = 0
|
11
|
-
@block = block if block_given?
|
12
12
|
end
|
13
13
|
|
14
14
|
def failed_rows
|
@@ -19,7 +19,12 @@ module ActiveAdminImporter
|
|
19
19
|
@headers ||= @csv_file.headers
|
20
20
|
end
|
21
21
|
|
22
|
+
def md5
|
23
|
+
@md5 ||= @csv_file.md5
|
24
|
+
end
|
25
|
+
|
22
26
|
def run
|
27
|
+
run_before_callback if run_before_callback?
|
23
28
|
log_info("STARTING IMPORT")
|
24
29
|
|
25
30
|
::CSV.parse(@csv_file, :headers => true, :header_converters => :symbol) do |row|
|
@@ -32,6 +37,7 @@ module ActiveAdminImporter
|
|
32
37
|
|
33
38
|
log_error("FAILED TO PARSE ROWS #{failed_rows}") if failed_rows.any?
|
34
39
|
log_info("FINISHED IMPORT")
|
40
|
+
run_after_callback if run_after_callback?
|
35
41
|
end
|
36
42
|
|
37
43
|
def result
|
@@ -50,17 +56,23 @@ module ActiveAdminImporter
|
|
50
56
|
|
51
57
|
private
|
52
58
|
|
59
|
+
def log_error(message)
|
60
|
+
::Rails.logger.error("[IMPORT: #{self.md5}]: #{message}")
|
61
|
+
end
|
62
|
+
|
63
|
+
def log_info(message)
|
64
|
+
::Rails.logger.info("[IMPORT: #{self.md5}]: #{message}")
|
65
|
+
end
|
66
|
+
|
53
67
|
def process_row(row)
|
54
68
|
@current_row += 1
|
55
69
|
log_info("IMPORTING ROW - #{current_row}")
|
56
70
|
data = row.to_hash
|
57
71
|
|
58
72
|
if data.present?
|
59
|
-
if @
|
60
|
-
|
61
|
-
|
62
|
-
@model.create!(data)
|
63
|
-
end
|
73
|
+
data = @definition[:transformer].new(data).to_hash if @definition[:transformer]
|
74
|
+
data = @definition[:transform].call(data) if @definition[:transform]
|
75
|
+
@definition[:each_row].call(data, self)
|
64
76
|
end
|
65
77
|
end
|
66
78
|
|
@@ -71,12 +83,20 @@ module ActiveAdminImporter
|
|
71
83
|
::Rails.logger.error(e.message)
|
72
84
|
end
|
73
85
|
|
74
|
-
def
|
75
|
-
|
86
|
+
def run_after_callback
|
87
|
+
self.instance_eval(&@definition[:after])
|
76
88
|
end
|
77
89
|
|
78
|
-
def
|
79
|
-
|
90
|
+
def run_after_callback?
|
91
|
+
@definition[:after]
|
92
|
+
end
|
93
|
+
|
94
|
+
def run_before_callback
|
95
|
+
self.instance_eval(&@definition[:before])
|
96
|
+
end
|
97
|
+
|
98
|
+
def run_before_callback?
|
99
|
+
@definition[:before]
|
80
100
|
end
|
81
101
|
end
|
82
102
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_admin_importer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Ayre
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -143,9 +143,11 @@ files:
|
|
143
143
|
- bin/setup
|
144
144
|
- lib/active_admin_importer.rb
|
145
145
|
- lib/active_admin_importer/csv_file.rb
|
146
|
+
- lib/active_admin_importer/definition.rb
|
146
147
|
- lib/active_admin_importer/dsl.rb
|
147
148
|
- lib/active_admin_importer/engine.rb
|
148
149
|
- lib/active_admin_importer/import.rb
|
150
|
+
- lib/active_admin_importer/registry.rb
|
149
151
|
- lib/active_admin_importer/version.rb
|
150
152
|
homepage: https://github.com/jasonayre/active_admin_importer
|
151
153
|
licenses:
|