Class: CarrierWaveDirect::Validations::ActiveModel::FilenameFormatValidator

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

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/carrierwave_direct/validations/active_model.rb', line 24

def validate_each(record, attribute, value)
  if record.send("has_#{attribute}_upload?") && record.send("#{attribute}_key") !~ record.send(attribute).key_regexp
    extensions = record.send(attribute).extension_allowlist
    message = I18n.t("errors.messages.carrierwave_direct_filename_invalid")

    if extensions.present?
      message += I18n.t("errors.messages.carrierwave_direct_allowed_extensions", :extensions => extensions.to_sentence)
    end

    record.errors.add(attribute, message)
  end
end