From e48e77e0222292176cd9f68658dd54524f582d9b Mon Sep 17 00:00:00 2001 From: Josh Susser Date: Mon, 18 Aug 2008 15:56:37 -0700 Subject: [PATCH] coerce blank strings to nil values for boolean and integer fields [#860 state:resolved] --- activerecord/lib/active_record/base.rb | 13 ++++++++----- .../abstract/schema_definitions.rb | 6 +++++- activerecord/test/cases/base_test.rb | 10 +++++++++- activerecord/test/cases/validations_test.rb | 8 ++++---- activerecord/test/schema/schema.rb | 2 +- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 45d9372842..2e139c5cc0 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -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 diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 98016ddab5..75032efe57 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -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 diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 36d30ade5e..730d2a89bd 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -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) diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index 4b2d28c80b..a40bda2533 100644 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -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 diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 487a08f4ab..ab5c7c520b 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -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|