rails_admin_import 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,7 +8,7 @@ Installation
8
8
 
9
9
  * First, add to Gemfile:
10
10
 
11
- gem "rails_admin_import", :git => "git://github.com/stephskardal/demo.git"
11
+ gem "rails_admin_import", :git => "git://github.com/stephskardal/rails_admin_import.git"
12
12
 
13
13
  * Next, mount in your application by adding:
14
14
 
@@ -42,11 +42,14 @@ Installation
42
42
 
43
43
  * "import" action must be added inside config.actions block in main application RailsAdmin configuration.
44
44
 
45
- config.actions do
46
- ...
47
- import
45
+ config.actions do
46
+ ...
47
+ import
48
+ ...
49
+ end
48
50
 
49
- end
51
+ * TODO: Right now, import doesn't work for fields ending in s, because inflector fails in models ending in s singularly. Belongs_to and many
52
+ mapping needs to be updated to use klasses instead of symbols
50
53
 
51
54
  Refer to RailAdmin documentation on custom actions that must be present in this block.
52
55
 
@@ -9,6 +9,7 @@ module RailsAdminImport
9
9
  #
10
10
  # @see RailsAdminImport::Config.model
11
11
  attr_reader :registry
12
+ attr_accessor :logging
12
13
 
13
14
  # Loads a model configuration instance from the registry or registers
14
15
  # a new one if one is yet to be added.
@@ -35,6 +36,7 @@ module RailsAdminImport
35
36
  # @see RailsAdminImport::Config.registry
36
37
  def reset
37
38
  @registry = {}
39
+ @logging = false
38
40
  end
39
41
 
40
42
  # Reset a provided model's configuration.
@@ -6,10 +6,11 @@ module RailsAdminImport
6
6
 
7
7
  module ClassMethods
8
8
  def file_fields
9
+ attrs = []
9
10
  if self.methods.include?(:attachment_definitions) && !self.attachment_definitions.nil?
10
- return self.attachment_definitions.keys
11
+ attrs = self.attachment_definitions.keys
11
12
  end
12
- []
13
+ attrs - RailsAdminImport.config(self).excluded_fields
13
14
  end
14
15
 
15
16
  def import_fields
@@ -58,6 +59,12 @@ module RailsAdminImport
58
59
  end
59
60
 
60
61
  file = CSV.new(params[:file].tempfile)
62
+
63
+ if RailsAdminImport.config.logging
64
+ FileUtils.copy(params[:file].tempfile, "#{Rails.root}/log/import/#{Time.now.strftime("%Y-%m-%d-%H-%M-%S")}-import.csv")
65
+ logger = Logger.new("#{Rails.root}/log/import/import.log")
66
+ end
67
+
61
68
  map = {}
62
69
 
63
70
  file.readline.each_with_index do |key, i|
@@ -94,11 +101,14 @@ module RailsAdminImport
94
101
  verb = object.new_record? ? "Create" : "Update"
95
102
  if object.errors.empty?
96
103
  if object.save
104
+ logger.info "#{Time.now.to_s}: #{verb}d: #{object.send(label_method)}" if RailsAdminImport.config.logging
97
105
  results[:success] << "#{verb}d: #{object.send(label_method)}"
98
106
  else
107
+ logger.info "#{Time.now.to_s}: Failed to #{verb}: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}." if RailsAdminImport.config.logging
99
108
  results[:error] << "Failed to #{verb}: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
100
109
  end
101
110
  else
111
+ logger.info "#{Time.now.to_s}: Errors before save: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}." if RailsAdminImport.config.logging
102
112
  results[:error] << "Errors before save: #{object.send(label_method)}. Errors: #{object.errors.full_messages.join(', ')}."
103
113
  end
104
114
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_admin_import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-05 00:00:00.000000000 Z
12
+ date: 2012-07-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description:
15
15
  email: steph@endpoint.com