Permalink
Browse files

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.
  • Loading branch information...
rafaelfranca committed Jan 4, 2015
1 parent 07d3d40 commit a502703c3d2151d4d3b421b29fefdac5ad05df61
@@ -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*
@@ -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
@@ -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
@@ -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

3 comments on commit a502703

@connorshea

This comment has been minimized.

Show comment
Hide comment
@connorshea

connorshea Sep 23, 2016

Contributor

@rafaelfranca it's a bit late to ask, but would it be acceptable to add a note about the removal of ConnectionAdapters::Column::TRUE_VALUES in the Changelog? I realize no one was intended to depend on that, but apparently we did at GitLab and that caused a bit of headache. Would have been nice to not have to search as hard to find this commit.

Contributor

connorshea replied Sep 23, 2016

@rafaelfranca it's a bit late to ask, but would it be acceptable to add a note about the removal of ConnectionAdapters::Column::TRUE_VALUES in the Changelog? I realize no one was intended to depend on that, but apparently we did at GitLab and that caused a bit of headache. Would have been nice to not have to search as hard to find this commit.

@arthurnn

This comment has been minimized.

Show comment
Hide comment
@arthurnn

arthurnn Sep 24, 2016

Member

@connorshea As far as I can tell, the TRUE_VALUES constant was public. So yeah, we could add a note to the changelog. WDYT @rafaelfranca ?

Member

arthurnn replied Sep 24, 2016

@connorshea As far as I can tell, the TRUE_VALUES constant was public. So yeah, we could add a note to the changelog. WDYT @rafaelfranca ?

@connorshea

This comment has been minimized.

Show comment
Hide comment
@connorshea

connorshea Sep 25, 2016

Contributor

Added in #26619, thanks @arthurnn :)

Contributor

connorshea replied Sep 25, 2016

Added in #26619, thanks @arthurnn :)

Please sign in to comment.