Class: Import

Inherits:
ApplicationRecord show all
Includes:
ActiveModel::Validations
Defined in:
app/models/import.rb,
app/models/import/csv_header_validator.rb,
app/models/import/csv_duplicates_validator.rb

Overview

CSV Duplicates Validator

  • Checks the CSV file for duplicate IDs.

  • Fails import if duplicate ID is found.

Direct Known Subclasses

ImportBtaa, ImportBtaaAardvark, ImportGblv1

Defined Under Namespace

Classes: CsvDuplicatesValidator, CsvHeaderValidator

Instance Method Summary collapse

Instance Method Details

#all_mapping_keysObject



54
55
56
57
58
59
60
61
# File 'app/models/import.rb', line 54

def all_mapping_keys
  database = mappings.collect(&:destination_field)
  default = default_mappings.collect(&:keys)
  assumed = assumed_mappings.collect(&:values)

  mappings = (database + default + assumed).flatten
  mappings.map(&:to_s)
end

#check_if_mappedObject



50
51
52
# File 'app/models/import.rb', line 50

def check_if_mapped
  state_machine.transition_to!(:mapped) if mappings_valid? && state_machine.can_transition_to?(:mapped)
end

#convert_data(extract_hash) ⇒ Object



78
79
80
81
82
83
84
# File 'app/models/import.rb', line 78

def convert_data(extract_hash)
  converted_data = transform_extract(extract_hash)
  converted_data = append_default_mappings(converted_data)
  converted_data = append_assumed_mappings(converted_data)
  converted_data = append_derived_mappings(converted_data)
  append_required_mappings(converted_data)
end

#mappings_valid?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'app/models/import.rb', line 63

def mappings_valid?
  (GEOMG_SCHEMA[:required] - all_mapping_keys.uniq).empty?
end

#run!Object



67
68
69
70
71
72
73
74
75
76
# File 'app/models/import.rb', line 67

def run!
  # @TODO: guard this call, unless mappings_valid?

  # Queue Job
  ImportRunJob.perform_later(self)

  # Capture State
  state_machine.transition_to!(:imported)
  save
end

#set_csv_file_attributesObject



38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/import.rb', line 38

def set_csv_file_attributes
  parsed = CSV.parse(csv_file.download)

  update_columns(
    headers: parsed[0],
    row_count: parsed.size - 1,
    content_type: csv_file.content_type.to_s,
    filename: csv_file.filename.to_s,
    extension: csv_file.filename.extension.to_s
  )
end

#state_machineObject



34
35
36
# File 'app/models/import.rb', line 34

def state_machine
  @state_machine ||= ImportStateMachine.new(self, transition_class: ImportTransition)
end