Class: Attachs::ActiveRecord::Validators::AttachmentSizeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper
Defined in:
lib/attachs/active_record/validators/attachment_size_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/attachs/active_record/validators/attachment_size_validator.rb', line 7

def validate_each(record, attribute, value)
  if value.exists?
    if options.has_key? :in
      if options[:in].exclude? value.size
        record.errors.add(
          attribute,
          :attachment_size_in,
          min: number_to_human_size(options[:in].begin),
          max: number_to_human_size(options[:in].end)
        )
      end
    else
      if options.has_key? :less_than and value.size > options[:less_than]
        record.errors.add attribute, :attachment_size_less_than, count: number_to_human_size(options[:less_than])
      end
      if options.has_key? :greater_than and value.size < options[:greater_than]
        record.errors.add attribute, :attachment_size_greater_than, count: number_to_human_size(options[:greater_than])
      end
    end
  end
end