From a502703c3d2151d4d3b421b29fefdac5ad05df61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Sun, 4 Jan 2015 11:31:58 -0300 Subject: [PATCH] Change the behavior of boolean columns to be closer to Ruby's semantics. Before this change we had a small set of "truthy", and all others are "falsy". Now, we have a small set of "falsy" values and all others are "truthy" matching Ruby's semantics. --- activerecord/CHANGELOG.md | 9 +++++++++ .../active_record/connection_adapters/column.rb | 1 - activerecord/lib/active_record/type/boolean.rb | 15 +++------------ activerecord/test/cases/types_test.rb | 10 ++++------ 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 3a05a9b020..d1eee8e73e 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,12 @@ +* Change the behavior of boolean columns to be closer to Ruby's semantics. + + Before this change we had a small set of "truthy", and all others are "falsy". + + Now, we have a small set of "falsy" values and all others are "truthy" matching + Ruby's semantics. + + *Rafael Mendonça França* + * Deprecate `ActiveRecord::Base.errors_in_transactional_callbacks=`. *Rafael Mendonça França* diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index d7f999c3c6..e74de60a83 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -5,7 +5,6 @@ module ActiveRecord module ConnectionAdapters # An abstract definition of a column in a table. class Column - TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].to_set module Format diff --git a/activerecord/lib/active_record/type/boolean.rb b/activerecord/lib/active_record/type/boolean.rb index 978d16d524..2e24afc7c0 100644 --- a/activerecord/lib/active_record/type/boolean.rb +++ b/activerecord/lib/active_record/type/boolean.rb @@ -10,19 +10,10 @@ def type def cast_value(value) if value == '' nil - elsif ConnectionAdapters::Column::TRUE_VALUES.include?(value) - true - else - if !ConnectionAdapters::Column::FALSE_VALUES.include?(value) - ActiveSupport::Deprecation.warn(<<-MSG.squish) - You attempted to assign a value which is not explicitly `true` or `false` - to a boolean column. Currently this value casts to `false`. This will - change to match Ruby's semantics, and will cast to `true` in Rails 5. - If you would like to maintain the current behavior, you should - explicitly handle the values you would like cast to `false`. - MSG - end + elsif ConnectionAdapters::Column::FALSE_VALUES.include?(value) false + else + true end end end diff --git a/activerecord/test/cases/types_test.rb b/activerecord/test/cases/types_test.rb index b0979cbe1f..73e92addfe 100644 --- a/activerecord/test/cases/types_test.rb +++ b/activerecord/test/cases/types_test.rb @@ -17,6 +17,10 @@ def test_type_cast_boolean assert type.type_cast_from_user('TRUE') assert type.type_cast_from_user('on') assert type.type_cast_from_user('ON') + assert type.type_cast_from_user(' ') + assert type.type_cast_from_user("\u3000\r\n") + assert type.type_cast_from_user("\u0000") + assert type.type_cast_from_user('SOMETHING RANDOM') # explicitly check for false vs nil assert_equal false, type.type_cast_from_user(false) @@ -28,12 +32,6 @@ def test_type_cast_boolean assert_equal false, type.type_cast_from_user('FALSE') assert_equal false, type.type_cast_from_user('off') assert_equal false, type.type_cast_from_user('OFF') - assert_deprecated do - assert_equal false, type.type_cast_from_user(' ') - assert_equal false, type.type_cast_from_user("\u3000\r\n") - assert_equal false, type.type_cast_from_user("\u0000") - assert_equal false, type.type_cast_from_user('SOMETHING RANDOM') - end end def test_type_cast_float -- GitLab