class Attachs::Extensions::ActiveRecord::Validations::AttachmentSizeValidator

Constants

CHECKS

Public Class Methods

new(options) click to toggle source
Calls superclass method
# File lib/attachs/extensions/active_record/validations/attachment_size_validator.rb, line 10
def initialize(options)
  if range = (options[:in] || options[:within])
    options[:less_than_or_equal_to] = range.max
    options[:greater_than_or_equal_to] = range.min
  end
  super
end

Public Instance Methods

validate_attachment(record, attachment) click to toggle source
# File lib/attachs/extensions/active_record/validations/attachment_size_validator.rb, line 18
def validate_attachment(record, attachment)
  unless attachment.blank?
    CHECKS.each do |name, operator|
      if options.has_key?(name)
        other = options[name]
        case other
        when Symbol
          other = record.send(other)
        when Proc
          other = other.call(record)
        end
        unless attachment.size.send(operator, other)
          attachment.errors.add :value, name, count: humanize_size(other)
        end
      end
    end
  end
end

Private Instance Methods

humanize_size(size) click to toggle source
# File lib/attachs/extensions/active_record/validations/attachment_size_validator.rb, line 39
def humanize_size(size)
  ActiveSupport::NumberHelper.number_to_human_size size
end