Class: DocumentDataDictionary

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

Overview

CSV Header Validation

Defined Under Namespace

Classes: CsvHeaderValidator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sort_entries(id_array) ⇒ Object



43
44
45
46
47
48
49
50
# File 'app/models/document_data_dictionary.rb', line 43

def self.sort_entries(id_array)
  transaction do
    logger.debug { id_array.inspect }
    id_array.each_with_index do |entry_id, i|
      DocumentDataDictionaryEntry.update(entry_id, position: i)
    end
  end
end

Instance Method Details

#parse_csv_fileObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/document_data_dictionary.rb', line 30

def parse_csv_file
  if csv_file.attached?
    csv_data = CSV.parse(csv_file.download, headers: true)
    csv_data.each do |row|
      entry = document_data_dictionary_entries.find_or_initialize_by(
        friendlier_id: row["friendlier_id"],
        field_name: row["field_name"]
      )
      entry.update(row.to_h)
    end
  end
end

#to_csvObject



21
22
23
24
25
26
27
28
# File 'app/models/document_data_dictionary.rb', line 21

def to_csv
  CSV.generate do |csv|
    csv << DocumentDataDictionaryEntry.column_names.excluding("id", "created_at", "updated_at")
    document_data_dictionary_entries.each do |entry|
      csv << entry.attributes.values_at(*DocumentDataDictionaryEntry.column_names.excluding("id", "created_at", "updated_at"))
    end
  end
end