aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type/boolean.rb
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-10-16 20:09:42 -0600
committerSean Griffin <sean@thoughtbot.com>2014-10-16 20:09:42 -0600
commite01a46f1f0e21d9018906a6a8dcfdae2d92f32ae (patch)
treeb9daeede0740a4a0f05707479adfd789776ea126 /activerecord/lib/active_record/type/boolean.rb
parent056d06627af1c99647d88521557c65ddc476520f (diff)
downloadrails-e01a46f1f0e21d9018906a6a8dcfdae2d92f32ae.tar.gz
rails-e01a46f1f0e21d9018906a6a8dcfdae2d92f32ae.tar.bz2
rails-e01a46f1f0e21d9018906a6a8dcfdae2d92f32ae.zip
Add a deprecation warning for abiguous boolean values
In Rails 5.0, we'd like to change the behavior of boolean columns in Rails to be closer to Ruby's semantics. Currently we have a small set of values which are "truthy", and all others are "falsy". In Rails 5.0, we will reverse this to have a small number of values which are "falsy", and all others will become "truthy". In the interim, all values which are ambiguous must emit a deprecation warning.
Diffstat (limited to 'activerecord/lib/active_record/type/boolean.rb')
-rw-r--r--activerecord/lib/active_record/type/boolean.rb9
1 files changed, 8 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/type/boolean.rb b/activerecord/lib/active_record/type/boolean.rb
index 06dd17ed28..1311be3944 100644
--- a/activerecord/lib/active_record/type/boolean.rb
+++ b/activerecord/lib/active_record/type/boolean.rb
@@ -10,8 +10,15 @@ module ActiveRecord
def cast_value(value)
if value == ''
nil
+ elsif ConnectionAdapters::Column::TRUE_VALUES.include?(value)
+ true
else
- ConnectionAdapters::Column::TRUE_VALUES.include?(value)
+ if !ConnectionAdapters::Column::FALSE_VALUES.include?(value)
+ ActiveSupport::Deprecation.warn(<<-EOM)
+ 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 sematics, and will cast to true in Rails 5.0. If you would like to maintain the current behavior, you should explicitly handle the values you would like cast to false.
+ EOM
+ end
+ false
end
end
end