Class: Import::CsvHeaderValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/models/import/csv_header_validator.rb

Overview

CsvHeaderValidator

Instance Method Summary collapse

Instance Method Details

#valid_csv_headers?(csv_file) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'app/models/import/csv_header_validator.rb', line 20

def valid_csv_headers?(csv_file)
  headers = CSV.parse(csv_file.download)[0]
  (["Title", "Resource Class", "Access Rights", "ID"] - headers).empty?
rescue ArgumentError, ActiveStorage::FileNotFoundError
  false
end

#validate(record) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'app/models/import/csv_header_validator.rb', line 9

def validate(record)
  valid_csv_header = true
  unless valid_csv_headers?(record&.csv_file)
    valid_csv_header = false
    record.errors.add(:csv_file,
      "Missing a required CSV header. Title, Resource Class, Access Rights, and ID are required.")
  end

  valid_csv_header
end