Add object_changes field for storage changes

This commit is contained in:
Edward Tsech 2011-07-12 23:22:04 +07:00
parent 16585a4f47
commit 3ceed94ec1
4 changed files with 16 additions and 5 deletions

View File

@ -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

View File

@ -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

Binary file not shown.

View File

@ -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