Class: Importable::ImportedItemsValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
lib/importable/imported_items_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate(importer) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/importable/imported_items_validator.rb', line 3

def validate(importer)
  # don't bother if there are already validation errors
  return if importer.errors[:base].any?

  mapper = importer.mapper

  importer.invalid_items.each do |object, line_number|
    object.errors.messages.each do |error|
      field, errors = *error
      errors.each do |original_message|

        original_val = mapper.original_value_for(line_number, field)

        message = if original_val
          if importer.class == Spreadsheet
            "on line #{line_number} could not be found: #{original_val}"
          else
            "could not be found: #{original_val}"
          end
        else
          "on line #{line_number} #{original_message}"
        end

        importer.errors[field] << message
      end
    end
  end
end