Class: ReadWriteValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/read_write_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/validators/read_write_validator.rb', line 2

def validate_each(record, attribute, value)
  return if value.nil?
  # i18n-tasks-use t("activerecord.errors.models.library.attributes.path.not_a_directory")
  record.errors.add attribute, :not_a_directory unless FileTest.directory?(value)
  # i18n-tasks-use t("activerecord.errors.models.library.attributes.path.non_readable")
  record.errors.add attribute, :non_readable unless FileTest.readable?(value)
  # i18n-tasks-use t("activerecord.errors.models.library.attributes.path.non_writable")
  record.errors.add attribute, :non_writable unless FileTest.writable?(value)
  # Make sure subfolder permissions are OK as well
  if File.exist?(value) && record.errors.empty?
    # Use Dir.glob because it leaves out hidden files
    Dir.glob(File.join(value, "*")).each do |path|
      if FileTest.directory?(path)
        # i18n-tasks-use t("activerecord.errors.models.library.attributes.path.non_readable_subfolder")
        record.errors.add attribute, :non_readable_subfolder unless FileTest.readable?(path)
        # i18n-tasks-use t("activerecord.errors.models.library.attributes.path.non_writable_subfolder")
        record.errors.add attribute, :non_writable_subfolder unless FileTest.writable?(path)
      end
    end
  end
end