diff --git a/lib/acts_as_versioned.rb b/lib/acts_as_versioned.rb index 6e5ef5d..87e6fc0 100644 --- a/lib/acts_as_versioned.rb +++ b/lib/acts_as_versioned.rb @@ -392,10 +392,12 @@ def next_version def clear_changed_attributes self.changed_attributes = [] end - + def write_changed_attribute(attr_name, attr_value) - (self.changed_attributes ||= []) << attr_name.to_s unless self.changed?(attr_name) or self.send(attr_name) == attr_value - write_attribute(attr_name.to_s, attr_value) + # Convert to db type for comparison. Avoids failing Float<=>String comparisons. + attr_value_for_db = self.class.columns_hash[attr_name.to_s].type_cast(attr_value) + (self.changed_attributes ||= []) << attr_name.to_s unless self.changed?(attr_name) || self.send(attr_name) == attr_value_for_db + write_attribute(attr_name, attr_value_for_db) end private diff --git a/test/schema.rb b/test/schema.rb index 7ae09bc..7d5153d 100644 --- a/test/schema.rb +++ b/test/schema.rb @@ -50,4 +50,19 @@ t.column :version, :integer t.column :updated_at, :datetime end -end \ No newline at end of file + + create_table :landmarks, :force => true do |t| + t.column :name, :string + t.column :latitude, :float + t.column :longitude, :float + t.column :version, :integer + end + + create_table :landmark_versions, :force => true do |t| + t.column :landmark_id, :integer + t.column :name, :string + t.column :latitude, :float + t.column :longitude, :float + t.column :version, :integer + end +end diff --git a/test/versioned_test.rb b/test/versioned_test.rb index 7259ffb..c1e1a4b 100644 --- a/test/versioned_test.rb +++ b/test/versioned_test.rb @@ -3,7 +3,7 @@ require File.join(File.dirname(__FILE__), 'fixtures/widget') class VersionedTest < Test::Unit::TestCase - fixtures :pages, :page_versions, :locked_pages, :locked_pages_revisions, :authors + fixtures :pages, :page_versions, :locked_pages, :locked_pages_revisions, :authors, :landmarks, :landmark_versions def test_saves_versioned_copy p = Page.create :title => 'first title', :body => 'first body' @@ -300,4 +300,14 @@ def test_versioned_records_should_belong_to_parent page_version = page.versions.last assert_equal page, page_version.page end + + def test_unchanged_attributes + landmarks(:washington).attributes = landmarks(:washington).attributes + assert !landmarks(:washington).changed? + end + + def test_unchanged_string_attributes + landmarks(:washington).attributes = landmarks(:washington).attributes.inject({}) { |params, (key, value)| params.update key => value.to_s } + assert !landmarks(:washington).changed? + end end