Class: SizeValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/vexile/validators/size_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/vexile/validators/size_validator.rb', line 2

def validate_each(record, attribute, value)
  real_value = record.__send__(attribute)
  if real_value.respond_to? :size
    if @options[:with] and (real_value.size != @options[:with].to_i)
      record.errors.add attribute, I18n.translate('errors.messages.incorrect_count', :count => @options[:with])
    else
      if @options[:min] and (real_value.size < @options[:min].to_i)
        record.errors.add attribute, I18n.translate('errors.messages.too_few', :count => @options[:min])
      end
      if @options[:max] and (real_value.size > @options[:max].to_i)
        record.errors.add attribute, I18n.translate('errors.messages.too_many', :count => @options[:max])
      end
    end
  else
    if (@options[:with] and (@options[:with].to_i != 0)) || @options[:min]
      record.errors.add attribute, I18n.translate('errors.messages.uncountable')
    end
  end
end