Skip to content

Commit a502703

Browse files
committed
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.
1 parent 07d3d40 commit a502703

4 files changed

Lines changed: 16 additions & 19 deletions

File tree

activerecord/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
* Change the behavior of boolean columns to be closer to Ruby's semantics.
2+
3+
Before this change we had a small set of "truthy", and all others are "falsy".
4+
5+
Now, we have a small set of "falsy" values and all others are "truthy" matching
6+
Ruby's semantics.
7+
8+
*Rafael Mendonça França*
9+
110
* Deprecate `ActiveRecord::Base.errors_in_transactional_callbacks=`.
211

312
*Rafael Mendonça França*

activerecord/lib/active_record/connection_adapters/column.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ module ActiveRecord
55
module ConnectionAdapters
66
# An abstract definition of a column in a table.
77
class Column
8-
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set
98
FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].to_set
109

1110
module Format

activerecord/lib/active_record/type/boolean.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,10 @@ def type
1010
def cast_value(value)
1111
if value == ''
1212
nil
13-
elsif ConnectionAdapters::Column::TRUE_VALUES.include?(value)
14-
true
15-
else
16-
if !ConnectionAdapters::Column::FALSE_VALUES.include?(value)
17-
ActiveSupport::Deprecation.warn(<<-MSG.squish)
18-
You attempted to assign a value which is not explicitly `true` or `false`
19-
to a boolean column. Currently this value casts to `false`. This will
20-
change to match Ruby's semantics, and will cast to `true` in Rails 5.
21-
If you would like to maintain the current behavior, you should
22-
explicitly handle the values you would like cast to `false`.
23-
MSG
24-
end
13+
elsif ConnectionAdapters::Column::FALSE_VALUES.include?(value)
2514
false
15+
else
16+
true
2617
end
2718
end
2819
end

activerecord/test/cases/types_test.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def test_type_cast_boolean
1717
assert type.type_cast_from_user('TRUE')
1818
assert type.type_cast_from_user('on')
1919
assert type.type_cast_from_user('ON')
20+
assert type.type_cast_from_user(' ')
21+
assert type.type_cast_from_user("\u3000\r\n")
22+
assert type.type_cast_from_user("\u0000")
23+
assert type.type_cast_from_user('SOMETHING RANDOM')
2024

2125
# explicitly check for false vs nil
2226
assert_equal false, type.type_cast_from_user(false)
@@ -28,12 +32,6 @@ def test_type_cast_boolean
2832
assert_equal false, type.type_cast_from_user('FALSE')
2933
assert_equal false, type.type_cast_from_user('off')
3034
assert_equal false, type.type_cast_from_user('OFF')
31-
assert_deprecated do
32-
assert_equal false, type.type_cast_from_user(' ')
33-
assert_equal false, type.type_cast_from_user("\u3000\r\n")
34-
assert_equal false, type.type_cast_from_user("\u0000")
35-
assert_equal false, type.type_cast_from_user('SOMETHING RANDOM')
36-
end
3735
end
3836

3937
def test_type_cast_float

0 commit comments

Comments
 (0)