paper_trail 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +26 -20
- data/lib/generators/paper_trail/install_generator.rb +18 -0
- data/lib/paper_trail/has_paper_trail.rb +11 -3
- data/lib/paper_trail/version.rb +1 -1
- data/lib/paper_trail/version_number.rb +1 -1
- data/test/dummy/app/models/article.rb +2 -1
- data/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +1 -0
- data/test/dummy/db/schema.rb +1 -0
- data/test/dummy/db/test.sqlite3 +0 -0
- data/test/unit/model_test.rb +12 -1
- metadata +5 -5
- data/lib/generators/paper_trail/paper_trail_generator.rb +0 -24
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -7,7 +7,7 @@ PaperTrail lets you track changes to your models' data. It's good for auditing
|
|
7
7
|
|
8
8
|
* Stores every create, update and destroy.
|
9
9
|
* Does not store updates which don't change anything.
|
10
|
-
*
|
10
|
+
* Allows you to specify attributes (by inclusion or exclusion) which must change for a Version to be stored.
|
11
11
|
* Allows you to get at every version, including the original, even once destroyed.
|
12
12
|
* Allows you to get at every version even if the schema has since changed.
|
13
13
|
* Allows you to get at the version as of a particular time.
|
@@ -26,7 +26,7 @@ PaperTrail lets you track changes to your models' data. It's good for auditing
|
|
26
26
|
|
27
27
|
## Rails Version
|
28
28
|
|
29
|
-
Works on Rails 3 and Rails 2.3. The Rails 3 code is on the master branch and tagged v2.x
|
29
|
+
Works on Rails 3 and Rails 2.3. The Rails 3 code is on the `master` branch and tagged `v2.x`. The Rails 2.3 code is on the `rails2` branch and tagged `v1.x`.
|
30
30
|
|
31
31
|
|
32
32
|
## API Summary
|
@@ -164,7 +164,7 @@ Here's a helpful table showing what PaperTrail stores:
|
|
164
164
|
PaperTrail stores the values in the Model Before column. Most other auditing/versioning plugins store the After column.
|
165
165
|
|
166
166
|
|
167
|
-
##
|
167
|
+
## Choosing Attributes To Monitor
|
168
168
|
|
169
169
|
You can ignore changes to certain attributes like this:
|
170
170
|
|
@@ -182,6 +182,23 @@ This means that changes to just the `title` or `rating` will not store another v
|
|
182
182
|
>> a.versions.length # 2
|
183
183
|
>> a.versions.last.reify.title # 'My Title'
|
184
184
|
|
185
|
+
Or, you can specify a list of all attributes you care about:
|
186
|
+
|
187
|
+
class Article < ActiveRecord::Base
|
188
|
+
has_paper_trail :only => [:title]
|
189
|
+
end
|
190
|
+
|
191
|
+
This means that only changes to the `title` will save a version of the article:
|
192
|
+
|
193
|
+
>> a = Article.create
|
194
|
+
>> a.versions.length # 1
|
195
|
+
>> a.update_attributes :title => 'My Title'
|
196
|
+
>> a.versions.length # 2
|
197
|
+
>> a.update_attributes :content => 'Hello'
|
198
|
+
>> a.versions.length # 2
|
199
|
+
|
200
|
+
Passing both `:ignore` and `:only` options will result in the article being saved if a changed attribute is included in `:only` but not in `:ignore`.
|
201
|
+
|
185
202
|
|
186
203
|
## Reverting And Undeleting A Model
|
187
204
|
|
@@ -494,29 +511,17 @@ Over time your `versions` table will grow to an unwieldy size. Because each ver
|
|
494
511
|
|
495
512
|
2. Generate a migration which will add a `versions` table to your database.
|
496
513
|
|
497
|
-
`rails generate paper_trail`
|
514
|
+
`bundle exec rails generate paper_trail`
|
498
515
|
|
499
516
|
3. Run the migration.
|
500
517
|
|
501
|
-
`rake db:migrate`
|
518
|
+
`bundle exec rake db:migrate`
|
502
519
|
|
503
520
|
4. Add `has_paper_trail` to the models you want to track.
|
504
521
|
|
505
522
|
### Rails 2
|
506
523
|
|
507
|
-
|
508
|
-
|
509
|
-
`config.gem 'paper_trail', :version => '~> 1'`
|
510
|
-
|
511
|
-
2. Generate a migration which will add a `versions` table to your database.
|
512
|
-
|
513
|
-
`script/generate paper_trail`
|
514
|
-
|
515
|
-
3. Run the migration.
|
516
|
-
|
517
|
-
`rake db:migrate`
|
518
|
-
|
519
|
-
4. Add `has_paper_trail` to the models you want to track.
|
524
|
+
Please see the `rails2` branch.
|
520
525
|
|
521
526
|
|
522
527
|
## Testing
|
@@ -547,7 +552,8 @@ Many thanks to:
|
|
547
552
|
* Danny Trelogan
|
548
553
|
* [Mikl Kurkov](http://github.com/mkurkov)
|
549
554
|
* [Franco Catena](https://github.com/francocatena)
|
550
|
-
* [Emmanuel](https://github.com/emmanuel)
|
555
|
+
* [Emmanuel Gomez](https://github.com/emmanuel)
|
556
|
+
* [Matthew MacLeod](https://github.com/mattmacleod)
|
551
557
|
|
552
558
|
|
553
559
|
## Inspirations
|
@@ -558,5 +564,5 @@ Many thanks to:
|
|
558
564
|
|
559
565
|
## Intellectual Property
|
560
566
|
|
561
|
-
Copyright (c)
|
567
|
+
Copyright (c) 2011 Andy Stewart (boss@airbladesoftware.com).
|
562
568
|
Released under the MIT licence.
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
require 'rails/generators/active_record/migration'
|
4
|
+
|
5
|
+
module PaperTrail
|
6
|
+
class InstallGenerator < Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
8
|
+
extend ActiveRecord::Generators::Migration
|
9
|
+
|
10
|
+
source_root File.expand_path('../templates', __FILE__)
|
11
|
+
|
12
|
+
desc 'Generates (but does not run) a migration to add a versions table.'
|
13
|
+
|
14
|
+
def create_migration_file
|
15
|
+
migration_template 'create_versions.rb', 'db/migrate/create_versions.rb'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -12,6 +12,7 @@ module PaperTrail
|
|
12
12
|
#
|
13
13
|
# Options:
|
14
14
|
# :ignore an array of attributes for which a new `Version` will not be created if only they change.
|
15
|
+
# :only inverse of `ignore` - a new `Version` will be created only for these attributes if supplied
|
15
16
|
# :meta a hash of extra data to store. You must add a column to the `versions` table for each key.
|
16
17
|
# Values are objects or procs (which are called with `self`, i.e. the model with the paper
|
17
18
|
# trail). See `PaperTrail::Controller.info_for_paper_trail` for how to store data from
|
@@ -25,8 +26,11 @@ module PaperTrail
|
|
25
26
|
attr_accessor :version
|
26
27
|
|
27
28
|
cattr_accessor :ignore
|
28
|
-
self.ignore = (options[:ignore] || []).map &:to_s
|
29
|
+
self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s
|
29
30
|
|
31
|
+
cattr_accessor :only
|
32
|
+
self.only = ([options[:only]].flatten.compact || []).map &:to_s
|
33
|
+
|
30
34
|
cattr_accessor :meta
|
31
35
|
self.meta = options[:meta] || {}
|
32
36
|
|
@@ -68,11 +72,11 @@ module PaperTrail
|
|
68
72
|
end
|
69
73
|
|
70
74
|
# Returns the object (not a Version) as it was at the given timestamp.
|
71
|
-
def version_at(timestamp)
|
75
|
+
def version_at(timestamp, reify_options={})
|
72
76
|
# Because a version stores how its object looked *before* the change,
|
73
77
|
# we need to look for the first version created *after* the timestamp.
|
74
78
|
version = versions.after(timestamp).first
|
75
|
-
version ? version.reify : self
|
79
|
+
version ? version.reify(reify_options) : self
|
76
80
|
end
|
77
81
|
|
78
82
|
# Returns the object (not a Version) as it was most recently.
|
@@ -147,6 +151,10 @@ module PaperTrail
|
|
147
151
|
end
|
148
152
|
|
149
153
|
def notably_changed
|
154
|
+
self.class.only.empty? ? changed_and_not_ignored : (changed_and_not_ignored & self.class.only)
|
155
|
+
end
|
156
|
+
|
157
|
+
def changed_and_not_ignored
|
150
158
|
changed - self.class.ignore
|
151
159
|
end
|
152
160
|
|
data/lib/paper_trail/version.rb
CHANGED
data/test/dummy/db/schema.rb
CHANGED
data/test/dummy/db/test.sqlite3
CHANGED
Binary file
|
data/test/unit/model_test.rb
CHANGED
@@ -10,10 +10,21 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
|
10
10
|
should_not_change('the number of versions') { Version.count }
|
11
11
|
end
|
12
12
|
|
13
|
-
context 'which updates an ignored column and a
|
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
16
|
end
|
17
|
+
|
18
|
+
context 'which updates a selected column' do
|
19
|
+
setup { @article.update_attributes :content => 'Some text here.' }
|
20
|
+
should_change('the number of versions', :by => 1) { Version.count }
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'which updates a non-ignored and non-selected column' do
|
24
|
+
setup { @article.update_attributes :abstract => 'Other abstract'}
|
25
|
+
should_not_change('the number of versions') { Version.count }
|
26
|
+
end
|
27
|
+
|
17
28
|
end
|
18
29
|
|
19
30
|
|
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: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 2.0.
|
9
|
+
- 1
|
10
|
+
version: 2.0.1
|
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-02-
|
18
|
+
date: 2011-02-25 00:00:00 +00:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -95,7 +95,7 @@ files:
|
|
95
95
|
- README.md
|
96
96
|
- Rakefile
|
97
97
|
- lib/generators/paper_trail/USAGE
|
98
|
-
- lib/generators/paper_trail/
|
98
|
+
- lib/generators/paper_trail/install_generator.rb
|
99
99
|
- lib/generators/paper_trail/templates/create_versions.rb
|
100
100
|
- lib/paper_trail.rb
|
101
101
|
- lib/paper_trail/config.rb
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'rails/generators/named_base'
|
2
|
-
require 'rails/generators/migration'
|
3
|
-
|
4
|
-
class PaperTrailGenerator < Rails::Generators::NamedBase
|
5
|
-
include Rails::Generators::Migration
|
6
|
-
|
7
|
-
desc "Generates (but does not run) a migration to add a versions table."
|
8
|
-
source_root File.expand_path('../templates', __FILE__)
|
9
|
-
argument :name, :type => :string, :default => "create_versions"
|
10
|
-
|
11
|
-
# Implement the required interface for Rails::Generators::Migration.
|
12
|
-
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
|
13
|
-
def self.next_migration_number(dirname)
|
14
|
-
if ActiveRecord::Base.timestamped_migrations
|
15
|
-
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
16
|
-
else
|
17
|
-
"%.3d" % (current_migration_number(dirname) + 1)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def create_migration_file
|
22
|
-
migration_template 'create_versions.rb', 'db/migrate/create_versions.rb'
|
23
|
-
end
|
24
|
-
end
|