Module: CSVImportable::Importable

Extended by:
ActiveSupport::Concern
Includes:
Asyncable
Defined in:
lib/csv_importable/importable.rb

Constant Summary collapse

DEFAULT_BIG_FILE_THRESHOLD =
10

Instance Method Summary collapse

Instance Method Details

#display_statusObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/csv_importable/importable.rb', line 57

def display_status
  case status
  when Statuses::SUCCEEDED
    'Import Succeeded'
  when Statuses::FAILED
    'Import Failed with Errors'
  else
    'Processing'
  end
end

#formatted_errorsObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/csv_importable/importable.rb', line 46

def formatted_errors
  @formatted_errors ||= (
    errors = []
    error_key = CSVImportable::CSVImporter::Statuses::ERROR
    errors << results.fetch(error_key) if results.has_key?(error_key)
    errors + results.fetch(:results, [])
      .select { |result| result.fetch(:status) == error_key }
      .map    { |error| "Line #{error.fetch(:row)}: #{error.fetch(:errors).join(', ')}" }
  )
end

#import!Object

END INTERFACE METHODS ===



32
33
34
35
36
# File 'lib/csv_importable/importable.rb', line 32

def import!
  # start_async provided by Asyncable module
  return start_async if run_async?
  process_now
end

#not_async?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/csv_importable/importable.rb', line 42

def not_async?
  !run_async?
end

#number_of_records_importedObject



68
69
70
71
# File 'lib/csv_importable/importable.rb', line 68

def number_of_records_imported
  return 0 unless results && results.is_a?(Hash)
  results[:results].try(:count) || 0
end

#read_fileObject

PUBLIC INTERFACE METHODS ===



19
20
21
22
23
# File 'lib/csv_importable/importable.rb', line 19

def read_file
  # returns CSV StringIO data
  # e.g. Paperclip.io_adapters.for(file).read
  fail "read_file method is required by #{self.class.name}"
end

#run_async?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/csv_importable/importable.rb', line 38

def run_async?
  big_file?
end

#save_to_dbObject



26
27
28
29
# File 'lib/csv_importable/importable.rb', line 26

def save_to_db
  return save if respond_to?(:save)
  fail "please implement the save_to_db method on #{self.class.name}"
end