Class: MiniPaperclip::Validators::FileSizeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/mini_paperclip/validators/file_size_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mini_paperclip/validators/file_size_validator.rb', line 6

def validate_each(record, attribute, value)
  attachment_file_size_name = "#{attribute}_file_size"
  attachment_file_size = record.read_attribute_for_validation(attachment_file_size_name)
  if attachment_file_size
    if check_value = options[:less_than]
      unless attachment_file_size < check_value
        count = ActiveSupport::NumberHelper.number_to_human_size(check_value)
        record.errors.add(attribute, :less_than, count: count)
        record.errors.add(attachment_file_size_name, :less_than, count: count)
      end
    end
  end
end