Class: ActiveModel::Validations::FileSizeValidator

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

Constant Summary collapse

CHECKS =
{ in: :===,
less_than: :<,
less_than_or_equal_to: :<=,
greater_than: :>,
greater_than_or_equal_to: :>= }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.helper_method_nameObject



11
12
13
# File 'lib/file_validators/validators/file_size_validator.rb', line 11

def self.helper_method_name
  :validates_file_size
end

Instance Method Details

#check_validity!Object



29
30
31
32
33
34
35
36
37
# File 'lib/file_validators/validators/file_size_validator.rb', line 29

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) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/file_validators/validators/file_size_validator.rb', line 15

def validate_each(record, attribute, value)
  values = parse_values(value)
  unless values.empty?
    options.slice(*CHECKS.keys).each do |option, option_value|
      option_value = option_value.call(record) if option_value.is_a?(Proc)
      if values.any? { |v| not valid_size?(value_byte_size(v), option, option_value) }
        record.errors.add(attribute,
                          "file_size_is_#{option}".to_sym,
                          filtered_options(values).merge!(detect_error_options(option_value)))
      end
    end
  end
end