Class: ActiveModel::Validations::FileReadabilityValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/active_model/validations/file_readability_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_model/validations/file_readability_validator.rb', line 4

def validate_each(record, attribute, value)
  filename = File.expand_path(value) if !value.nil? && value.respond_to?(:to_s)
  if filename.nil?
    record.errors.add(attribute, options[:message] || :filename_is_nil)
    return
  end
  unless File.exists?(filename)
    record.errors.add(attribute, options[:message] || :file_not_existent, filename: filename)
    return
  end
  unless File.file?(filename)
    record.errors.add(attribute, options[:message] || :file_not_file, filename: filename)
    return
  end
  unless File.readable?(filename)
    record.errors.add(attribute, options[:message] || :file_not_readable, filename: filename)
    return
  end
end