Class: ActiveAdminImport::Importer
- Inherits:
-
Object
- Object
- ActiveAdminImport::Importer
- Defined in:
- lib/active_admin_import/importer.rb
Instance Attribute Summary collapse
-
#csv_lines ⇒ Object
readonly
Returns the value of attribute csv_lines.
-
#cycle_data ⇒ Object
readonly
Returns the value of attribute cycle_data.
-
#extra_options ⇒ Object
readonly
Returns the value of attribute extra_options.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Instance Method Summary collapse
- #cycle(lines) ⇒ Object
- #import ⇒ Object
-
#initialize(resource, file, options, extra_options = nil) ⇒ Importer
constructor
A new instance of Importer.
- #prepare_headers(headers) ⇒ Object
- #store ⇒ Object
Constructor Details
#initialize(resource, file, options, extra_options = nil) ⇒ Importer
Returns a new instance of Importer.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/active_admin_import/importer.rb', line 31 def initialize resource, file, , = nil @resource = resource @file = file @options = { :col_sep => ',', :batch_size => 1000, :validate => true }.merge() @headers = [] @result= { :failed => [], :imported => 0 } @extra_options = @csv_options = @options.slice(:col_sep, :row_sep) end |
Instance Attribute Details
#csv_lines ⇒ Object (readonly)
Returns the value of attribute csv_lines.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def csv_lines @csv_lines end |
#cycle_data ⇒ Object (readonly)
Returns the value of attribute cycle_data.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def cycle_data @cycle_data end |
#extra_options ⇒ Object (readonly)
Returns the value of attribute extra_options.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def @extra_options end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def file @file end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def headers @headers end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def @options end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def resource @resource end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
5 6 7 |
# File 'lib/active_admin_import/importer.rb', line 5 def result @result end |
Instance Method Details
#cycle(lines) ⇒ Object
48 49 50 51 |
# File 'lib/active_admin_import/importer.rb', line 48 def cycle(lines) @csv_lines = CSV.parse(lines.join, @csv_options) @result.merge!(self.store){|key,val1,val2| val1+val2} end |
#import ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/active_admin_import/importer.rb', line 53 def import [:before_import].call(self) if [:before_import].is_a?(Proc) lines = [] batch_size = [:batch_size].to_i IO.foreach(file.path) do |line| next if line.blank? if headers.empty? prepare_headers(CSV.parse(line, @csv_options).first) else lines << line if lines.size >= batch_size cycle lines lines = [] end end end cycle(lines) unless lines.blank? [:after_import].call(self) if [:after_import].is_a?(Proc) result end |
#prepare_headers(headers) ⇒ Object
25 26 27 28 29 |
# File 'lib/active_admin_import/importer.rb', line 25 def prepare_headers(headers) @headers = Hash[headers.zip(headers.map { |el| el.underscore.gsub(/\s+/, '_') })] @headers.merge!([:headers_rewrites]) @headers end |
#store ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/active_admin_import/importer.rb', line 8 def store result = @resource.transaction do [:before_batch_import].call(self) if [:before_batch_import].is_a?(Proc) result = resource.import headers.values, csv_lines, { :validate => [:validate], :on_duplicate_key_update => [:on_duplicate_key_update], :ignore => [:ignore], :timestamps => [:timestamps] } [:after_batch_import].call(self) if [:after_batch_import].is_a?(Proc) result end {:imported => csv_lines.count - result.failed_instances.count , :failed => result.failed_instances} end |