Module: ActiveItem::Validations
- Included in:
- Base
- Defined in:
- lib/active_item/validations.rb
Instance Method Summary collapse
- #validates_format_of(*attributes, **options) ⇒ Object
- #validates_length_of(*attributes, **options) ⇒ Object
- #validates_numericality_of(*attributes, **options) ⇒ Object
- #validates_uniqueness_of(*attributes, **options) ⇒ Object
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, **) attributes.each { |attribute| validates attribute, format: } 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, **) attributes.each do |attribute| validate do value = send(attribute) next if value.nil? length = value.to_s.length if [:minimum] && length < [:minimum] errors.add(attribute, [:message] || "is too short (minimum is #{[:minimum]} characters)") end if [:maximum] && length > [:maximum] errors.add(attribute, [:message] || "is too long (maximum is #{[:maximum]} characters)") end if [:in] && ![:in].include?(length) errors.add(attribute, [:message] || "length must be between #{[:in].min} and #{[:in].max} characters") end if [:is] && length != [:is] errors.add(attribute, [:message] || "must be exactly #{[: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, **) 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, [:message] || "is not a number") next end num_value = value.to_f if [:only_integer] && num_value != num_value.to_i errors.add(attribute, [:message] || "must be an integer") next end if [:greater_than] && num_value <= [:greater_than] errors.add(attribute, [:message] || "must be greater than #{[:greater_than]}") end if [:greater_than_or_equal_to] && num_value < [:greater_than_or_equal_to] errors.add(attribute, [:message] || "must be greater than or equal to #{[:greater_than_or_equal_to]}") end if [:less_than] && num_value >= [:less_than] errors.add(attribute, [:message] || "must be less than #{[:less_than]}") end if [:less_than_or_equal_to] && num_value > [:less_than_or_equal_to] errors.add(attribute, [:message] || "must be less than or equal to #{[:less_than_or_equal_to]}") end if [:equal_to] && num_value != [:equal_to] errors.add(attribute, [:message] || "must be equal to #{[: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, **) validates(*attributes, uniqueness: .empty? ? true : ) end |