Module: ActiveItem::Validations

Included in:
Base
Defined in:
lib/active_item/validations.rb

Instance Method Summary collapse

Instance Method Details

#validates_format_of(*attributes, **options) ⇒ Object



91
92
93
# File 'lib/active_item/validations.rb', line 91

def validates_format_of(*attributes, **options)
  attributes.each { |attribute| validates attribute, format: options }
end

#validates_length_of(*attributes, **options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/active_item/validations.rb', line 30

def validates_length_of(*attributes, **options)
  attributes.each do |attribute|
    validate do
      value = send(attribute)
      next if value.nil?

      length = value.to_s.length

      if options[:minimum] && length < options[:minimum]
        errors.add(attribute, options[:message] || "is too short (minimum is #{options[:minimum]} characters)")
      end
      if options[:maximum] && length > options[:maximum]
        errors.add(attribute, options[:message] || "is too long (maximum is #{options[:maximum]} characters)")
      end
      if options[:in] && !options[:in].include?(length)
        errors.add(attribute, options[:message] || "length must be between #{options[:in].min} and #{options[:in].max} characters")
      end
      if options[:is] && length != options[:is]
        errors.add(attribute, options[:message] || "must be exactly #{options[:is]} characters")
      end
    end
  end
end

#validates_numericality_of(*attributes, **options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/active_item/validations.rb', line 54

def validates_numericality_of(*attributes, **options)
  attributes.each do |attribute|
    validate do
      value = send(attribute)
      next if value.nil?

      unless value.is_a?(Numeric) || value.to_s.match?(/\A-?\d+(\.\d+)?\z/)
        errors.add(attribute, options[:message] || "is not a number")
        next
      end

      num_value = value.to_f

      if options[:only_integer] && num_value != num_value.to_i
        errors.add(attribute, options[:message] || "must be an integer")
        next
      end

      if options[:greater_than] && num_value <= options[:greater_than]
        errors.add(attribute, options[:message] || "must be greater than #{options[:greater_than]}")
      end
      if options[:greater_than_or_equal_to] && num_value < options[:greater_than_or_equal_to]
        errors.add(attribute, options[:message] || "must be greater than or equal to #{options[:greater_than_or_equal_to]}")
      end
      if options[:less_than] && num_value >= options[:less_than]
        errors.add(attribute, options[:message] || "must be less than #{options[:less_than]}")
      end
      if options[:less_than_or_equal_to] && num_value > options[:less_than_or_equal_to]
        errors.add(attribute, options[:message] || "must be less than or equal to #{options[:less_than_or_equal_to]}")
      end
      if options[:equal_to] && num_value != options[:equal_to]
        errors.add(attribute, options[:message] || "must be equal to #{options[:equal_to]}")
      end
    end
  end
end

#validates_uniqueness_of(*attributes, **options) ⇒ Object



26
27
28
# File 'lib/active_item/validations.rb', line 26

def validates_uniqueness_of(*attributes, **options)
  validates(*attributes, uniqueness: options.empty? ? true : options)
end