Class: UploadValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/echo_uploads/validation.rb

Overview

If you pass in the presence: true option, this validator will assume you’re using the CachedUploads module. It will look at the has_cached_upload config for the given attribute and check if a) the upload is present, b) the temporary MD5 hash is present, or c) the record has already been saved.

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/echo_uploads/validation.rb', line 78

def validate_each(record, attribute, value)
  if options[:presence]
    config = record.class.cached_uploads[attribute.to_sym]
    if record.new_record? and value.blank? and record.send(config[:md5_attr]).blank?
      record.errors[attribute] << (options[:message] || "can't be blank")
    end
  end
  if value.present? and value.size > options[:max_size]
    record.errors[attribute] << (options[:message] || "is too large (max is #{options[:max_size]} bytes)")
  end
end