提交 aee14630 编写于 作者: J Josh Susser 提交者: Michael Koziarski

coerce blank strings to nil values for boolean and integer fields

Signed-off-by: NMichael Koziarski <michael@koziarski.com>
上级 8622787f
......@@ -2572,11 +2572,14 @@ def ensure_proper_type
end
def convert_number_column_value(value)
case value
when FalseClass; 0
when TrueClass; 1
when ''; nil
else value
if value == false
0
elsif value == true
1
elsif value.is_a?(String) && value.blank?
nil
else
value
end
end
......
......@@ -138,7 +138,11 @@ def string_to_dummy_time(string)
# convert something to a boolean
def value_to_boolean(value)
TRUE_VALUES.include?(value)
if value.blank?
nil
else
TRUE_VALUES.include?(value)
end
end
# convert something to a BigDecimal
......
......@@ -1420,8 +1420,8 @@ def test_default_validates_numericality_of
def test_validates_numericality_of_with_nil_allowed
Topic.validates_numericality_of :approved, :allow_nil => true
invalid!(BLANK + JUNK)
valid!(NIL + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
invalid!(JUNK)
valid!(NIL + BLANK + FLOATS + INTEGERS + BIGDECIMAL + INFINITY)
end
def test_validates_numericality_of_with_integer_only
......@@ -1434,8 +1434,8 @@ def test_validates_numericality_of_with_integer_only
def test_validates_numericality_of_with_integer_only_and_nil_allowed
Topic.validates_numericality_of :approved, :only_integer => true, :allow_nil => true
invalid!(BLANK + JUNK + FLOATS + BIGDECIMAL + INFINITY)
valid!(NIL + INTEGERS)
invalid!(JUNK + FLOATS + BIGDECIMAL + INFINITY)
valid!(NIL + BLANK + INTEGERS)
end
def test_validates_numericality_with_greater_than
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册