class ActiveModel::Validations::FileSizeValidator

Constants

CHECKS

Public Class Methods

helper_method_name() click to toggle source
# File lib/file_validators/validators/file_size_validator.rb, line 12
def self.helper_method_name
  :validates_file_size
end

Public Instance Methods

check_validity!() click to toggle source
# File lib/file_validators/validators/file_size_validator.rb, line 31
def check_validity!
  unless (CHECKS.keys & options.keys).present?
    raise ArgumentError, 'You must at least pass in one of these options' \
                         ' - :in, :less_than, :less_than_or_equal_to,' \
                         ' :greater_than and :greater_than_or_equal_to'
  end

  check_options(Numeric, options.slice(*(CHECKS.keys - [:in])))
  check_options(Range, options.slice(:in))
end
validate_each(record, attribute, value) click to toggle source
# File lib/file_validators/validators/file_size_validator.rb, line 16
def validate_each(record, attribute, value)
  begin
    values = parse_values(value)
  rescue JSON::ParserError
    record.errors.add attribute, :invalid
    return
  end

  return if values.empty?

  options.slice(*CHECKS.keys).each do |option, option_value|
    check_errors(record, attribute, values, option, option_value)
  end
end

Private Instance Methods

check_errors(record, attribute, values, option, option_value) click to toggle source
# File lib/file_validators/validators/file_size_validator.rb, line 63
def check_errors(record, attribute, values, option, option_value)
  option_value = option_value.call(record) if option_value.is_a?(Proc)
  has_invalid_size = values.any? { |v| !valid_size?(value_byte_size(v), option, option_value) }
  if has_invalid_size
    record.errors.add(
      attribute,
      "file_size_is_#{option}".to_sym,
      **filtered_options(values).merge!(detect_error_options(option_value))
    )
  end
end
check_options(klass, options) click to toggle source
# File lib/file_validators/validators/file_size_validator.rb, line 55
def check_options(klass, options)
  options.each do |option, value|
    unless value.is_a?(klass) || value.is_a?(Proc)
      raise ArgumentError, ":#{option} must be a #{klass.name.to_s.downcase} or a proc"
    end
  end
end
detect_error_options(option_value) click to toggle source
# File lib/file_validators/validators/file_size_validator.rb, line 98
def detect_error_options(option_value)
  if option_value.is_a?(Range)
    { min: human_size(option_value.min), max: human_size(option_value.max) }
  else
    { count: human_size(option_value) }
  end
end
filtered_options(value) click to toggle source
# File lib/file_validators/validators/file_size_validator.rb, line 92
def filtered_options(value)
  filtered = options.except(*CHECKS.keys)
  filtered[:value] = value
  filtered
end
human_size(size) click to toggle source
# File lib/file_validators/validators/file_size_validator.rb, line 106
def human_size(size)
  if defined?(ActiveSupport::NumberHelper) # Rails 4.0+
    ActiveSupport::NumberHelper.number_to_human_size(size)
  else
    storage_units_format = I18n.translate(
      :'number.human.storage_units.format',
      locale: options[:locale],
      raise: true
    )

    unit = I18n.translate(
      :'number.human.storage_units.units.byte',
      locale: options[:locale],
      count: size.to_i,
      raise: true
    )

    storage_units_format.gsub(/%n/, size.to_i.to_s).gsub(/%u/, unit).html_safe
  end
end
parse_values(value) click to toggle source
# File lib/file_validators/validators/file_size_validator.rb, line 44
def parse_values(value)
  return [] unless value.present?

  value = JSON.parse(value) if value.is_a?(String)
  return [] unless value.present?

  value = OpenStruct.new(value) if value.is_a?(Hash)

  Array.wrap(value).reject(&:blank?)
end
valid_size?(size, option, option_value) click to toggle source
# File lib/file_validators/validators/file_size_validator.rb, line 83
def valid_size?(size, option, option_value)
  return false if size.nil?
  if option_value.is_a?(Range)
    option_value.send(CHECKS[option], size)
  else
    size.send(CHECKS[option], option_value)
  end
end
value_byte_size(value) click to toggle source
# File lib/file_validators/validators/file_size_validator.rb, line 75
def value_byte_size(value)
  if value.respond_to?(:byte_size)
    value.byte_size
  else
    value.size
  end
end