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.
This commit is contained in:
parent
07d3d40234
commit
a502703c3d
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user