diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-08-27 11:06:22 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2014-08-27 11:06:22 -0300 |
commit | 77f5a706b985773caa84b5fb56624ec2fceb478d (patch) | |
tree | 1584bb59c174d480b63f1114a53511baa67f5eec /activerecord | |
parent | 17a39c841d7ee9508da2b06eec72206ad3510881 (diff) | |
parent | 990322b298c41b65be01238c56e2d7ff0dd76cb6 (diff) | |
download | rails-77f5a706b985773caa84b5fb56624ec2fceb478d.tar.gz rails-77f5a706b985773caa84b5fb56624ec2fceb478d.tar.bz2 rails-77f5a706b985773caa84b5fb56624ec2fceb478d.zip |
Merge pull request #16702 from sgrif/sg-binary-serialized
Correctly detect mutation on serialized columns mapping to binary
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/type/binary.rb | 10 | ||||
-rw-r--r-- | activerecord/lib/active_record/type/serialized.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/dirty_test.rb | 16 |
3 files changed, 31 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/type/binary.rb b/activerecord/lib/active_record/type/binary.rb index d29ff4e494..005a48ef0d 100644 --- a/activerecord/lib/active_record/type/binary.rb +++ b/activerecord/lib/active_record/type/binary.rb @@ -22,6 +22,11 @@ module ActiveRecord Data.new(super) end + def changed_in_place?(raw_old_value, value) + old_value = type_cast_from_database(raw_old_value) + old_value != value + end + class Data # :nodoc: def initialize(value) @value = value.to_s @@ -30,10 +35,15 @@ module ActiveRecord def to_s @value end + alias_method :to_str, :to_s def hex @value.unpack('H*')[0] end + + def ==(other) + other == to_s || super + end end end end diff --git a/activerecord/lib/active_record/type/serialized.rb b/activerecord/lib/active_record/type/serialized.rb index abeea769c4..5b512433b0 100644 --- a/activerecord/lib/active_record/type/serialized.rb +++ b/activerecord/lib/active_record/type/serialized.rb @@ -26,6 +26,11 @@ module ActiveRecord end end + def changed_in_place?(raw_old_value, value) + return false if value.nil? + subtype.changed_in_place?(raw_old_value, coder.dump(value)) + end + def accessor ActiveRecord::Store::IndifferentHashAccessor end diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index 0c77eedb52..5cb6b97117 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -682,6 +682,22 @@ class DirtyTest < ActiveRecord::TestCase assert_not pirate.changed? end + test "in place mutation for binary" do + klass = Class.new(ActiveRecord::Base) do + self.table_name = :binaries + serialize :data + end + + klass.create!(data: "foo") + binary = klass.first + + assert_not binary.changed? + + binary.data << "bar" + + assert binary.changed? + end + private def with_partial_writes(klass, on = true) old = klass.partial_writes? |