diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index d9866a18..e5ad547e 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -109,9 +109,15 @@ module PaperTrail def record_update if switched_on? && changed_notably? - versions.build merge_metadata(:event => 'update', - :object => object_to_string(item_before_change), - :whodunnit => PaperTrail.whodunnit) + data = { + :event => 'update', + :object => object_to_string(item_before_change), + :whodunnit => PaperTrail.whodunnit + } + + data[:object_changes] = self.changes.to_yaml if Version.method_defined? :object_changes + + versions.build merge_metadata(data) end end diff --git a/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb b/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb index a6de6262..df02a716 100644 --- a/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +++ b/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb @@ -21,6 +21,7 @@ class SetUpTestTables < ActiveRecord::Migration t.string :event, :null => false t.string :whodunnit t.text :object + t.text :object_changes t.datetime :created_at # Metadata columns. @@ -34,7 +35,7 @@ class SetUpTestTables < ActiveRecord::Migration t.string :user_agent end add_index :versions, [:item_type, :item_id] - + create_table :post_versions, :force => true do |t| t.string :item_type, :null => false t.integer :item_id, :null => false diff --git a/test/dummy/db/test.sqlite3 b/test/dummy/db/test.sqlite3 index 3d65e32c..e499d270 100644 Binary files a/test/dummy/db/test.sqlite3 and b/test/dummy/db/test.sqlite3 differ diff --git a/test/unit/model_test.rb b/test/unit/model_test.rb index a05cbcbf..24a60e05 100644 --- a/test/unit/model_test.rb +++ b/test/unit/model_test.rb @@ -97,6 +97,10 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase assert @widget.versions.map(&:reify).compact.all? { |w| !w.live? } end + should 'have stored changes' do + assert_equal ({'name' => ['Henry', 'Harry']}), YAML::load(@widget.versions.last.object_changes) + end + if defined?(ActiveRecord::IdentityMap) && ActiveRecord::IdentityMap.respond_to?(:without) should 'not clobber the IdentityMap when reifying' do module ActiveRecord::IdentityMap @@ -118,7 +122,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase setup do @wotsit = @widget.create_wotsit :name => 'John' end - + should 'not copy the has_one association by default when reifying' do reified_widget = @widget.versions.last.reify assert_equal @wotsit, reified_widget.wotsit # association hasn't been affected by reifying