active_admin_csv_import 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -19,11 +19,17 @@ $(document).ready(function() {
|
|
19
19
|
// Reset input so .change will be triggered if we load the same file again.
|
20
20
|
$($file).wrap('<form>').closest('form').get(0).reset();
|
21
21
|
$($file).unwrap();
|
22
|
+
|
23
|
+
var progress = $("#csv-import-progress");
|
24
|
+
progress.text("");
|
22
25
|
};
|
23
26
|
|
24
27
|
// listen for the file to be submitted
|
25
28
|
$($file).change(function(e) {
|
26
29
|
|
30
|
+
var progress = $("#csv-import-progress");
|
31
|
+
progress.text("Loading...");
|
32
|
+
|
27
33
|
// create the dataset in the usual way but specifying file attribute
|
28
34
|
var dataset = new recline.Model.Dataset({
|
29
35
|
file: $file.files[0],
|
@@ -38,6 +44,8 @@ $(document).ready(function() {
|
|
38
44
|
return;
|
39
45
|
}
|
40
46
|
|
47
|
+
// Re-query to get all records. Otherwise recline.js defaults to just 100.
|
48
|
+
dataset.query({size: dataset.recordCount});
|
41
49
|
|
42
50
|
// Check whether the CSV's columns match up with our data model.
|
43
51
|
// import_csv_fields is passed in from Rails in import_csv.html.erb
|
@@ -58,37 +66,49 @@ $(document).ready(function() {
|
|
58
66
|
} else {
|
59
67
|
// Import!
|
60
68
|
|
61
|
-
var progress = $("#csv-import-progress");
|
62
69
|
var total = data.recordCount;
|
63
70
|
var loaded = 0;
|
64
71
|
var succeeded = 0;
|
72
|
+
var i = 0;
|
65
73
|
|
66
74
|
_.each(data.records.models, function(record) {
|
67
75
|
|
68
|
-
//
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
var
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
76
|
+
// Add a gap between each post to give the server
|
77
|
+
// room to breathe
|
78
|
+
setTimeout(function () {
|
79
|
+
|
80
|
+
// Filter only the attributes we want, and normalise column names.
|
81
|
+
var record_data = {};
|
82
|
+
record_data[import_csv_resource_name] = {};
|
83
|
+
|
84
|
+
_.each(_.pairs(record.attributes), function(attr) {
|
85
|
+
var underscored_name = _.underscored(attr[0]);
|
86
|
+
if (_.contains(wanted_columns, underscored_name)) {
|
87
|
+
record_data[import_csv_resource_name][underscored_name] = attr[1];
|
88
|
+
}
|
89
|
+
});
|
90
|
+
|
91
|
+
$.post(
|
92
|
+
import_csv_path,
|
93
|
+
record_data,
|
94
|
+
function(data) {
|
95
|
+
succeeded = succeeded + 1;
|
96
|
+
}).always(function() {
|
97
|
+
loaded = loaded + 1;
|
98
|
+
progress.text("Progress: " + loaded + " of " + total);
|
99
|
+
|
100
|
+
if (loaded == total) {
|
101
|
+
progress.html("Done. Imported " + total + " records, " + succeeded + " succeeded.");
|
102
|
+
if (redirect_path) {
|
103
|
+
progress.html(progress.text() + " <a href='"+redirect_path +"'>Click to continue.</a>");
|
104
|
+
}
|
105
|
+
}
|
106
|
+
});
|
107
|
+
|
108
|
+
}, 100 * i);
|
109
|
+
|
110
|
+
i++;
|
111
|
+
|
92
112
|
});
|
93
113
|
}
|
94
114
|
|
@@ -3,7 +3,7 @@
|
|
3
3
|
<h3>Import <%= active_admin_config.resource_name.pluralize.humanize %> from a CSV File<h3>
|
4
4
|
<ul>
|
5
5
|
<li>Save a CSV as 'Windows Comma Separated' from Excel.</li>
|
6
|
-
<li>Your CSV should have the following column headings: <%= @fields.map(&:humanize).to_sentence %>. The order doesn't matter.</li>
|
6
|
+
<li>Your CSV should have the following column headings: <%= @fields.map(&:to_s).map(&:humanize).to_sentence %>. The order doesn't matter.</li>
|
7
7
|
<li>If a record already exists a duplicate will be created.</li>
|
8
8
|
</ul>
|
9
9
|
<input id="csv-file-input" type="file">
|
@@ -12,6 +12,7 @@
|
|
12
12
|
|
13
13
|
<script type="text/javascript">
|
14
14
|
var import_csv_fields = <%= @fields.to_json.html_safe %>;
|
15
|
-
var import_csv_path = <%=
|
15
|
+
var import_csv_path = <%= @post_path.to_json.html_safe %>;
|
16
16
|
var import_csv_resource_name = <%= active_admin_config.resource_name.underscore.to_json.html_safe %>;
|
17
|
+
var redirect_path = <%= @redirect_path.to_json.html_safe %>;
|
17
18
|
</script>
|
@@ -1,19 +1,23 @@
|
|
1
1
|
module ActiveAdminCsvImport
|
2
2
|
module DSL
|
3
3
|
|
4
|
-
def csv_importable
|
4
|
+
def csv_importable(options={})
|
5
5
|
action_item :only => :index do
|
6
6
|
link_to "Import #{active_admin_config.resource_name.to_s.pluralize}", :action => 'import_csv'
|
7
7
|
end
|
8
8
|
|
9
9
|
collection_action :import_csv do
|
10
|
-
@fields
|
10
|
+
@fields = options[:columns] ||= active_admin_config.resource_class.columns.map(&:name) - ["id", "updated_at", "created_at"]
|
11
|
+
|
12
|
+
@post_path = options[:path].try(:call)
|
13
|
+
@post_path ||= collection_path
|
14
|
+
|
15
|
+
@redirect_path = options[:redirect_path].try(:call)
|
16
|
+
@redirect_path ||= collection_path
|
17
|
+
|
11
18
|
render "admin/csv/import_csv"
|
12
19
|
end
|
13
20
|
|
14
|
-
collection_action :create_or_update_batch, :method => :post do
|
15
|
-
# Create or update from posted data.
|
16
|
-
end
|
17
21
|
end
|
18
22
|
|
19
23
|
end
|
@@ -2,5 +2,7 @@ class Railtie < ::Rails::Railtie
|
|
2
2
|
initializer "active_admin_csv_import.setup_vendor", :after => "active_admin_csv_import.setup", :group => :all do |app|
|
3
3
|
vendor_path = File.expand_path("../../vendor/assets", __FILE__)
|
4
4
|
app.config.assets.paths.push(vendor_path.to_s)
|
5
|
+
|
6
|
+
app.config.assets.precompile += %w( active_admin_csv_import/import_csv.js )
|
5
7
|
end
|
6
8
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_admin_csv_import
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2013-08-16 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70194897983060 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70194897983060
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: railties
|
27
|
-
requirement: &
|
27
|
+
requirement: &70194897981980 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '3.1'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70194897981980
|
36
36
|
description: CSV import for Active Admin capable of handling CSV files too large to
|
37
37
|
import via direct file upload to Heroku
|
38
38
|
email:
|