active_admin_csv_import 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +3 -0
- data/Rakefile +40 -0
- data/app/assets/javascripts/active_admin_csv_import/import_csv.js +98 -0
- data/app/views/admin/csv/import_csv.html.erb +17 -0
- data/lib/active_admin_csv_import.rb +8 -0
- data/lib/active_admin_csv_import/dsl.rb +20 -0
- data/lib/active_admin_csv_import/engine.rb +4 -0
- data/lib/active_admin_csv_import/railtie.rb +6 -0
- data/lib/active_admin_csv_import/version.rb +3 -0
- metadata +78 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'ActiveAdminCsvImport'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
|
24
|
+
load 'rails/tasks/engine.rake'
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
Bundler::GemHelper.install_tasks
|
29
|
+
|
30
|
+
require 'rake/testtask'
|
31
|
+
|
32
|
+
Rake::TestTask.new(:test) do |t|
|
33
|
+
t.libs << 'lib'
|
34
|
+
t.libs << 'test'
|
35
|
+
t.pattern = 'test/**/*_test.rb'
|
36
|
+
t.verbose = false
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
task :default => :test
|
@@ -0,0 +1,98 @@
|
|
1
|
+
//= require backbone/json2
|
2
|
+
//= require backbone/underscore
|
3
|
+
//= require backbone/backbone
|
4
|
+
//= require recline/backend.csv.js
|
5
|
+
//= require recline/backend.memory.js
|
6
|
+
//= require recline/model
|
7
|
+
//= require underscore.string.min.js
|
8
|
+
//= require_self
|
9
|
+
|
10
|
+
// Mix in underscore.string.js methods into underscore.js
|
11
|
+
_.mixin(_.str.exports());
|
12
|
+
|
13
|
+
|
14
|
+
$(document).ready(function() {
|
15
|
+
// the file input
|
16
|
+
var $file = $('#csv-file-input')[0];
|
17
|
+
|
18
|
+
var clearFileInput = function() {
|
19
|
+
// Reset input so .change will be triggered if we load the same file again.
|
20
|
+
$($file).wrap('<form>').closest('form').get(0).reset();
|
21
|
+
$($file).unwrap();
|
22
|
+
};
|
23
|
+
|
24
|
+
// listen for the file to be submitted
|
25
|
+
$($file).change(function(e) {
|
26
|
+
|
27
|
+
// create the dataset in the usual way but specifying file attribute
|
28
|
+
var dataset = new recline.Model.Dataset({
|
29
|
+
file: $file.files[0],
|
30
|
+
backend: 'csv'
|
31
|
+
});
|
32
|
+
|
33
|
+
dataset.fetch().done(function(data) {
|
34
|
+
|
35
|
+
if (!data.recordCount) {
|
36
|
+
alert("No records found. Please save as 'Windows Comma Separated' from Excel (2nd CSV option).");
|
37
|
+
clearFileInput();
|
38
|
+
return;
|
39
|
+
}
|
40
|
+
|
41
|
+
|
42
|
+
// Check whether the CSV's columns match up with our data model.
|
43
|
+
// import_csv_fields is passed in from Rails in import_csv.html.erb
|
44
|
+
var wanted_columns = import_csv_fields;
|
45
|
+
var csv_columns = _.pluck(data.records.first().fields.models, "id");
|
46
|
+
var normalised_csv_columns = _.map(csv_columns, function(name) {
|
47
|
+
return _.underscored(name);
|
48
|
+
});
|
49
|
+
|
50
|
+
// Check we have all the columns we want.
|
51
|
+
var missing_columns = _.difference(wanted_columns, normalised_csv_columns);
|
52
|
+
var missing_columns_humanized = _.map(missing_columns, function(name) {
|
53
|
+
return _.humanize(name);
|
54
|
+
});
|
55
|
+
|
56
|
+
if (missing_columns.length > 0) {
|
57
|
+
alert("The following columns are missing: " + _.toSentence(missing_columns_humanized) + ". Please check your column names.");
|
58
|
+
} else {
|
59
|
+
// Import!
|
60
|
+
|
61
|
+
var progress = $("#csv-import-progress");
|
62
|
+
var total = data.recordCount;
|
63
|
+
var loaded = 0;
|
64
|
+
var succeeded = 0;
|
65
|
+
|
66
|
+
_.each(data.records.models, function(record) {
|
67
|
+
|
68
|
+
// Filter only the attributes we want, and normalise column names.
|
69
|
+
var record_data = {};
|
70
|
+
record_data[import_csv_resource_name] = {};
|
71
|
+
|
72
|
+
_.each(_.pairs(record.attributes), function(attr) {
|
73
|
+
var underscored_name = _.underscored(attr[0]);
|
74
|
+
if (_.contains(wanted_columns, underscored_name)) {
|
75
|
+
record_data[import_csv_resource_name][underscored_name] = attr[1];
|
76
|
+
}
|
77
|
+
});
|
78
|
+
|
79
|
+
$.post(
|
80
|
+
import_csv_path,
|
81
|
+
record_data,
|
82
|
+
function(data) {
|
83
|
+
succeeded = succeeded + 1;
|
84
|
+
}).done(function() {
|
85
|
+
loaded = loaded + 1;
|
86
|
+
progress.text("Progress " + toString(Math.round((total / loaded))) + "%");
|
87
|
+
|
88
|
+
if (loaded == total) {
|
89
|
+
progress.text("Done. Imported " + total + " records, " + succeeded + " succeeded.");
|
90
|
+
}
|
91
|
+
});
|
92
|
+
});
|
93
|
+
}
|
94
|
+
|
95
|
+
clearFileInput();
|
96
|
+
});
|
97
|
+
});
|
98
|
+
});
|
@@ -0,0 +1,17 @@
|
|
1
|
+
<%= javascript_include_tag "active_admin_csv_import/import_csv" %>
|
2
|
+
|
3
|
+
<h3>Import <%= active_admin_config.resource_name.pluralize.humanize %> from a CSV File<h3>
|
4
|
+
<ul>
|
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>
|
7
|
+
<li>If a record already exists a duplicate will be created.</li>
|
8
|
+
</ul>
|
9
|
+
<input id="csv-file-input" type="file">
|
10
|
+
|
11
|
+
<div id="csv-import-progress"></div>
|
12
|
+
|
13
|
+
<script type="text/javascript">
|
14
|
+
var import_csv_fields = <%= @fields.to_json.html_safe %>;
|
15
|
+
var import_csv_path = <%= collection_path.to_json.html_safe %>;
|
16
|
+
var import_csv_resource_name = <%= active_admin_config.resource_name.underscore.to_json.html_safe %>;
|
17
|
+
</script>
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ActiveAdminCsvImport
|
2
|
+
module DSL
|
3
|
+
|
4
|
+
def csv_importable
|
5
|
+
action_item :only => :index do
|
6
|
+
link_to "Import #{active_admin_config.resource_name.to_s.pluralize}", :action => 'import_csv'
|
7
|
+
end
|
8
|
+
|
9
|
+
collection_action :import_csv do
|
10
|
+
@fields = active_admin_config.resource_class.columns.map(&:name) - ["id", "updated_at", "created_at"]
|
11
|
+
render "admin/csv/import_csv"
|
12
|
+
end
|
13
|
+
|
14
|
+
collection_action :create_or_update_batch, :method => :post do
|
15
|
+
# Create or update from posted data.
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
class Railtie < ::Rails::Railtie
|
2
|
+
initializer "active_admin_csv_import.setup_vendor", :after => "active_admin_csv_import.setup", :group => :all do |app|
|
3
|
+
vendor_path = File.expand_path("../../vendor/assets", __FILE__)
|
4
|
+
app.config.assets.paths.push(vendor_path.to_s)
|
5
|
+
end
|
6
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_admin_csv_import
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tomas Spacek
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-08-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70172754812400 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.14
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70172754812400
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sqlite3
|
27
|
+
requirement: &70172754805200 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70172754805200
|
36
|
+
description: CSV import for Active Admin capable of handling CSV files too large to
|
37
|
+
import via direct file upload to Heroku
|
38
|
+
email:
|
39
|
+
- ts@papercloud.com.au
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- app/assets/javascripts/active_admin_csv_import/import_csv.js
|
45
|
+
- app/views/admin/csv/import_csv.html.erb
|
46
|
+
- lib/active_admin_csv_import/dsl.rb
|
47
|
+
- lib/active_admin_csv_import/engine.rb
|
48
|
+
- lib/active_admin_csv_import/railtie.rb
|
49
|
+
- lib/active_admin_csv_import/version.rb
|
50
|
+
- lib/active_admin_csv_import.rb
|
51
|
+
- MIT-LICENSE
|
52
|
+
- Rakefile
|
53
|
+
- README.rdoc
|
54
|
+
homepage: http://www.papercloud.com.au
|
55
|
+
licenses: []
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
requirements: []
|
73
|
+
rubyforge_project:
|
74
|
+
rubygems_version: 1.8.15
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Add CSV import to Active Admin
|
78
|
+
test_files: []
|