diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-06-03 08:23:30 -0600 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-06-03 10:27:38 -0600 |
commit | ed559d4b00fbd7c6f86e75fd2d18a40e16b98281 (patch) | |
tree | 87cfc9cd37c31c4c53c2437915db4da8e8019c46 /activerecord/test/cases | |
parent | 098bb63ae4acc349826ab84b3e1dff985e38609c (diff) | |
download | rails-ed559d4b00fbd7c6f86e75fd2d18a40e16b98281.tar.gz rails-ed559d4b00fbd7c6f86e75fd2d18a40e16b98281.tar.bz2 rails-ed559d4b00fbd7c6f86e75fd2d18a40e16b98281.zip |
Keep column defaults in type cast form
The contract of `_field_changed?` assumes that the old value is always
type cast. That is not the case for the value in `Column#default` as
things are today. It appears there are other public methods that
assume that `Column#default` is type cast, as well. The reason for this
change originally was because the value gets put into `@raw_attributes`
in initialize. This reverts to the old behavior on `Column`, and updates
`initialize` to make sure that the values are in the right format.
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/dirty_test.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index df4183c065..987c55ebc2 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -616,6 +616,34 @@ class DirtyTest < ActiveRecord::TestCase end end + test "defaults with type that implements `type_cast_for_write`" do + type = Class.new(ActiveRecord::Type::Value) do + def type_cast(value) + value.to_i + end + + def type_cast_for_write(value) + value.to_s + end + + alias type_cast_for_database type_cast_for_write + end + + model_class = Class.new(ActiveRecord::Base) do + self.table_name = 'numeric_data' + property :foo, type.new, default: 1 + end + + model = model_class.new + assert_not model.foo_changed? + + model = model_class.new(foo: 1) + assert_not model.foo_changed? + + model = model_class.new(foo: '1') + assert_not model.foo_changed? + end + private def with_partial_writes(klass, on = true) old = klass.partial_writes? |