From 7666f84f22cacfa193e9444c2264ffda8c7a8210 Mon Sep 17 00:00:00 2001 From: Stephen H. Gerstacker Date: Tue, 13 Jan 2009 20:10:03 -0500 Subject: [PATCH] Fixed bug with decimals of 0.0 being set as '' not counting as a change --- activerecord/lib/active_record/dirty.rb | 6 +++--- activerecord/test/cases/dirty_test.rb | 27 +++++++++++++++++++++++++++ activerecord/test/schema/schema.rb | 1 + 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/activerecord/lib/active_record/dirty.rb b/activerecord/lib/active_record/dirty.rb index 4c899f5..a2eddaf 100644 --- a/activerecord/lib/active_record/dirty.rb +++ b/activerecord/lib/active_record/dirty.rb @@ -151,11 +151,11 @@ module ActiveRecord def field_changed?(attr, old, value) if column = column_for_attribute(attr) - if column.type == :integer && column.null && (old.nil? || old == 0) && value.blank? - # For nullable integer columns, NULL gets stored in database for blank (i.e. '') values. + if (column.type == :integer || column.type == :decimal) && column.null && (old.nil? || old == 0) && value.blank? + # For nullable integer and decimal columns, NULL gets stored in database for blank (i.e. '') values. # Hence we don't record it as a change if the value changes from nil to ''. # If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll - # be typecast back to 0 (''.to_i => 0) + # be typecast back to 0 (''.to_i => 0; ''.to_f => 0.0) value = nil else value = column.type_cast(value) diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index 10cdbdc..6d4bf34 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -107,6 +107,33 @@ class DirtyTest < ActiveRecord::TestCase assert_equal([0, nil], pirate.parrot_id_change) end + def test_decimal_zero_to_blank_marked_as_changed + pirate = Pirate.new + pirate.catchphrase = "Yarrrr, me hearties" + pirate.legs = 1.0 + pirate.save + + # check the change from 1 to '' + pirate = Pirate.find_by_catchphrase("Yarrrr, me hearties") + pirate.legs = '' + assert pirate.legs_changed? + assert_equal([1.0, nil], pirate.legs_change) + pirate.save + + # check the change from nil to 0 + pirate = Pirate.find_by_catchphrase("Yarrrr, me hearties") + pirate.legs = 0.0 + assert pirate.legs_changed? + assert_equal([nil, 0.0], pirate.legs_change) + pirate.save + + # check the change from 0 to '' + pirate = Pirate.find_by_catchphrase("Yarrrr, me hearties") + pirate.legs = '' + assert pirate.legs_changed? + assert_equal([0.0, nil], pirate.legs_change) + end + def test_object_should_be_changed_if_any_attribute_is_changed pirate = Pirate.new assert !pirate.changed? diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 8199cb8..6258827 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -312,6 +312,7 @@ ActiveRecord::Schema.define do create_table :pirates, :force => true do |t| t.column :catchphrase, :string t.column :parrot_id, :integer + t.column :legs, :decimal, :precision => 2, :scale => 1 t.column :created_on, :datetime t.column :updated_on, :datetime end -- 1.6.1