Class: SexyValidations::Validators::Count

Inherits:
Object
  • Object
show all
Defined in:
lib/sexy_validations/validators/count.rb

Class Method Summary collapse

Class Method Details

.validate(record, attribute, value, options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/sexy_validations/validators/count.rb', line 5

def self.validate(record, attribute, value, options)
  return unless value

  unless options.is_a?(Hash)
    options = {
      :within => options,
    }
  end
  
  case options[:within]
    when Fixnum, Integer, Bignum
      unless value.count == options[:within]
        record.errors.add(attribute, "ungültige Anzahl Selektionen")
      end

    when Array
      unless options[:within].include?(value.count)
        record.errors.add(attribute, "ungültige Anzahl Selektionen")
      end
    
    when Range
      min = options[:within].min
      if value.count < min
        record.errors.add(attribute, "zu wenig Selektionen (mindestes #{min})")
      end

      max = options[:within].max
      if value.count > max
        record.errors.add(attribute, "zu viele Selektionen (maximal #{max})")
      end
  end
end