Class: Para::Importer::Base

Inherits:
Job::Base
  • Object
show all
Defined in:
lib/para/importer/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



6
7
8
# File 'lib/para/importer/base.rb', line 6

def file
  @file
end

#sheetObject (readonly)

Returns the value of attribute sheet.



6
7
8
# File 'lib/para/importer/base.rb', line 6

def sheet
  @sheet
end

Instance Method Details

#perform(file, options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/para/importer/base.rb', line 8

def perform(file, options = {})
  @file = file
  options.reverse_merge!(extension: file.attachment_ext)
  @sheet = Roo::Spreadsheet.open(file.attachment_path, options)

  ActiveRecord::Base.transaction do
    (2..(sheet.last_row)).each do |index|
      begin
        progress!
        import_from_row(sheet.row(index))
      rescue ActiveRecord::RecordInvalid => error
        if allows_import_errors?
          add_errors_from(index, error.record)
        else
          raise
        end
      end
    end
  end

  save_errors!

  # Ensure that `.perform_now` returns the importer
  self
end