Class: StableMimeTypeValidator

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

Instance Method Summary collapse

Instance Method Details

#mime(value) ⇒ Object



8
9
10
# File 'app/validators/stable_mime_type_validator.rb', line 8

def mime(value)
  Mime::EXTENSION_LOOKUP[File.extname(value).delete(".").downcase].to_s
end

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
# File 'app/validators/stable_mime_type_validator.rb', line 2

def validate_each(record, attribute, value)
  return if record.attachment.nil? || value.nil?
  # i18n-tasks-use t("activerecord.errors.models.model_file.attributes.filename.cannot_change_type")
  record.errors.add attribute, :cannot_change_type if mime(value) != mime(record.attachment.id)
end