Class: CarrierWaveDirect::Validations::ActiveModel::RemoteNetUrlFormatValidator

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/carrierwave_direct/validations/active_model.rb', line 39

def validate_each(record, attribute, value)
  if record.send("has_remote_#{attribute}_net_url?")
    remote_net_url = record.send("remote_#{attribute}_net_url")
    uploader = record.send(attribute)
    url_scheme_white_list = uploader.url_scheme_white_list

    if (remote_net_url !~ URI.regexp(url_scheme_white_list) || remote_net_url !~ /#{uploader.extension_regexp}\z/)
      extensions = uploader.extension_white_list

      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

      if url_scheme_white_list.present?
        message += I18n.t("errors.messages.carrierwave_direct_allowed_schemes", :schemes => url_scheme_white_list.to_sentence)
      end

      record.errors.add(:"remote_#{attribute}_net_url", message)
    end
  end
end