rails_admin_import 0.1.8 → 0.1.9

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b11e6300c09b071ce9b711b6b3fd3cda25aa5520
4
+ data.tar.gz: a462c3e1016ca867f092d0e9219188f693af4c4c
5
+ SHA512:
6
+ metadata.gz: d38fd170f28cd50af1b5bfb632bb32c3dd8c98a5b170e4c11e7bb4a614730872abd9f79592e40c401d4398283dffc013daab13f4a322dc384d42c0a3da68527e
7
+ data.tar.gz: a13f5db4ae247c17ee7a67e8db890356d62b75d473da8ba93662382b97c2677e593f9a19b75ec4fa8fc31f087dfcc2e09f0822fa81f6760397d394aa17356f94
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 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.md CHANGED
@@ -1,3 +1,9 @@
1
+ Request for Contributors / Core Contributors
2
+ ========
3
+
4
+ I apologize for my extreme lack of attention to this repository since it was created. I see that several users have forked this gem and applied updates. I'd be interested in giving access to this main repository for any interested in maintaining it and/or adding features. Please contact me if you are interested at steph at endpoint dot com.
5
+
6
+
1
7
  Rails Admin Import functionality
2
8
  ========
3
9
 
@@ -8,17 +14,17 @@ Installation
8
14
 
9
15
  * First, add to Gemfile:
10
16
 
11
- gem "rails_admin_import", :git => "git://github.com/stephskardal/rails_admin_import.git"
17
+ gem "rails_admin_import"
12
18
 
13
19
  * Next, mount in your application by adding following line to your config/routes.rb:
14
20
 
15
21
  mount RailsAdminImport::Engine => '/rails_admin_import', :as => 'rails_admin_import'
16
22
 
17
- * Add to cancan to allow access to import in your app/models/ability.rb:
23
+ * If you are using cancan, add to ability.rb to specify which models can be imported:
18
24
 
19
25
  can :import, [User, Model1, Model2]
20
26
 
21
- * Define configuration in config/initializers/rails_admin.rb:
27
+ * Define configuration in config/initializers/rails_admin_import.rb:
22
28
 
23
29
  RailsAdminImport.config do |config|
24
30
  config.model User do
@@ -36,8 +42,11 @@ Installation
36
42
 
37
43
  # some model
38
44
  def before_import_save(row, map)
39
- self.set_permalink
40
- self.import_nested_data(row, map)
45
+ # Your custom special sauce
46
+ end
47
+
48
+ def after_import_save(row, map)
49
+ # Your custom special sauce
41
50
  end
42
51
 
43
52
  * "import" action must be added inside config.actions block in main application RailsAdmin configuration: config/initializers/rails_admin.rb.
@@ -62,4 +71,4 @@ TODO
62
71
  Copyright
63
72
  ========
64
73
 
65
- Copyright (c) 2011 End Point & Steph Skardal. See LICENSE.txt for further details.
74
+ Copyright (c) 2014 End Point & Steph Skardal. See LICENSE.txt for further details.
data/RELEASE_NOTES ADDED
@@ -0,0 +1,6 @@
1
+ Version: 0.1.9
2
+ Released: 5.22.2014
3
+ * Updated/corrected README
4
+ * Merged ImportLogger work
5
+ * Merged modifications to import view
6
+ * Merged post save hook on models
data/Rakefile ADDED
@@ -0,0 +1,39 @@
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 = 'RailsAdminImport'
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
+
25
+
26
+
27
+ Bundler::GemHelper.install_tasks
28
+
29
+ require 'rake/testtask'
30
+
31
+ Rake::TestTask.new(:test) do |t|
32
+ t.libs << 'lib'
33
+ t.libs << 'test'
34
+ t.pattern = 'test/**/*_test.rb'
35
+ t.verbose = false
36
+ end
37
+
38
+
39
+ task :default => :test
@@ -1,102 +1,143 @@
1
1
  <% if @response -%>
2
2
  <% if @response.has_key?(:error) -%>
3
- <div class="alert-error alert">
4
- <%= @response[:error] %>
5
- </div>
3
+ <div class="alert-error alert"><%= @response[:error] %></div>
6
4
  <% end -%>
7
5
  <% if @response.has_key?(:notice) -%>
8
- <div class="alert-success alert">
9
- <%= @response[:notice] %>
10
- </div>
6
+ <div class="alert-success alert"><%= @response[:notice] %></div>
11
7
  <% end -%>
12
8
  <% end -%>
13
9
 
14
- <h1>Import <%= @abstract_model.to_param.titleize %></h1>
10
+ <%= form_tag rails_admin.import_url(@abstract_model.to_param), :multipart => true, :class => 'form-horizontal' do |f| -%>
11
+ <fieldset>
12
+ <legend>
13
+ <i class="icon-chevron-down"></i>
14
+ Fields to import
15
+ </legend>
16
+ <div class="control-group">
17
+ <label class="control-label" data-original-title="Click to reverse selection" onclick="jQuery(this).siblings('.controls').find('input').click()" rel="tooltip">Fields from <%= @abstract_model.to_param %></label>
18
+ <div class="controls">
19
+ <ul>
20
+ <% @abstract_model.model.import_fields.each do |field| -%>
21
+ <li><%= field %></li>
22
+ <% end -%>
23
+ </ul>
24
+ <p class="help-block">The following fields may be included in the import file.</p>
25
+ </div>
26
+ </div>
27
+ <% if @abstract_model.model.belongs_to_fields.any? -%>
28
+ <div class="control-group">
29
+ <label class="control-label" data-original-title="Click to reverse selection" onclick="jQuery(this).siblings('.controls').find('input').click()" rel="tooltip">Belongs To Fields</label>
30
+ <div class="controls">
31
+ <ul>
32
+ <% @abstract_model.model.belongs_to_fields.each do |field| -%>
33
+ <li><a href="<%= url_for(:action => :index, :model_name => field) %>"><%= field.to_s.titleize %></a></li>
34
+ <% end -%>
35
+ </ul>
36
+ <p class="help-block">These fields map to other items in the database, lookup via attribute selected below.</p>
37
+ </div>
38
+ </div>
39
+ <% end -%>
40
+ <% if @abstract_model.model.file_fields.any? -%>
41
+ <div class="control-group">
42
+ <label class="control-label" data-original-title="Click to reverse selection" onclick="jQuery(this).siblings('.controls').find('input').click()" rel="tooltip">Belongs To Fields</label>
43
+ <div class="controls">
44
+ <ul>
45
+ <% @abstract_model.model.file_fields.each do |field| -%>
46
+ <li><%= field %></li>
47
+ <% end -%>
48
+ </ul>
49
+ <p class="help-block">These must be a downloadable URL.</p>
50
+ </div>
51
+ </div>
52
+ <% end -%>
53
+ <% if @abstract_model.model.many_fields.any? -%>
54
+ <div class="control-group">
55
+ <label class="control-label" data-original-title="Click to reverse selection" onclick="jQuery(this).siblings('.controls').find('input').click()" rel="tooltip">Belongs To Fields</label>
56
+ <div class="controls">
57
+ <ul>
58
+ <% @abstract_model.model.many_fields.each do |field| -%>
59
+ <li><%= field %></li>
60
+ <% end -%>
61
+ </ul>
62
+ <p class="help-block">These fields map to other columns in the database, lookup via attribute selected below. There may be multiple columns with this header in the spreadsheet.</p>
63
+ </div>
64
+ </div>
65
+ <% end -%>
66
+ <% if RailsAdminImport.config(@abstract_model.model).extra_fields.any? -%>
67
+ <div class="control-group">
68
+ <label class="control-label" data-original-title="Click to reverse selection" onclick="jQuery(this).siblings('.controls').find('input').click()" rel="tooltip">Belongs To Fields</label>
69
+ <div class="controls">
70
+ <ul>
71
+ <% RailsAdminImport.config(@abstract_model.model).extra_fields.each do |field| -%>
72
+ <li><%= field %></li>
73
+ <% end -%>
74
+ </ul>
75
+ <p class="help-block">Additional application specific fields.</p>
76
+ </div>
77
+ </div>
78
+ <% end -%>
79
+ </fieldset>
80
+ <fieldset>
81
+ <legend>
82
+ <i class="icon-chevron-down"></i>
83
+ Upload file
84
+ </legend>
85
+ <div class="control-group">
86
+ <label class="control-label" data-original-title="Click to reverse selection" onclick="jQuery(this).siblings('.controls').find('input').click()" rel="tooltip">File</label>
87
+ <div class="controls">
88
+ <%= file_field_tag :file %>
89
+ <p class="help-block">Please limit upload file to <%= RailsAdminImport.config.line_item_limit %> line items.</p>
90
+ </div>
91
+ </div>
92
+ <div class="control-group">
93
+ <label class="control-label" data-original-title="Click to reverse selection" onclick="jQuery(this).siblings('.controls').find('input').click()" rel="tooltip">Update if exists</label>
94
+ <div class="controls">
95
+ <label class="checkbox">
96
+ <%= check_box_tag :update_if_exists %>
97
+ </label>
98
+ </div>
99
+ </div>
100
+ <div class="control-group">
101
+ <label class="control-label" data-original-title="Click to reverse selection" onclick="jQuery(this).siblings('.controls').find('input').click()" rel="tooltip">Update lookup field</label>
102
+ <div class="controls">
103
+ <select name="update_lookup">
104
+ <% @abstract_model.model.new.attributes.keys.each do |key| -%>
105
+ <option value="<%= key %>"><%= key %></option>
106
+ <% end -%>
107
+ </select>
108
+ </div>
109
+ </div>
15
110
 
16
- <small>The following fields may be included in the import file</small>
17
- <table width="100%" cellpadding="0" cellspacing="0">
18
- <tr>
19
- <td width="20%" valign="top">
20
- <h3>Standard Fields</h3>
21
- <ul>
22
- <% @abstract_model.model.import_fields.each do |field| -%>
23
- <li><%= field %></li>
24
- <% end -%>
25
- </ul>
26
- </td>
27
-
28
- <% if @abstract_model.model.belongs_to_fields.any? -%>
29
- <td width="20%" valign="top">
30
- <h3>Belongs To Fields</h3>
31
- <ul>
32
- <% @abstract_model.model.belongs_to_fields.each do |field| -%>
33
- <li><%= field %></li>
34
- <% end -%>
35
- </ul>
36
- <small>These fields map to other items in the database, lookup via attribute selected below.</small>
37
- </td>
38
- <% end -%>
39
-
40
- <% if @abstract_model.model.file_fields.any? -%>
41
- <td width="20%" valign="top">
42
- <h3>File Fields</h3>
43
- <ul>
44
- <% @abstract_model.model.file_fields.each do |field| -%>
45
- <li><%= field %></li>
46
- <% end -%>
47
- </ul>
48
- <small>These must be a downloadable URL.</small>
49
- </td>
50
- <% end -%>
51
-
52
- <% if @abstract_model.model.many_fields.any? -%>
53
- <td width="20%" valign="top">
54
- <h3>Multiple Fields</h3>
55
- <ul>
56
- <% @abstract_model.model.many_fields.each do |field| -%>
57
- <li><%= field %></li>
58
- <% end -%>
59
- </ul>
60
- <small>These fields map to other columns in the database, lookup via attribute selected below. There may be multiple columns with this header in the spreadsheet.</small>
61
- </td>
62
- <% end -%>
63
-
64
- <% if RailsAdminImport.config(@abstract_model.model).extra_fields.any? -%>
65
- <td width="20%" valign="top">
66
- <h3>Extra Fields</h3>
67
- <ul>
68
- <% RailsAdminImport.config(@abstract_model.model).extra_fields.each do |field| -%>
69
- <li><%= field %></li>
70
- <% end -%>
71
- </ul>
72
- <small>Additional application specific fields.</small>
73
- </td>
74
- <% end -%>
75
- </tr>
76
- </table>
77
-
78
- <%= form_tag rails_admin.import_url(@abstract_model.to_param), :multipart => true do |f| -%>
79
- <h5 style="color:red;">Please limit upload file to <%= RailsAdminImport.config.line_item_limit %> line items.</h5>
80
- <%= file_field_tag :file %>
81
- <p>
82
- <%= check_box_tag :update_if_exists %> Update if Exists<br />
83
- Update lookup field
84
- <select name="update_lookup">
85
- <% @abstract_model.model.new.attributes.keys.each do |key| -%>
86
- <option value="<%= key %>"><%= key %></option>
87
- <% end -%>
88
- </select>
89
- </p>
90
-
91
- <% [@abstract_model.model.belongs_to_fields, @abstract_model.model.many_fields].flatten.each do |field| -%>
92
- <div style="display:inline-block; width: 45%;background:#cecece;margin: 5px;padding: 5px;">
93
- <label style="width:200px;"><%= field %> mapping</label>&nbsp;&nbsp;
94
- <select name="<%= field %>">
95
- <% field.to_s.classify.constantize.new.attributes.keys.each do |key| -%>
96
- <option value="<%= key %>"><%= key %></option>
97
- <% end -%>
98
- <select>
111
+ </fieldset>
112
+ <% if @abstract_model.model.belongs_to_fields.any? -%>
113
+ <fieldset>
114
+ <legend>
115
+ <i class="icon-chevron-down"></i>
116
+ Related fields mapping
117
+ </legend>
118
+ <% [@abstract_model.model.belongs_to_fields, @abstract_model.model.many_fields].flatten.each do |field| -%>
119
+ <div class="control-group">
120
+ <label class="control-label" for="csv_options_encoding_to"><a href="<%= url_for(:action => :index, :model_name => field) %>"><%= field.to_s.titleize %></a> mapping</label>
121
+ <div class="controls">
122
+ <select name="<%= field %>">
123
+ <% field.to_s.classify.constantize.new.attributes.keys.each do |key| -%>
124
+ <option value="<%= key %>"><%= key %></option>
125
+ <% end -%>
126
+ </select>
127
+ </div>
128
+ </div>
129
+ <% end -%>
130
+ </fieldset>
131
+ <% end -%>
132
+ <div class="form-actions">
133
+ <button class="btn btn-primary" name="commit" type="submit" data-disable-with="Uploading...">
134
+ <i class="icon-white icon-ok"></i>
135
+ Import
136
+ </button>
137
+ <button class="btn" name="_continue" type="submit">
138
+ <i class="icon-remove"></i>
139
+ Cancel
140
+ </button>
141
+ <!--<%= submit_tag "Upload", :disable_with => "Uploading..." %>-->
99
142
  </div>
100
- <% end -%><br />
101
- <%= submit_tag "Upload", :disable_with => "Uploading..." %>
102
143
  <% end -%>
@@ -1,4 +1,5 @@
1
1
  require 'open-uri'
2
+ require "rails_admin_import/import_logger"
2
3
 
3
4
  module RailsAdminImport
4
5
  module Import
@@ -61,13 +62,13 @@ module RailsAdminImport
61
62
 
62
63
  if RailsAdminImport.config.logging
63
64
  FileUtils.copy(params[:file].tempfile, "#{Rails.root}/log/import/#{Time.now.strftime("%Y-%m-%d-%H-%M-%S")}-import.csv")
64
- logger = Logger.new("#{Rails.root}/log/import/import.log")
65
65
  end
66
66
 
67
- text = File.read(params[:file].tempfile)
68
- clean = text.force_encoding('BINARY').encode('UTF-8', :undef => :replace, :replace => '').gsub(/\n$/, '')
67
+ text = File.read(params[:file].tempfile)
68
+ clean = text.force_encoding('BINARY').encode('UTF-8', :undef => :replace, :replace => '').gsub(/\n$/, '')
69
69
  file_check = CSV.new(clean)
70
-
70
+ logger = ImportLogger.new
71
+
71
72
  if file_check.readlines.size > RailsAdminImport.config.line_item_limit
72
73
  return results = { :success => [], :error => ["Please limit upload file to #{RailsAdminImport.config.line_item_limit} line items."] }
73
74
  end
@@ -109,18 +110,19 @@ module RailsAdminImport
109
110
  object.before_import_save(row, map)
110
111
 
111
112
  object.import_files(row, map)
112
-
113
+
113
114
  verb = object.new_record? ? "Create" : "Update"
114
115
  if object.errors.empty?
115
116
  if object.save
116
- logger.info "#{Time.now.to_s}: #{verb}d: #{object.send(label_method)}" if RailsAdminImport.config.logging
117
+ logger.info "#{Time.now.to_s}: #{verb}d: #{object.send(label_method)}"
117
118
  results[:success] << "#{verb}d: #{object.send(label_method)}"
119
+ object.after_import_save(row, map)
118
120
  else
119
- logger.info "#{Time.now.to_s}: Failed to #{verb}: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}." if RailsAdminImport.config.logging
121
+ logger.info "#{Time.now.to_s}: Failed to #{verb}: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
120
122
  results[:error] << "Failed to #{verb}: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
121
123
  end
122
124
  else
123
- logger.info "#{Time.now.to_s}: Errors before save: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}." if RailsAdminImport.config.logging
125
+ logger.info "#{Time.now.to_s}: Errors before save: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
124
126
  results[:error] << "Errors before save: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
125
127
  end
126
128
  end
@@ -158,6 +160,10 @@ module RailsAdminImport
158
160
  # Meant to be overridden to do special actions
159
161
  end
160
162
 
163
+ def after_import_save(*args)
164
+ # Meant to be overridden to do special actions
165
+ end
166
+
161
167
  def import_display
162
168
  self.id
163
169
  end
@@ -0,0 +1,14 @@
1
+ module RailsAdminImport
2
+ class ImportLogger
3
+ attr_reader :logger
4
+
5
+ def initialize(log_file_name = 'rails_admin_import.log')
6
+ @logger = Logger.new("#{Rails.root}/log/#{log_file_name}") if RailsAdminImport.config.logging
7
+ end
8
+
9
+ def info(message)
10
+ @logger.info message if RailsAdminImport.config.logging
11
+ end
12
+
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsAdminImport
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.9"
3
3
  end
@@ -5,11 +5,11 @@ require "rails_admin_import/config"
5
5
  module RailsAdminImport
6
6
  def self.config(entity = nil, &block)
7
7
  if entity
8
- RailsAdminImport::Config.model(entity, &block)
8
+ RailsAdminImport::Config.model(entity, &block)
9
9
  elsif block_given? && ENV['SKIP_RAILS_ADMIN_INITIALIZER'] != "true"
10
10
  block.call(RailsAdminImport::Config)
11
- else
12
- RailsAdminImport::Config
11
+ else
12
+ RailsAdminImport::Config
13
13
  end
14
14
  end
15
15
 
metadata CHANGED
@@ -1,23 +1,25 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_admin_import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
5
- prerelease:
4
+ version: 0.1.9
6
5
  platform: ruby
7
6
  authors:
8
7
  - Steph Skardal
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-11-08 00:00:00.000000000 Z
11
+ date: 2014-05-22 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description:
15
14
  email: steph@endpoint.com
16
15
  executables: []
17
16
  extensions: []
18
- extra_rdoc_files:
19
- - README.md
17
+ extra_rdoc_files: []
20
18
  files:
19
+ - MIT-LICENSE
20
+ - README.md
21
+ - RELEASE_NOTES
22
+ - Rakefile
21
23
  - app/views/rails_admin/main/import.html.erb
22
24
  - config/locales/import.en.yml
23
25
  - lib/rails_admin_import.rb
@@ -26,30 +28,29 @@ files:
26
28
  - lib/rails_admin_import/config/model.rb
27
29
  - lib/rails_admin_import/engine.rb
28
30
  - lib/rails_admin_import/import.rb
31
+ - lib/rails_admin_import/import_logger.rb
29
32
  - lib/rails_admin_import/version.rb
30
- - README.md
31
33
  homepage:
32
34
  licenses: []
35
+ metadata: {}
33
36
  post_install_message:
34
37
  rdoc_options: []
35
38
  require_paths:
36
39
  - lib
37
40
  required_ruby_version: !ruby/object:Gem::Requirement
38
- none: false
39
41
  requirements:
40
- - - ! '>='
42
+ - - ">="
41
43
  - !ruby/object:Gem::Version
42
44
  version: '0'
43
45
  required_rubygems_version: !ruby/object:Gem::Requirement
44
- none: false
45
46
  requirements:
46
- - - ! '>='
47
+ - - ">="
47
48
  - !ruby/object:Gem::Version
48
49
  version: '0'
49
50
  requirements: []
50
51
  rubyforge_project:
51
- rubygems_version: 1.8.11
52
+ rubygems_version: 2.2.2
52
53
  signing_key:
53
- specification_version: 3
54
+ specification_version: 4
54
55
  summary: Import functionality for rails admin
55
56
  test_files: []