paper_trail 2.2.7 → 2.2.8
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +7 -7
- data/lib/paper_trail/has_paper_trail.rb +6 -1
- data/lib/paper_trail/version.rb +8 -1
- data/lib/paper_trail/version_number.rb +1 -1
- data/test/unit/model_test.rb +10 -2
- metadata +4 -4
data/README.md
CHANGED
@@ -348,7 +348,7 @@ PaperTrail can restore `:has_one` associations as they were at (actually, 3 seco
|
|
348
348
|
|
349
349
|
>> treasure.update_attributes :amount => 153
|
350
350
|
>> treasure.location.update_attributes :latitude => 54.321
|
351
|
-
|
351
|
+
|
352
352
|
>> t = treasure.versions.last.reify(:has_one => true)
|
353
353
|
>> t.amount # 100
|
354
354
|
>> t.location.latitude # 12.345
|
@@ -374,13 +374,13 @@ Given these models:
|
|
374
374
|
has_many :authors, :through => :authorships, :source => :person
|
375
375
|
has_paper_trail
|
376
376
|
end
|
377
|
-
|
377
|
+
|
378
378
|
class Authorship < ActiveRecord::Base
|
379
379
|
belongs_to :book
|
380
380
|
belongs_to :person
|
381
381
|
has_paper_trail # NOTE
|
382
382
|
end
|
383
|
-
|
383
|
+
|
384
384
|
class Person < ActiveRecord::Base
|
385
385
|
has_many :authorships, :dependent => :destroy
|
386
386
|
has_many :books, :through => :authorships
|
@@ -403,7 +403,7 @@ But none of these will:
|
|
403
403
|
Having said that, you can apparently get all these working (I haven't tested it myself) with this [monkey patch](http://stackoverflow.com/questions/2381033/how-to-create-a-full-audit-log-in-rails-for-every-table/2381411#2381411):
|
404
404
|
|
405
405
|
# In config/initializers/core_extensions.rb or lib/core_extensions.rb
|
406
|
-
ActiveRecord::Associations::HasManyThroughAssociation.class_eval do
|
406
|
+
ActiveRecord::Associations::HasManyThroughAssociation.class_eval do
|
407
407
|
def delete_records(records)
|
408
408
|
klass = @reflection.through_reflection.klass
|
409
409
|
records.each do |associate|
|
@@ -413,7 +413,7 @@ Having said that, you can apparently get all these working (I haven't tested it
|
|
413
413
|
end
|
414
414
|
|
415
415
|
The difference is the call to `destroy_all` instead of `delete_all` in [the original](http://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/has_many_through_association.rb#L76-81).
|
416
|
-
|
416
|
+
|
417
417
|
|
418
418
|
There may be a way to store authorship versions, probably using association callbacks, no matter how the collection is manipulated but I haven't found it yet. Let me know if you do.
|
419
419
|
|
@@ -455,10 +455,10 @@ Remember to add those extra columns to your `versions` table ;)
|
|
455
455
|
|
456
456
|
There are two scenarios: diffing adjacent versions and diffing non-adjacent versions.
|
457
457
|
|
458
|
-
The best way to diff adjacent versions is to get PaperTrail to do it for you. If you add an `object_changes` text column to your `versions` table, either at installation time with the `--with-changes` option or manually, PaperTrail will store the `changes` diff in each `update` version. You can use the `version.changeset` method to retrieve it. For example:
|
458
|
+
The best way to diff adjacent versions is to get PaperTrail to do it for you. If you add an `object_changes` text column to your `versions` table, either at installation time with the `--with-changes` option or manually, PaperTrail will store the `changes` diff (excluding any attributes PaperTrail is ignoring) in each `update` version. You can use the `version.changeset` method to retrieve it. For example:
|
459
459
|
|
460
460
|
>> widget = Widget.create :name => 'Bob'
|
461
|
-
>> widget.versions.last.changeset #
|
461
|
+
>> widget.versions.last.changeset # {}
|
462
462
|
>> widget.update_attributes :name => 'Robert'
|
463
463
|
>> widget.versions.last.changeset # {'name' => ['Bob', 'Robert']}
|
464
464
|
|
@@ -114,7 +114,12 @@ module PaperTrail
|
|
114
114
|
:object => object_to_string(item_before_change),
|
115
115
|
:whodunnit => PaperTrail.whodunnit
|
116
116
|
}
|
117
|
-
|
117
|
+
if Version.method_defined? :object_changes
|
118
|
+
# The double negative (reject, !include?) preserves the hash structure of self.changes.
|
119
|
+
data[:object_changes] = self.changes.reject do |key, value|
|
120
|
+
!notably_changed.include?(key)
|
121
|
+
end.to_yaml
|
122
|
+
end
|
118
123
|
versions.build merge_metadata(data)
|
119
124
|
end
|
120
125
|
end
|
data/lib/paper_trail/version.rb
CHANGED
@@ -80,8 +80,15 @@ class Version < ActiveRecord::Base
|
|
80
80
|
end
|
81
81
|
|
82
82
|
# Returns what changed in this version of the item. Cf. `ActiveModel::Dirty#changes`.
|
83
|
+
# Returns nil if your `versions` table does not have an `object_changes` text column.
|
83
84
|
def changeset
|
84
|
-
|
85
|
+
if Version.method_defined?(:object_changes)
|
86
|
+
if changes = object_changes
|
87
|
+
YAML::load(changes)
|
88
|
+
else
|
89
|
+
{}
|
90
|
+
end
|
91
|
+
end
|
85
92
|
end
|
86
93
|
|
87
94
|
# Returns who put the item into the state stored in this version.
|
data/test/unit/model_test.rb
CHANGED
@@ -13,6 +13,10 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
13
13
|
context 'which updates an ignored column and a selected column' do
|
14
14
|
setup { @article.update_attributes :title => 'My first title', :content => 'Some text here.' }
|
15
15
|
should_change('the number of versions', :by => 1) { Version.count }
|
16
|
+
|
17
|
+
should 'have stored only non-ignored attributes' do
|
18
|
+
assert_equal ({'content' => [nil, 'Some text here.']}), @article.versions.last.changeset
|
19
|
+
end
|
16
20
|
end
|
17
21
|
|
18
22
|
context 'which updates a selected column' do
|
@@ -61,7 +65,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
61
65
|
end
|
62
66
|
|
63
67
|
should 'should not have changes' do
|
64
|
-
|
68
|
+
assert_equal Hash.new, @widget.versions.last.changeset
|
65
69
|
end
|
66
70
|
|
67
71
|
context 'and then updated without any changes' do
|
@@ -105,7 +109,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
105
109
|
assert_equal ({'name' => ['Henry', 'Harry']}), @widget.versions.last.changeset
|
106
110
|
end
|
107
111
|
|
108
|
-
should
|
112
|
+
should "not have stored changes if object_changes column doesn't exist" do
|
109
113
|
remove_object_changes_column
|
110
114
|
Version.reset_column_information
|
111
115
|
assert_nil @widget.versions.last.changeset
|
@@ -192,6 +196,10 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
192
196
|
@reified_widget.save
|
193
197
|
assert_equal 1, @reified_widget.fluxors.length
|
194
198
|
end
|
199
|
+
|
200
|
+
should 'should not have changes' do
|
201
|
+
assert_equal Hash.new, @widget.versions.last.changeset
|
202
|
+
end
|
195
203
|
end
|
196
204
|
end
|
197
205
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paper_trail
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 2.2.
|
9
|
+
- 8
|
10
|
+
version: 2.2.8
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Andy Stewart
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-07-
|
18
|
+
date: 2011-07-14 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|