Class: ActiveAdminImport::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin_import/importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options , extra_options = nil
  @resource = resource
  @file = file
  @options = {
      :col_sep => ',',
      :batch_size => 1000,
      :validate => true
  }.merge(options)
  @headers = []
  @result= {
    :failed => [],
    :imported => 0
  }
  @extra_options = extra_options
  @csv_options = @options.slice(:col_sep, :row_sep)
end

Instance Attribute Details

#csv_linesObject (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_dataObject (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_optionsObject (readonly)

Returns the value of attribute extra_options.



5
6
7
# File 'lib/active_admin_import/importer.rb', line 5

def extra_options
  @extra_options
end

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/active_admin_import/importer.rb', line 5

def file
  @file
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/active_admin_import/importer.rb', line 5

def headers
  @headers
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/active_admin_import/importer.rb', line 5

def options
  @options
end

#resourceObject (readonly)

Returns the value of attribute resource.



5
6
7
# File 'lib/active_admin_import/importer.rb', line 5

def resource
  @resource
end

#resultObject (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

#importObject



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
  options[:before_import].call(self) if options[:before_import].is_a?(Proc)
  lines = []
  batch_size = options[: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?
  options[:after_import].call(self) if options[: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!(options[:headers_rewrites])
  @headers
end

#storeObject



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
    options[:before_batch_import].call(self) if options[:before_batch_import].is_a?(Proc)


    result = resource.import headers.values, csv_lines, {
        :validate => options[:validate],
        :on_duplicate_key_update => options[:on_duplicate_key_update],
        :ignore => options[:ignore],
        :timestamps => options[:timestamps]
    }
    options[:after_batch_import].call(self) if options[:after_batch_import].is_a?(Proc)
    result
  end
  {:imported => csv_lines.count -  result.failed_instances.count , :failed => result.failed_instances}
end