1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

coerce blank strings to nil values for boolean and integer fields

[#860 state:resolved]
This commit is contained in:
Josh Susser 2008-08-18 15:56:37 -07:00 committed by Jeremy Kemper
parent 707ee0e269
commit e48e77e022
5 changed files with 27 additions and 12 deletions

View file

@ -2607,11 +2607,14 @@ module ActiveRecord #:nodoc:
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

View file

@ -138,7 +138,11 @@ module ActiveRecord
# convert something to a boolean
def value_to_boolean(value)
TRUE_VALUES.include?(value)
if value.is_a?(String) && value.blank?
nil
else
TRUE_VALUES.include?(value)
end
end
# convert something to a BigDecimal

View file

@ -138,7 +138,7 @@ class BasicsTest < ActiveRecord::TestCase
if current_adapter?(:MysqlAdapter)
def test_read_attributes_before_type_cast_on_boolean
bool = Booleantest.create({ "value" => false })
assert_equal 0, bool.attributes_before_type_cast["value"]
assert_equal "0", bool.reload.attributes_before_type_cast["value"]
end
end
@ -1114,11 +1114,15 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_boolean
b_nil = Booleantest.create({ "value" => nil })
nil_id = b_nil.id
b_false = Booleantest.create({ "value" => false })
false_id = b_false.id
b_true = Booleantest.create({ "value" => true })
true_id = b_true.id
b_nil = Booleantest.find(nil_id)
assert_nil b_nil.value
b_false = Booleantest.find(false_id)
assert !b_false.value?
b_true = Booleantest.find(true_id)
@ -1126,11 +1130,15 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_boolean_cast_from_string
b_blank = Booleantest.create({ "value" => "" })
blank_id = b_blank.id
b_false = Booleantest.create({ "value" => "0" })
false_id = b_false.id
b_true = Booleantest.create({ "value" => "1" })
true_id = b_true.id
b_blank = Booleantest.find(blank_id)
assert_nil b_blank.value
b_false = Booleantest.find(false_id)
assert !b_false.value?
b_true = Booleantest.find(true_id)

View file

@ -1420,8 +1420,8 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase
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 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase
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

View file

@ -60,7 +60,7 @@ ActiveRecord::Schema.define do
end
create_table :booleantests, :force => true do |t|
t.integer :value
t.boolean :value
end
create_table :categories, :force => true do |t|