active_admin_import 1.0.0

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.
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ *.rbc
2
+ *.sassc
3
+ .sass-cache
4
+ capybara-*.html
5
+ .rspec
6
+ /.bundle
7
+ /vendor/bundle
8
+ /log/*
9
+ /tmp/*
10
+ /db/*.sqlite3
11
+ /public/system/*
12
+ /coverage/
13
+ /spec/tmp/*
14
+ **.orig
15
+ rerun.txt
16
+ pickle-email-*.html
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_admin_importable.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Igor Fedoronchuk
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # ActiveAdminImport
2
+ The most fastest and efficient CSV import for Active Admin (based on activerecord-import gem)
3
+ with support of validations and bulk inserts
4
+
5
+
6
+ #Links
7
+ https://github.com/gregbell/active_admin
8
+
9
+ https://github.com/zdennis/activerecord-import
10
+
11
+
12
+
13
+ #Options
14
+
15
+ # +back+:: resource action to redirect after processing
16
+ # +col_sep+:: column separator used for CSV parsing
17
+ # +validate+:: rue|false, means perfoem validations or not
18
+ # +batch_size+:: integer value of max record count inserted by 1 query/transaction
19
+ # +before_import+:: proc for before import action, called with resource, file, options arguments
20
+ # +before_batch_import+:: proc for before each batch action, called with imported data and headers arguments
21
+ # +after_batch_import+:: proc for after each batch action, called with imported data and headers arguments
22
+ # +on_duplicate_key_update+:: an Array or Hash, tells activerecord-import to use MySQL's ON DUPLICATE KEY UPDATE ability.
23
+ # +timestamps+:: true|false, tells activerecord-import to not add timestamps (if false) even if record timestamps is disabled in ActiveRecord::Base
24
+ # +ignore+:: true|false, tells activerecord-import toto use MySQL's INSERT IGNORE ability
25
+
26
+
27
+ #Example
28
+
29
+ ActiveAdmin.register Post do
30
+ active_admin_import :validate => false,
31
+ :col_sep => ',',
32
+ :back => :index ,
33
+ :before_import => proc{|data| Post.delete_all},
34
+ :batch_size => 1000
35
+
36
+
37
+ end
38
+
39
+
40
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/active_admin_import/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Igor Fedoronchuk"]
6
+ gem.email = ["fedoronchuk@gmail.com"]
7
+ gem.description = "CSV import for Active Admin"
8
+ gem.summary = "ActiveAdmin import based on activerecord-import gem."
9
+ gem.homepage = "http://github.com/Fivell/active_admin_import"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "active_admin_import"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = ActiveAdminImport::VERSION
17
+
18
+ gem.add_runtime_dependency('chronic')
19
+ gem.add_runtime_dependency('activerecord-import','0.3.0')
20
+ end
@@ -0,0 +1,7 @@
1
+ <%= form_for :import, :url => {:action => :do_import}, :html => {:multipart => true} do |f| %>
2
+ <%= label_tag "import_file", "File for import:" %><br>
3
+ <%= f.file_field :file %>
4
+ <hr />
5
+ <%= submit_tag 'Import' %>
6
+ <% end %>
7
+
@@ -0,0 +1,7 @@
1
+ require 'activerecord-import'
2
+ require 'active_admin_import/version'
3
+ require 'active_admin_import/engine'
4
+ require 'active_admin_import/dsl'
5
+ require 'active_admin_import/importer'
6
+
7
+ ::ActiveAdmin::DSL.send(:include, ActiveAdminImport::DSL)
@@ -0,0 +1,55 @@
1
+ module ActiveAdminImport
2
+ module DSL
3
+
4
+
5
+ # Declares import functionality
6
+ #
7
+ # Options
8
+ # +back+:: resource action to redirect after processing
9
+ # +col_sep+:: column separator used for CSV parsing
10
+ # +validate+:: rue|false, means perfoem validations or not
11
+ # +batch_size+:: integer value of max record count inserted by 1 query/transaction
12
+ # +before_import+:: proc for before import action, called with resource, file, options arguments
13
+ # +before_batch_import+:: proc for before each batch action, called with imported data and headers arguments
14
+ # +after_batch_import+:: proc for after each batch action, called with imported data and headers arguments
15
+ # +on_duplicate_key_update+:: an Array or Hash, tells activerecord-import to use MySQL's ON DUPLICATE KEY UPDATE ability.
16
+ # +timestamps+:: true|false, tells activerecord-import to not add timestamps (if false) even if record timestamps is disabled in ActiveRecord::Base
17
+ # +ignore+:: true|false, tells activerecord-import toto use MySQL's INSERT IGNORE ability
18
+ def active_admin_import options = {}
19
+ default_options = {
20
+ :back => :import,
21
+ :col_sep => ','
22
+ }
23
+ options = default_options.merge(options)
24
+ action_item :only => :index do
25
+ link_to "Import #{active_admin_config.resource_name.pluralize}", :action => 'import'
26
+ end
27
+
28
+ collection_action :import, :method => :get do
29
+ render "admin/import"
30
+ end
31
+
32
+ collection_action :do_import, :method => :post do
33
+ if params[:import].blank?
34
+ flash[:alert] = "Please, select file to import"
35
+ return redirect_to :action => options[:back]
36
+ end
37
+ unless params[:import]['file'].try(:content_type) && params[:import]['file'].content_type.in?(["text/csv"])
38
+ flash[:alert] = "You can import file only with extension csv"
39
+ return redirect_to :action => options[:back]
40
+ end
41
+ importer = Importer.new( active_admin_config.resource_class, params[:import][:file], options)
42
+
43
+
44
+ result = importer.import
45
+ flash[:notice] = "#{view_context.pluralize(result[:imported].to_i,active_admin_config.resource_name)} was imported"
46
+ unless result[:failed].count == 0
47
+ flash[:error] = "#{view_context.pluralize(result[:failed].count,active_admin_config.resource_name)} was failed to imported"
48
+ end
49
+
50
+ redirect_to :action => options[:back]
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,9 @@
1
+ require 'rails'
2
+
3
+ module ActiveAdminImport
4
+ class Engine < Rails::Engine
5
+
6
+ config.mount_at = '/'
7
+
8
+ end
9
+ end
@@ -0,0 +1,65 @@
1
+ require 'csv'
2
+ module ActiveAdminImport
3
+ class Importer
4
+
5
+ attr_reader :resource, :file, :options , :result
6
+
7
+ def store data, headers
8
+ result = @resource.transaction do
9
+ options[:before_batch_import].call(data, headers) if options[:before_batch_import].is_a?(Proc)
10
+ result = resource.import headers, data, :validate => options[:validate]
11
+ options[:after_batch_import].call(data, headers) if options[:after_batch_import].is_a?(Proc)
12
+ result
13
+ end
14
+ {:imported => data.count - result.failed_instances.count , :failed => result.failed_instances}
15
+ end
16
+
17
+ def prepare_headers(headers)
18
+ Hash[headers.zip(headers.map { |el| el.underscore.gsub(/\s+/, '_') })]
19
+ end
20
+
21
+
22
+ def initialize resource, file, options
23
+ @resource = resource
24
+ @file = file
25
+ @options = {
26
+ :col_sep => ',',
27
+ :batch_size => 1000,
28
+ :validate => true
29
+ }.merge(options)
30
+ @headers = []
31
+ @result= {
32
+ :failed => [],
33
+ :imported => 0
34
+ }
35
+ end
36
+
37
+ def cycle(lines)
38
+ lines = CSV.parse(lines.join)
39
+ @result.merge!(self.store(lines, @headers.values)){|key,val1,val2| val1+val2}
40
+ end
41
+
42
+
43
+ def import
44
+
45
+ options[:before_import].call(@resource, @file, @options) if options[:before_import].is_a?(Proc)
46
+ lines = []
47
+
48
+ batch_size = @options[:batch_size].to_i
49
+ IO.foreach(file.path) do |line|
50
+ if @headers.empty?
51
+ @headers = prepare_headers(CSV.parse(line).first)
52
+ else
53
+ lines << line
54
+ if lines.size >= batch_size
55
+ cycle lines
56
+ lines = []
57
+ end
58
+ end
59
+ end
60
+ cycle(lines) unless lines.blank?
61
+ options[:after_import].call(@result) if options[:after_import].is_a?(Proc)
62
+ result
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveAdminImport
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_admin_import
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Igor Fedoronchuk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chronic
16
+ requirement: &70329918285720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70329918285720
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord-import
27
+ requirement: &70329918300940 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - =
31
+ - !ruby/object:Gem::Version
32
+ version: 0.3.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70329918300940
36
+ description: CSV import for Active Admin
37
+ email:
38
+ - fedoronchuk@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - active_admin_import.gemspec
49
+ - app/views/admin/import.html.erb
50
+ - lib/active_admin_import.rb
51
+ - lib/active_admin_import/dsl.rb
52
+ - lib/active_admin_import/engine.rb
53
+ - lib/active_admin_import/importer.rb
54
+ - lib/active_admin_import/version.rb
55
+ homepage: http://github.com/Fivell/active_admin_import
56
+ licenses: []
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 1.8.17
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: ActiveAdmin import based on activerecord-import gem.
79
+ test_files: []
80
+ has_rdoc: