1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00

Merge remote-tracking branch 'bm5k/issues/43'

Updated README to reflect the impact of the changes being merged in.
This commit is contained in:
Ben Atkins 2012-10-16 16:49:48 -04:00
commit f5e0bbf488
3 changed files with 25 additions and 6 deletions

View file

@ -614,12 +614,14 @@ The best way to diff adjacent versions is to get PaperTrail to do it for you. I
```ruby
>> widget = Widget.create :name => 'Bob'
>> widget.versions.last.changeset # {}
>> widget.versions.last.changeset # {'name' => [nil, 'Bob']}
>> widget.update_attributes :name => 'Robert'
>> widget.versions.last.changeset # {'name' => ['Bob', 'Robert']}
>> widget.destroy
>> widget.versions.last.changeset # {}
```
Note PaperTrail only stores the changes for updates; there's no point storing them for created or destroyed objects.
Note PaperTrail only stores the changes for creation and updates; it doesn't store anything when an object is destroyed.
Please be aware that PaperTrail doesn't use diffs internally. When I designed PaperTrail I wanted simplicity and robustness so I decided to make each version of an object self-contained. A version stores all of its object's data, not a diff from the previous version. This means you can delete any version without affecting any other.

View file

@ -149,7 +149,17 @@ module PaperTrail
def record_create
if switched_on?
send(self.class.versions_association_name).create merge_metadata(:event => 'create', :whodunnit => PaperTrail.whodunnit)
data = {
:event => 'create',
:whodunnit => PaperTrail.whodunnit
}
if changed_notably? and version_class.column_names.include?('object_changes')
# The double negative (reject, !include?) preserves the hash structure of self.changes.
data[:object_changes] = self.changes.reject { |k, _| !notably_changed.include?(k) }.to_yaml
end
send(self.class.versions_association_name).create merge_metadata(data)
end
end

View file

@ -143,12 +143,19 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
assert @widget.live?
end
should 'not have changes' do
assert_equal Hash.new, @widget.versions.last.changeset
should 'have changes' do
changes = {
'name' => [nil, 'Henry'],
'created_at' => [nil, @widget.created_at],
'updated_at' => [nil, @widget.updated_at],
'id' => [nil, 1]
}
assert_equal changes, @widget.versions.last.changeset
end
context 'and then updated without any changes' do
setup { @widget.save }
setup { @widget.touch }
should 'not have a new version' do
assert_equal 1, @widget.versions.length