Skip to content
This repository
Browse code

added :other_than => :!= option to numericality validator

  • Loading branch information...
commit 0fe311a7fcf1457b9a72f99f887f756a28a53db4 1 parent afea8c7
Kuba Kuźma qoobaa authored
1  activemodel/lib/active_model/locale/en.yml
@@ -23,5 +23,6 @@ en:
23 23 equal_to: "must be equal to %{count}"
24 24 less_than: "must be less than %{count}"
25 25 less_than_or_equal_to: "must be less than or equal to %{count}"
  26 + other_than: "must be other than %{count}"
26 27 odd: "must be odd"
27 28 even: "must be even"
5 activemodel/lib/active_model/validations/numericality.rb
@@ -5,7 +5,7 @@ module Validations
5 5 class NumericalityValidator < EachValidator
6 6 CHECKS = { :greater_than => :>, :greater_than_or_equal_to => :>=,
7 7 :equal_to => :==, :less_than => :<, :less_than_or_equal_to => :<=,
8   - :odd => :odd?, :even => :even? }.freeze
  8 + :odd => :odd?, :even => :even?, :other_than => :!= }.freeze
9 9
10 10 RESERVED_OPTIONS = CHECKS.keys + [:only_integer]
11 11
@@ -99,6 +99,7 @@ module HelperMethods
99 99 # * <tt>:equal_to</tt> - Specifies the value must be equal to the supplied value.
100 100 # * <tt>:less_than</tt> - Specifies the value must be less than the supplied value.
101 101 # * <tt>:less_than_or_equal_to</tt> - Specifies the value must be less than or equal the supplied value.
  102 + # * <tt>:other_than</tt> - Specifies the value must be other than the supplied value.
102 103 # * <tt>:odd</tt> - Specifies the value must be an odd number.
103 104 # * <tt>:even</tt> - Specifies the value must be an even number.
104 105 # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
@@ -107,7 +108,7 @@ module HelperMethods
107 108 # * <tt>:unless</tt> - Specifies a method, proc or string to call to determine if the validation should
108 109 # not occur (e.g. <tt>:unless => :skip_validation</tt>, or <tt>:unless => Proc.new { |user| user.signup_step <= 2 }</tt>). The
109 110 # method, proc or string should return or evaluate to a true or false value.
110   - # * <tt>:strict</tt> - Specifies whether validation should be strict.
  111 + # * <tt>:strict</tt> - Specifies whether validation should be strict.
111 112 # See <tt>ActiveModel::Validation#validates!</tt> for more information
112 113 #
113 114 # The following checks can also be supplied with a proc or a symbol which corresponds to a method:
7 activemodel/test/cases/validations/numericality_validation_test.rb
@@ -106,6 +106,13 @@ def test_validates_numericality_with_greater_than_less_than_and_even
106 106 valid!([2])
107 107 end
108 108
  109 + def test_validates_numericality_with_other_than
  110 + Topic.validates_numericality_of :approved, :other_than => 0
  111 +
  112 + invalid!([0, 0.0])
  113 + valid!([-1, 42])
  114 + end
  115 +
109 116 def test_validates_numericality_with_proc
110 117 Topic.send(:define_method, :min_approved, lambda { 5 })
111 118 Topic.validates_numericality_of :approved, :greater_than_or_equal_to => Proc.new {|topic| topic.min_approved }

0 comments on commit 0fe311a

Please sign in to comment.
Something went wrong with that request. Please try again.