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


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

def self.helper_method_name
  :validates_file_size
end

Instance Method Details

#check_validity!Object


31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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