paper_trail_without_deprecated 3.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.rspec +3 -0
- data/.travis.yml +21 -0
- data/CHANGELOG.md +68 -0
- data/Gemfile +2 -0
- data/MIT-LICENSE +20 -0
- data/README.md +979 -0
- data/Rakefile +18 -0
- data/gemfiles/3.0.gemfile +31 -0
- data/lib/generators/paper_trail/USAGE +2 -0
- data/lib/generators/paper_trail/install_generator.rb +23 -0
- data/lib/generators/paper_trail/templates/add_object_changes_column_to_versions.rb +9 -0
- data/lib/generators/paper_trail/templates/create_versions.rb +18 -0
- data/lib/paper_trail.rb +115 -0
- data/lib/paper_trail/cleaner.rb +34 -0
- data/lib/paper_trail/config.rb +14 -0
- data/lib/paper_trail/frameworks/cucumber.rb +31 -0
- data/lib/paper_trail/frameworks/rails.rb +79 -0
- data/lib/paper_trail/frameworks/rspec.rb +24 -0
- data/lib/paper_trail/frameworks/rspec/extensions.rb +20 -0
- data/lib/paper_trail/frameworks/sinatra.rb +31 -0
- data/lib/paper_trail/has_paper_trail.rb +308 -0
- data/lib/paper_trail/serializers/json.rb +17 -0
- data/lib/paper_trail/serializers/yaml.rb +17 -0
- data/lib/paper_trail/version.rb +200 -0
- data/lib/paper_trail/version_number.rb +3 -0
- data/paper_trail.gemspec +36 -0
- data/spec/models/widget_spec.rb +13 -0
- data/spec/paper_trail_spec.rb +47 -0
- data/spec/spec_helper.rb +41 -0
- data/test/custom_json_serializer.rb +13 -0
- data/test/dummy/Rakefile +7 -0
- data/test/dummy/app/controllers/application_controller.rb +17 -0
- data/test/dummy/app/controllers/test_controller.rb +5 -0
- data/test/dummy/app/controllers/widgets_controller.rb +31 -0
- data/test/dummy/app/helpers/application_helper.rb +2 -0
- data/test/dummy/app/models/animal.rb +4 -0
- data/test/dummy/app/models/article.rb +16 -0
- data/test/dummy/app/models/authorship.rb +5 -0
- data/test/dummy/app/models/book.rb +5 -0
- data/test/dummy/app/models/cat.rb +2 -0
- data/test/dummy/app/models/document.rb +4 -0
- data/test/dummy/app/models/dog.rb +2 -0
- data/test/dummy/app/models/elephant.rb +3 -0
- data/test/dummy/app/models/fluxor.rb +3 -0
- data/test/dummy/app/models/foo_widget.rb +2 -0
- data/test/dummy/app/models/legacy_widget.rb +4 -0
- data/test/dummy/app/models/person.rb +28 -0
- data/test/dummy/app/models/post.rb +4 -0
- data/test/dummy/app/models/protected_widget.rb +3 -0
- data/test/dummy/app/models/song.rb +12 -0
- data/test/dummy/app/models/translation.rb +4 -0
- data/test/dummy/app/models/widget.rb +10 -0
- data/test/dummy/app/models/wotsit.rb +4 -0
- data/test/dummy/app/versions/post_version.rb +3 -0
- data/test/dummy/app/views/layouts/application.html.erb +14 -0
- data/test/dummy/config.ru +4 -0
- data/test/dummy/config/application.rb +63 -0
- data/test/dummy/config/boot.rb +10 -0
- data/test/dummy/config/database.yml +22 -0
- data/test/dummy/config/environment.rb +5 -0
- data/test/dummy/config/environments/development.rb +40 -0
- data/test/dummy/config/environments/production.rb +73 -0
- data/test/dummy/config/environments/test.rb +37 -0
- data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/test/dummy/config/initializers/inflections.rb +10 -0
- data/test/dummy/config/initializers/mime_types.rb +5 -0
- data/test/dummy/config/initializers/paper_trail.rb +5 -0
- data/test/dummy/config/initializers/secret_token.rb +7 -0
- data/test/dummy/config/initializers/session_store.rb +8 -0
- data/test/dummy/config/locales/en.yml +5 -0
- data/test/dummy/config/routes.rb +3 -0
- data/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +136 -0
- data/test/dummy/db/schema.rb +101 -0
- data/test/dummy/public/404.html +26 -0
- data/test/dummy/public/422.html +26 -0
- data/test/dummy/public/500.html +26 -0
- data/test/dummy/public/favicon.ico +0 -0
- data/test/dummy/public/javascripts/application.js +2 -0
- data/test/dummy/public/javascripts/controls.js +965 -0
- data/test/dummy/public/javascripts/dragdrop.js +974 -0
- data/test/dummy/public/javascripts/effects.js +1123 -0
- data/test/dummy/public/javascripts/prototype.js +6001 -0
- data/test/dummy/public/javascripts/rails.js +175 -0
- data/test/dummy/public/stylesheets/.gitkeep +0 -0
- data/test/dummy/script/rails +6 -0
- data/test/functional/controller_test.rb +90 -0
- data/test/functional/modular_sinatra_test.rb +44 -0
- data/test/functional/sinatra_test.rb +45 -0
- data/test/functional/thread_safety_test.rb +26 -0
- data/test/paper_trail_test.rb +27 -0
- data/test/test_helper.rb +40 -0
- data/test/unit/cleaner_test.rb +143 -0
- data/test/unit/inheritance_column_test.rb +43 -0
- data/test/unit/model_test.rb +1314 -0
- data/test/unit/protected_attrs_test.rb +46 -0
- data/test/unit/serializer_test.rb +117 -0
- data/test/unit/serializers/json_test.rb +40 -0
- data/test/unit/serializers/mixin_json_test.rb +36 -0
- data/test/unit/serializers/mixin_yaml_test.rb +49 -0
- data/test/unit/serializers/yaml_test.rb +40 -0
- data/test/unit/timestamp_test.rb +44 -0
- data/test/unit/version_test.rb +74 -0
- metadata +286 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b3e3e9ebacc10e3441e46d8795d5bb8e9fe0d480
|
4
|
+
data.tar.gz: 6e90df633e9b187164c36868b1f3b8fbf7dcba3b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 333ec1b5f83bc9994983fbb121949266de217e967d38847b8301bcfd44d65871866cf7afdfe111f4b3cd429c0f63a0279cc1d0f7513602e9fe8481996be04347
|
7
|
+
data.tar.gz: 03cc7eabdadd122bd0ee8a1aa0611364f3c51148afc5705c17e315b46514e3baa391df88d234a29237467e0a4999936701cb10636fb775b4d4f57125bd079b63
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.0.0
|
4
|
+
- 1.9.3
|
5
|
+
- 1.8.7
|
6
|
+
- jruby-19mode
|
7
|
+
- jruby-18mode
|
8
|
+
|
9
|
+
before_install:
|
10
|
+
- gem install bundler --version '~> 1.3'
|
11
|
+
|
12
|
+
gemfile:
|
13
|
+
- Gemfile
|
14
|
+
- gemfiles/3.0.gemfile
|
15
|
+
|
16
|
+
matrix:
|
17
|
+
allow_failures:
|
18
|
+
- rvm: jruby-18mode
|
19
|
+
gemfile: Gemfile
|
20
|
+
- rvm: 1.8.7
|
21
|
+
gemfile: Gemfile
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
## 3.0.0 (Unreleased)
|
2
|
+
|
3
|
+
- [#264](https://github.com/airblade/paper_trail/pull/264) - Allow unwrapped symbol to be passed in to the `on` option.
|
4
|
+
- [#224](https://github.com/airblade/paper_trail/issues/224)/[#236](https://github.com/airblade/paper_trail/pull/236) -
|
5
|
+
Fixed compatibility with [ActsAsTaggableOn](https://github.com/mbleigh/acts-as-taggable-on).
|
6
|
+
- [#235](https://github.com/airblade/paper_trail/pull/235) - Dropped unnecessary secondary sort on `versions` association.
|
7
|
+
- [#216](https://github.com/airblade/paper_trail/pull/216) - Added helper & extension for [RSpec](https://github.com/rspec/rspec),
|
8
|
+
and helper for [Cucumber](http://cukes.info).
|
9
|
+
- [#212](https://github.com/airblade/paper_trail/pull/212) - Added `PaperTrail::Cleaner` module, useful for discarding draft versions.
|
10
|
+
- [#207](https://github.com/airblade/paper_trail/issues/207) - Versions for `'create'` events are now created with `create!` instead of
|
11
|
+
`create` so that an exception gets raised if it is appropriate to do so.
|
12
|
+
- [#199](https://github.com/airblade/paper_trail/pull/199) - Rails 4 compatibility.
|
13
|
+
- [#165](https://github.com/airblade/paper_trail/pull/165) - Namespaced the `Version` class under the `PaperTrail` module.
|
14
|
+
- [#119](https://github.com/airblade/paper_trail/issues/119) - Support for [Sinatra](http://www.sinatrarb.com/); decoupled gem from `Rails`.
|
15
|
+
|
16
|
+
## 2.7.2
|
17
|
+
|
18
|
+
- [#228](https://github.com/airblade/paper_trail/issues/228) - Refactored default `user_for_paper_trail` method implementation
|
19
|
+
so that `current_user` only gets invoked if it is defined.
|
20
|
+
- [#219](https://github.com/airblade/paper_trail/pull/219) - Fixed issue where attributes stored with `nil` value might not get
|
21
|
+
reified properly depending on the way the serializer worked.
|
22
|
+
- [#213](https://github.com/airblade/paper_trail/issues/213) - Added a `version_limit` option to the `PaperTrail::Config` options
|
23
|
+
that can be used to restrict the number of versions PaperTrail will store per object instance.
|
24
|
+
- [#187](https://github.com/airblade/paper_trail/pull/187) - Confirmed JRuby support.
|
25
|
+
- [#174](https://github.com/airblade/paper_trail/pull/174) - The `event` field on the versions table can now be customized.
|
26
|
+
|
27
|
+
## 2.7.1
|
28
|
+
|
29
|
+
- [#206](https://github.com/airblade/paper_trail/issues/206) - Fixed Ruby 1.8.7 compatibility for tracking `object_changes`.
|
30
|
+
- [#200](https://github.com/airblade/paper_trail/issues/200) - Fixed `next_version` method so that it returns the live model
|
31
|
+
when called on latest reified version of a model prior to the live model.
|
32
|
+
- [#197](https://github.com/airblade/paper_trail/issues/197) - PaperTrail now falls back on using YAML for serialization of
|
33
|
+
serialized model attributes for storage in the `object` and `object_changes` columns in the `Version` table. This fixes
|
34
|
+
compatibility for `Rails 3.0.x` for projects that employ the `serialize` declaration on a model.
|
35
|
+
- [#194](https://github.com/airblade/paper_trail/issues/194) - A JSON serializer is now included in the gem.
|
36
|
+
- [#192](https://github.com/airblade/paper_trail/pull/192) - `object_changes` should store serialized representation of serialized
|
37
|
+
attributes for `create` actions (in addition to `update` actions, which had already been patched by
|
38
|
+
[#180](https://github.com/airblade/paper_trail/pull/180)).
|
39
|
+
- [#190](https://github.com/airblade/paper_trail/pull/190) - Fixed compatibility with
|
40
|
+
[SerializedAttributes](https://github.com/technoweenie/serialized_attributes) gem.
|
41
|
+
- [#189](https://github.com/airblade/paper_trail/pull/189) - Provided support for a `configure` block initializer.
|
42
|
+
- Added `setter` method for the `serializer` config option.
|
43
|
+
|
44
|
+
## 2.7.0
|
45
|
+
|
46
|
+
- [#183](https://github.com/airblade/paper_trail/pull/183) - Fully qualify the `Version` class to help prevent
|
47
|
+
namespace resolution errors within other gems / plugins.
|
48
|
+
- [#180](https://github.com/airblade/paper_trail/pull/180) - Store serialized representation of serialized attributes
|
49
|
+
on the `object` and `object_changes` columns in the `Version` table.
|
50
|
+
- [#164](https://github.com/airblade/paper_trail/pull/164) - Allow usage of custom serializer for storage of object attributes.
|
51
|
+
|
52
|
+
## 2.6.4
|
53
|
+
|
54
|
+
- [#181](https://github.com/airblade/paper_trail/issues/181)/[#182](https://github.com/airblade/paper_trail/pull/182) -
|
55
|
+
Controller metadata methods should only be evaluated when `paper_trail_enabled_for_controller == true`.
|
56
|
+
- [#177](https://github.com/airblade/paper_trail/issues/177)/[#178](https://github.com/airblade/paper_trail/pull/178) -
|
57
|
+
Factored out `version_key` into it's own method to prevent `ConnectionNotEstablished` error from getting thrown in
|
58
|
+
instances where `has_paper_trail` is declared on class prior to ActiveRecord establishing a connection.
|
59
|
+
- [#176](https://github.com/airblade/paper_trail/pull/176) - Force metadata calls for attributes to use current value
|
60
|
+
if attribute value is changing.
|
61
|
+
- [#173](https://github.com/airblade/paper_trail/pull/173) - Update link to [diff-lcs](https://github.com/halostatue/diff-lcs).
|
62
|
+
- [#172](https://github.com/airblade/paper_trail/pull/172) - Save `object_changes` on creation.
|
63
|
+
- [#168](https://github.com/airblade/paper_trail/pull/168) - Respect conditional `:if` or `:unless` arguments to the
|
64
|
+
`has_paper_trail` method for `destroy` events.
|
65
|
+
- [#167](https://github.com/airblade/paper_trail/pull/167) - Fix `originator` method so that it works with subclasses and STI.
|
66
|
+
- [#160](https://github.com/airblade/paper_trail/pull/160) - Fixed failing tests and resolved out of date dependency issues.
|
67
|
+
- [#157](https://github.com/airblade/paper_trail/pull/157) - Refactored `class_attribute` names on the `ClassMethods` module
|
68
|
+
for names that are not obviously pertaining to PaperTrail to prevent method name collision.
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Andy Stewart, AirBlade Software Ltd.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,979 @@
|
|
1
|
+
# PaperTrail [![Build Status](https://secure.travis-ci.org/airblade/paper_trail.png?branch=master)](http://travis-ci.org/airblade/paper_trail) [![Dependency Status](https://gemnasium.com/airblade/paper_trail.png)](https://gemnasium.com/airblade/paper_trail)
|
2
|
+
|
3
|
+
PaperTrail lets you track changes to your models' data. It's good for auditing or versioning. You can see how a model looked at any stage in its lifecycle, revert it to any version, and even undelete it after it's been destroyed.
|
4
|
+
|
5
|
+
There's an excellent [Railscast on implementing Undo with Paper Trail](http://railscasts.com/episodes/255-undo-with-paper-trail).
|
6
|
+
|
7
|
+
|
8
|
+
## Features
|
9
|
+
|
10
|
+
* Stores every create, update and destroy (or only the lifecycle events you specify).
|
11
|
+
* Does not store updates which don't change anything.
|
12
|
+
* Allows you to specify attributes (by inclusion or exclusion) which must change for a Version to be stored.
|
13
|
+
* Allows you to get at every version, including the original, even once destroyed.
|
14
|
+
* Allows you to get at every version even if the schema has since changed.
|
15
|
+
* Allows you to get at the version as of a particular time.
|
16
|
+
* Option to automatically restore `has_one` associations as they were at the time.
|
17
|
+
* Automatically records who was responsible via your controller. PaperTrail calls `current_user` by default, if it exists, but you can have it call any method you like.
|
18
|
+
* Allows you to set who is responsible at model-level (useful for migrations).
|
19
|
+
* Allows you to store arbitrary model-level metadata with each version (useful for filtering versions).
|
20
|
+
* Allows you to store arbitrary controller-level information with each version, e.g. remote IP.
|
21
|
+
* Can be turned off/on per class (useful for migrations).
|
22
|
+
* Can be turned off/on per request (useful for testing with an external service).
|
23
|
+
* Can be turned off/on globally (useful for testing).
|
24
|
+
* No configuration necessary.
|
25
|
+
* Stores everything in a single database table by default (generates migration for you), or can use separate tables for separate models.
|
26
|
+
* Supports custom version classes so different models' versions can have different behaviour.
|
27
|
+
* Supports custom name for versions association.
|
28
|
+
* Thoroughly tested.
|
29
|
+
* Threadsafe.
|
30
|
+
|
31
|
+
|
32
|
+
## Compatibility
|
33
|
+
|
34
|
+
Works with ActiveRecord 4 and ActiveRecord 3. Note: this code is on the `master` branch and tagged `v3.x`.
|
35
|
+
|
36
|
+
**You are reading the docs for the `master` branch. The latest release of PaperTrail is 2.7.2, the docs for which can be viewed on the [`2.7-stable`](https://github.com/airblade/paper_trail/tree/2.7-stable branch).**
|
37
|
+
|
38
|
+
Version 2 is on the branch named [`2.7-stable`](https://github.com/airblade/paper_trail/tree/2.7-stable) and is tagged `v2.x`, and works with Rails 3.
|
39
|
+
The Rails 2.3 code is on the [`rails2`](https://github.com/airblade/paper_trail/tree/rails2) branch and tagged `v1.x`. These branches are both stable with their respective versions of Rails but will not have new features added/backported to them.
|
40
|
+
|
41
|
+
## Installation
|
42
|
+
|
43
|
+
### Rails 3 & 4
|
44
|
+
|
45
|
+
1. Install PaperTrail as a gem via your `Gemfile`:
|
46
|
+
|
47
|
+
`gem 'paper_trail', '>= 3.0.0.beta1'`
|
48
|
+
|
49
|
+
2. Generate a migration which will add a `versions` table to your database.
|
50
|
+
|
51
|
+
`bundle exec rails generate paper_trail:install`
|
52
|
+
|
53
|
+
3. Run the migration.
|
54
|
+
|
55
|
+
`bundle exec rake db:migrate`
|
56
|
+
|
57
|
+
4. Add `has_paper_trail` to the models you want to track.
|
58
|
+
|
59
|
+
### Sinatra
|
60
|
+
|
61
|
+
PaperTrail provides a helper extension that acts similar to the controller mixin it provides for `Rails` applications.
|
62
|
+
|
63
|
+
It will set `PaperTrail.whodunnit` to whatever is returned by a method named `user_for_paper_trail` which you can define inside your Sinatra Application. (by default it attempts to invoke a method named `current_user`)
|
64
|
+
|
65
|
+
If you're using the modular [Sinatra::Base](http://www.sinatrarb.com/intro.html#Modular%20vs.%20Classic%20Style) style of application, you will need to register the extension:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
# bleh_app.rb
|
69
|
+
require 'sinatra/base'
|
70
|
+
|
71
|
+
class BlehApp < Sinatra::Base
|
72
|
+
register Sinatra::PaperTrail
|
73
|
+
end
|
74
|
+
```
|
75
|
+
|
76
|
+
## API Summary
|
77
|
+
|
78
|
+
When you declare `has_paper_trail` in your model, you get these methods:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
class Widget < ActiveRecord::Base
|
82
|
+
has_paper_trail # you can pass various options here
|
83
|
+
end
|
84
|
+
|
85
|
+
# Returns this widget's versions. You can customise the name of the association.
|
86
|
+
widget.versions
|
87
|
+
|
88
|
+
# Return the version this widget was reified from, or nil if it is live.
|
89
|
+
# You can customise the name of the method.
|
90
|
+
widget.version
|
91
|
+
|
92
|
+
# Returns true if this widget is the current, live one; or false if it is from a previous version.
|
93
|
+
widget.live?
|
94
|
+
|
95
|
+
# Returns who put the widget into its current state.
|
96
|
+
widget.originator
|
97
|
+
|
98
|
+
# Returns the widget (not a version) as it looked at the given timestamp.
|
99
|
+
widget.version_at(timestamp)
|
100
|
+
|
101
|
+
# Returns the widget (not a version) as it was most recently.
|
102
|
+
widget.previous_version
|
103
|
+
|
104
|
+
# Returns the widget (not a version) as it became next.
|
105
|
+
widget.next_version
|
106
|
+
|
107
|
+
# Turn PaperTrail off for all widgets.
|
108
|
+
Widget.paper_trail_off
|
109
|
+
|
110
|
+
# Turn PaperTrail on for all widgets.
|
111
|
+
Widget.paper_trail_on
|
112
|
+
```
|
113
|
+
|
114
|
+
And a `PaperTrail::Version` instance has these methods:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
# Returns the item restored from this version.
|
118
|
+
version.reify(options = {})
|
119
|
+
|
120
|
+
# Returns who put the item into the state stored in this version.
|
121
|
+
version.originator
|
122
|
+
|
123
|
+
# Returns who changed the item from the state it had in this version.
|
124
|
+
version.terminator
|
125
|
+
version.whodunnit
|
126
|
+
|
127
|
+
# Returns the next version.
|
128
|
+
version.next
|
129
|
+
|
130
|
+
# Returns the previous version.
|
131
|
+
version.previous
|
132
|
+
|
133
|
+
# Returns the index of this version in all the versions.
|
134
|
+
version.index
|
135
|
+
|
136
|
+
# Returns the event that caused this version (create|update|destroy).
|
137
|
+
version.event
|
138
|
+
```
|
139
|
+
|
140
|
+
In your controllers you can override these methods:
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
# Returns the user who is responsible for any changes that occur.
|
144
|
+
# Defaults to current_user.
|
145
|
+
user_for_paper_trail
|
146
|
+
|
147
|
+
# Returns any information about the controller or request that you want
|
148
|
+
# PaperTrail to store alongside any changes that occur.
|
149
|
+
info_for_paper_trail
|
150
|
+
```
|
151
|
+
|
152
|
+
## Basic Usage
|
153
|
+
|
154
|
+
PaperTrail is simple to use. Just add 15 characters to a model to get a paper trail of every `create`, `update`, and `destroy`.
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
class Widget < ActiveRecord::Base
|
158
|
+
has_paper_trail
|
159
|
+
end
|
160
|
+
```
|
161
|
+
|
162
|
+
This gives you a `versions` method which returns the paper trail of changes to your model.
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
>> widget = Widget.find 42
|
166
|
+
>> widget.versions # [<PaperTrail::Version>, <PaperTrail::Version>, ...]
|
167
|
+
```
|
168
|
+
|
169
|
+
Once you have a version, you can find out what happened:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
>> v = widget.versions.last
|
173
|
+
>> v.event # 'update' (or 'create' or 'destroy')
|
174
|
+
>> v.whodunnit # '153' (if the update was via a controller and
|
175
|
+
# the controller has a current_user method,
|
176
|
+
# here returning the id of the current user)
|
177
|
+
>> v.created_at # when the update occurred
|
178
|
+
>> widget = v.reify # the widget as it was before the update;
|
179
|
+
# would be nil for a create event
|
180
|
+
```
|
181
|
+
|
182
|
+
PaperTrail stores the pre-change version of the model, unlike some other auditing/versioning plugins, so you can retrieve the original version. This is useful when you start keeping a paper trail for models that already have records in the database.
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
>> widget = Widget.find 153
|
186
|
+
>> widget.name # 'Doobly'
|
187
|
+
|
188
|
+
# Add has_paper_trail to Widget model.
|
189
|
+
|
190
|
+
>> widget.versions # []
|
191
|
+
>> widget.update_attributes :name => 'Wotsit'
|
192
|
+
>> widget.versions.last.reify.name # 'Doobly'
|
193
|
+
>> widget.versions.last.event # 'update'
|
194
|
+
```
|
195
|
+
|
196
|
+
This also means that PaperTrail does not waste space storing a version of the object as it currently stands. The `versions` method gives you previous versions; to get the current one just call a finder on your `Widget` model as usual.
|
197
|
+
|
198
|
+
Here's a helpful table showing what PaperTrail stores:
|
199
|
+
|
200
|
+
<table>
|
201
|
+
<tr>
|
202
|
+
<th>Event</th>
|
203
|
+
<th>Model Before</th>
|
204
|
+
<th>Model After</th>
|
205
|
+
</tr>
|
206
|
+
<tr>
|
207
|
+
<td>create</td>
|
208
|
+
<td>nil</td>
|
209
|
+
<td>widget</td>
|
210
|
+
</tr>
|
211
|
+
<tr>
|
212
|
+
<td>update</td>
|
213
|
+
<td>widget</td>
|
214
|
+
<td>widget'</td>
|
215
|
+
<tr>
|
216
|
+
<td>destroy</td>
|
217
|
+
<td>widget</td>
|
218
|
+
<td>nil</td>
|
219
|
+
</tr>
|
220
|
+
</table>
|
221
|
+
|
222
|
+
PaperTrail stores the values in the Model Before column. Most other auditing/versioning plugins store the After column.
|
223
|
+
|
224
|
+
|
225
|
+
## Choosing Lifecycle Events To Monitor
|
226
|
+
|
227
|
+
You can choose which events to track with the `on` option. For example, to ignore `create` events:
|
228
|
+
|
229
|
+
```ruby
|
230
|
+
class Article < ActiveRecord::Base
|
231
|
+
has_paper_trail :on => [:update, :destroy]
|
232
|
+
end
|
233
|
+
```
|
234
|
+
|
235
|
+
You may also have the `PaperTrail::Version` model save a custom string in it's `event` field instead of the typical `create`, `update`, `destroy`.
|
236
|
+
PaperTrail supplies a custom accessor method called `paper_trail_event`, which it will attempt to use to fill the `event` field before
|
237
|
+
falling back on one of the default events.
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
>> a = Article.create
|
241
|
+
>> a.versions.size # 1
|
242
|
+
>> a.versions.last.event # 'create'
|
243
|
+
>> a.paper_trail_event = 'update title'
|
244
|
+
>> a.update_attributes :title => 'My Title'
|
245
|
+
>> a.versions.size # 2
|
246
|
+
>> a.versions.last.event # 'update title'
|
247
|
+
>> a.paper_trail_event = nil
|
248
|
+
>> a.update_attributes :title => "Alternate"
|
249
|
+
>> a.versions.size # 3
|
250
|
+
>> a.versions.last.event # 'update'
|
251
|
+
```
|
252
|
+
|
253
|
+
## Choosing When To Save New Versions
|
254
|
+
|
255
|
+
You can choose the conditions when to add new versions with the `if` and `unless` options. For example, to save versions only for US non-draft translations:
|
256
|
+
|
257
|
+
```ruby
|
258
|
+
class Translation < ActiveRecord::Base
|
259
|
+
has_paper_trail :if => Proc.new { |t| t.language_code == 'US' },
|
260
|
+
:unless => Proc.new { |t| t.type == 'DRAFT' }
|
261
|
+
end
|
262
|
+
```
|
263
|
+
|
264
|
+
|
265
|
+
## Choosing Attributes To Monitor
|
266
|
+
|
267
|
+
You can ignore changes to certain attributes like this:
|
268
|
+
|
269
|
+
```ruby
|
270
|
+
class Article < ActiveRecord::Base
|
271
|
+
has_paper_trail :ignore => [:title, :rating]
|
272
|
+
end
|
273
|
+
```
|
274
|
+
|
275
|
+
This means that changes to just the `title` or `rating` will not store another version of the article. It does not mean that the `title` and `rating` attributes will be ignored if some other change causes a new `PaperTrail::Version` to be created. For example:
|
276
|
+
|
277
|
+
```ruby
|
278
|
+
>> a = Article.create
|
279
|
+
>> a.versions.length # 1
|
280
|
+
>> a.update_attributes :title => 'My Title', :rating => 3
|
281
|
+
>> a.versions.length # 1
|
282
|
+
>> a.update_attributes :title => 'Greeting', :content => 'Hello'
|
283
|
+
>> a.versions.length # 2
|
284
|
+
>> a.previous_version.title # 'My Title'
|
285
|
+
```
|
286
|
+
|
287
|
+
Or, you can specify a list of all attributes you care about:
|
288
|
+
|
289
|
+
```ruby
|
290
|
+
class Article < ActiveRecord::Base
|
291
|
+
has_paper_trail :only => [:title]
|
292
|
+
end
|
293
|
+
```
|
294
|
+
|
295
|
+
This means that only changes to the `title` will save a version of the article:
|
296
|
+
|
297
|
+
```ruby
|
298
|
+
>> a = Article.create
|
299
|
+
>> a.versions.length # 1
|
300
|
+
>> a.update_attributes :title => 'My Title'
|
301
|
+
>> a.versions.length # 2
|
302
|
+
>> a.update_attributes :content => 'Hello'
|
303
|
+
>> a.versions.length # 2
|
304
|
+
>> a.previous_version.content # nil
|
305
|
+
```
|
306
|
+
|
307
|
+
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`.
|
308
|
+
|
309
|
+
You can skip fields altogether with the `:skip` option. As with `:ignore`, updates to these fields will not create a new `PaperTrail::Version`. In addition, these fields will not be included in the serialized version of the object whenever a new `PaperTrail::Version` is created.
|
310
|
+
|
311
|
+
For example:
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
class Article < ActiveRecord::Base
|
315
|
+
has_paper_trail :skip => [:file_upload]
|
316
|
+
end
|
317
|
+
```
|
318
|
+
|
319
|
+
## Reverting And Undeleting A Model
|
320
|
+
|
321
|
+
PaperTrail makes reverting to a previous version easy:
|
322
|
+
|
323
|
+
```ruby
|
324
|
+
>> widget = Widget.find 42
|
325
|
+
>> widget.update_attributes :name => 'Blah blah'
|
326
|
+
# Time passes....
|
327
|
+
>> widget = widget.previous_version # the widget as it was before the update
|
328
|
+
>> widget.save # reverted
|
329
|
+
```
|
330
|
+
|
331
|
+
Alternatively you can find the version at a given time:
|
332
|
+
|
333
|
+
```ruby
|
334
|
+
>> widget = widget.version_at(1.day.ago) # the widget as it was one day ago
|
335
|
+
>> widget.save # reverted
|
336
|
+
```
|
337
|
+
|
338
|
+
Note `version_at` gives you the object, not a version, so you don't need to call `reify`.
|
339
|
+
|
340
|
+
Undeleting is just as simple:
|
341
|
+
|
342
|
+
```ruby
|
343
|
+
>> widget = Widget.find 42
|
344
|
+
>> widget.destroy
|
345
|
+
# Time passes....
|
346
|
+
>> widget = PaperTrail::Version.find(153).reify # the widget as it was before it was destroyed
|
347
|
+
>> widget.save # the widget lives!
|
348
|
+
```
|
349
|
+
|
350
|
+
In fact you could use PaperTrail to implement an undo system, though I haven't had the opportunity yet to do it myself. However [Ryan Bates has](http://railscasts.com/episodes/255-undo-with-paper-trail)!
|
351
|
+
|
352
|
+
|
353
|
+
## Navigating Versions
|
354
|
+
|
355
|
+
You can call `previous_version` and `next_version` on an item to get it as it was/became. Note that these methods reify the item for you.
|
356
|
+
|
357
|
+
```ruby
|
358
|
+
>> live_widget = Widget.find 42
|
359
|
+
>> live_widget.versions.length # 4 for example
|
360
|
+
>> widget = live_widget.previous_version # => widget == live_widget.versions.last.reify
|
361
|
+
>> widget = widget.previous_version # => widget == live_widget.versions[-2].reify
|
362
|
+
>> widget = widget.next_version # => widget == live_widget.versions.last.reify
|
363
|
+
>> widget.next_version # nil
|
364
|
+
```
|
365
|
+
|
366
|
+
As an aside, I'm undecided about whether `widget.previous_version.next_version` should return `nil` or `self` (i.e. `widget`). Let me know if you have a view.
|
367
|
+
|
368
|
+
If instead you have a particular `version` of an item you can navigate to the previous and next versions.
|
369
|
+
|
370
|
+
```ruby
|
371
|
+
>> widget = Widget.find 42
|
372
|
+
>> version = widget.versions[-2] # assuming widget has several versions
|
373
|
+
>> previous = version.previous
|
374
|
+
>> next = version.next
|
375
|
+
```
|
376
|
+
|
377
|
+
You can find out which of an item's versions yours is:
|
378
|
+
|
379
|
+
```ruby
|
380
|
+
>> current_version_number = version.index # 0-based
|
381
|
+
```
|
382
|
+
|
383
|
+
Finally, if you got an item by reifying one of its versions, you can navigate back to the version it came from:
|
384
|
+
|
385
|
+
```ruby
|
386
|
+
>> latest_version = Widget.find(42).versions.last
|
387
|
+
>> widget = latest_version.reify
|
388
|
+
>> widget.version == latest_version # true
|
389
|
+
```
|
390
|
+
|
391
|
+
You can find out whether a model instance is the current, live one -- or whether it came instead from a previous version -- with `live?`:
|
392
|
+
|
393
|
+
```ruby
|
394
|
+
>> widget = Widget.find 42
|
395
|
+
>> widget.live? # true
|
396
|
+
>> widget = widget.previous_version
|
397
|
+
>> widget.live? # false
|
398
|
+
```
|
399
|
+
|
400
|
+
## Finding Out Who Was Responsible For A Change
|
401
|
+
|
402
|
+
If your `ApplicationController` has a `current_user` method, PaperTrail will store the value it returns in the `version`'s `whodunnit` column. Note that this column is a string so you will have to convert it to an integer if it's an id and you want to look up the user later on:
|
403
|
+
|
404
|
+
```ruby
|
405
|
+
>> last_change = widget.versions.last
|
406
|
+
>> user_who_made_the_change = User.find last_change.whodunnit.to_i
|
407
|
+
```
|
408
|
+
|
409
|
+
You may want PaperTrail to call a different method to find out who is responsible. To do so, override the `user_for_paper_trail` method in your controller like this:
|
410
|
+
|
411
|
+
```ruby
|
412
|
+
class ApplicationController
|
413
|
+
def user_for_paper_trail
|
414
|
+
logged_in? ? current_member : 'Public user' # or whatever
|
415
|
+
end
|
416
|
+
end
|
417
|
+
```
|
418
|
+
|
419
|
+
In a migration or in `rails console` you can set who is responsible like this:
|
420
|
+
|
421
|
+
```ruby
|
422
|
+
>> PaperTrail.whodunnit = 'Andy Stewart'
|
423
|
+
>> widget.update_attributes :name => 'Wibble'
|
424
|
+
>> widget.versions.last.whodunnit # Andy Stewart
|
425
|
+
```
|
426
|
+
|
427
|
+
You can avoid having to do this manually by setting your initializer to pick up the username of the current user from the OS, like this:
|
428
|
+
|
429
|
+
```ruby
|
430
|
+
class PaperTrail::Version < ActiveRecord::Base
|
431
|
+
if defined?(Rails::Console)
|
432
|
+
PaperTrail.whodunnit = "#{`whoami`.strip}: console"
|
433
|
+
elsif File.basename($0) == "rake"
|
434
|
+
PaperTrail.whodunnit = "#{`whoami`.strip}: rake #{ARGV.join ' '}"
|
435
|
+
end
|
436
|
+
end
|
437
|
+
```
|
438
|
+
|
439
|
+
N.B. A `version`'s `whodunnit` records who changed the object causing the `version` to be stored. Because a `version` stores the object as it looked before the change (see the table above), `whodunnit` returns who stopped the object looking like this -- not who made it look like this. Hence `whodunnit` is aliased as `terminator`.
|
440
|
+
|
441
|
+
To find out who made a `version`'s object look that way, use `version.originator`. And to find out who made a "live" object look like it does, use `originator` on the object.
|
442
|
+
|
443
|
+
```ruby
|
444
|
+
>> widget = Widget.find 153 # assume widget has 0 versions
|
445
|
+
>> PaperTrail.whodunnit = 'Alice'
|
446
|
+
>> widget.update_attributes :name => 'Yankee'
|
447
|
+
>> widget.originator # 'Alice'
|
448
|
+
>> PaperTrail.whodunnit = 'Bob'
|
449
|
+
>> widget.update_attributes :name => 'Zulu'
|
450
|
+
>> widget.originator # 'Bob'
|
451
|
+
>> first_version, last_version = widget.versions.first, widget.versions.last
|
452
|
+
>> first_version.whodunnit # 'Alice'
|
453
|
+
>> first_version.originator # nil
|
454
|
+
>> first_version.terminator # 'Alice'
|
455
|
+
>> last_version.whodunnit # 'Bob'
|
456
|
+
>> last_version.originator # 'Alice'
|
457
|
+
>> last_version.terminator # 'Bob'
|
458
|
+
```
|
459
|
+
|
460
|
+
## Custom Version Classes
|
461
|
+
|
462
|
+
You can specify custom version subclasses with the `:class_name` option:
|
463
|
+
|
464
|
+
```ruby
|
465
|
+
class PostVersion < PaperTrail::Version
|
466
|
+
# custom behaviour, e.g:
|
467
|
+
self.table_name = :post_versions
|
468
|
+
end
|
469
|
+
|
470
|
+
class Post < ActiveRecord::Base
|
471
|
+
has_paper_trail :class_name => 'PostVersion'
|
472
|
+
end
|
473
|
+
```
|
474
|
+
|
475
|
+
This allows you to store each model's versions in a separate table, which is useful if you have a lot of versions being created.
|
476
|
+
|
477
|
+
If you are using Postgres, you should also define the sequence that your custom version class will use:
|
478
|
+
|
479
|
+
```ruby
|
480
|
+
class PostVersion < PaperTrail::Version
|
481
|
+
self.table_name = :post_versions
|
482
|
+
self.sequence_name = :post_version_id_seq
|
483
|
+
end
|
484
|
+
```
|
485
|
+
|
486
|
+
Alternatively you could store certain metadata for one type of version, and other metadata for other versions.
|
487
|
+
|
488
|
+
If you only use custom version classes and don't use PaperTrail's built-in one, on Rails 3.2 you must:
|
489
|
+
|
490
|
+
- either declare PaperTrail's version class abstract like this (in `config/initializers/paper_trail_patch.rb`):
|
491
|
+
|
492
|
+
```ruby
|
493
|
+
PaperTrail::Version.module_eval do
|
494
|
+
self.abstract_class = true
|
495
|
+
end
|
496
|
+
```
|
497
|
+
|
498
|
+
- or define a `versions` table in the database so Rails can instantiate the version superclass.
|
499
|
+
|
500
|
+
You can also specify custom names for the versions and version associations. This is useful if you already have `versions` or/and `version` methods on your model. For example:
|
501
|
+
|
502
|
+
```ruby
|
503
|
+
class Post < ActiveRecord::Base
|
504
|
+
has_paper_trail :versions => :paper_trail_versions,
|
505
|
+
:version => :paper_trail_version
|
506
|
+
|
507
|
+
# Existing versions method. We don't want to clash.
|
508
|
+
def versions
|
509
|
+
...
|
510
|
+
end
|
511
|
+
# Existing version method. We don't want to clash.
|
512
|
+
def version
|
513
|
+
...
|
514
|
+
end
|
515
|
+
end
|
516
|
+
```
|
517
|
+
|
518
|
+
## Associations
|
519
|
+
|
520
|
+
I haven't yet found a good way to get PaperTrail to automatically restore associations when you reify a model. See [here for a little more info](http://airbladesoftware.com/notes/undo-and-redo-with-papertrail).
|
521
|
+
|
522
|
+
If you can think of a good way to achieve this, please let me know.
|
523
|
+
|
524
|
+
|
525
|
+
## Has-One Associations
|
526
|
+
|
527
|
+
PaperTrail can restore `:has_one` associations as they were at (actually, 3 seconds before) the time.
|
528
|
+
|
529
|
+
```ruby
|
530
|
+
class Treasure < ActiveRecord::Base
|
531
|
+
has_one :location
|
532
|
+
end
|
533
|
+
|
534
|
+
>> treasure.amount # 100
|
535
|
+
>> treasure.location.latitude # 12.345
|
536
|
+
|
537
|
+
>> treasure.update_attributes :amount => 153
|
538
|
+
>> treasure.location.update_attributes :latitude => 54.321
|
539
|
+
|
540
|
+
>> t = treasure.versions.last.reify(:has_one => true)
|
541
|
+
>> t.amount # 100
|
542
|
+
>> t.location.latitude # 12.345
|
543
|
+
```
|
544
|
+
|
545
|
+
The implementation is complicated by the edge case where the parent and child are updated in one go, e.g. in one web request or database transaction. PaperTrail doesn't know about different models being updated "together", so you can't ask it definitively to get the child as it was before the joint parent-and-child update.
|
546
|
+
|
547
|
+
The correct solution is to make PaperTrail aware of requests or transactions (c.f. [Efficiency's transaction ID middleware](http://github.com/efficiency20/ops_middleware/blob/master/lib/e20/ops/middleware/transaction_id_middleware.rb)). In the meantime we work around the problem by finding the child as it was a few seconds before the parent was updated. By default we go 3 seconds before but you can change this by passing the desired number of seconds to the `:has_one` option:
|
548
|
+
|
549
|
+
```ruby
|
550
|
+
>> t = treasure.versions.last.reify(:has_one => 1) # look back 1 second instead of 3
|
551
|
+
```
|
552
|
+
|
553
|
+
If you are shuddering, take solace from knowing PaperTrail opts out of these shenanigans by default. This means your `:has_one` associated objects will be the live ones, not the ones the user saw at the time. Since PaperTrail doesn't auto-restore `:has_many` associations (I can't get it to work) or `:belongs_to` (I ran out of time looking at `:has_many`), this at least makes your associations wrong consistently ;)
|
554
|
+
|
555
|
+
|
556
|
+
|
557
|
+
## Has-Many-Through Associations
|
558
|
+
|
559
|
+
PaperTrail can track most changes to the join table. Specifically it can track all additions but it can only track removals which fire the `after_destroy` callback on the join table. Here are some examples:
|
560
|
+
|
561
|
+
Given these models:
|
562
|
+
|
563
|
+
```ruby
|
564
|
+
class Book < ActiveRecord::Base
|
565
|
+
has_many :authorships, :dependent => :destroy
|
566
|
+
has_many :authors, :through => :authorships, :source => :person
|
567
|
+
has_paper_trail
|
568
|
+
end
|
569
|
+
|
570
|
+
class Authorship < ActiveRecord::Base
|
571
|
+
belongs_to :book
|
572
|
+
belongs_to :person
|
573
|
+
has_paper_trail # NOTE
|
574
|
+
end
|
575
|
+
|
576
|
+
class Person < ActiveRecord::Base
|
577
|
+
has_many :authorships, :dependent => :destroy
|
578
|
+
has_many :books, :through => :authorships
|
579
|
+
has_paper_trail
|
580
|
+
end
|
581
|
+
```
|
582
|
+
|
583
|
+
Then each of the following will store authorship versions:
|
584
|
+
|
585
|
+
```ruby
|
586
|
+
>> @book.authors << @dostoyevsky
|
587
|
+
>> @book.authors.create :name => 'Tolstoy'
|
588
|
+
>> @book.authorships.last.destroy
|
589
|
+
>> @book.authorships.clear
|
590
|
+
```
|
591
|
+
|
592
|
+
But none of these will:
|
593
|
+
|
594
|
+
```ruby
|
595
|
+
>> @book.authors.delete @tolstoy
|
596
|
+
>> @book.author_ids = [@solzhenistyn.id, @dostoyevsky.id]
|
597
|
+
>> @book.authors = []
|
598
|
+
```
|
599
|
+
|
600
|
+
Having said that, you can apparently get all these working (I haven't tested it myself) with this patch:
|
601
|
+
|
602
|
+
```ruby
|
603
|
+
# In config/initializers/active_record_patch.rb
|
604
|
+
module ActiveRecord
|
605
|
+
# = Active Record Has Many Through Association
|
606
|
+
module Associations
|
607
|
+
class HasManyThroughAssociation < HasManyAssociation #:nodoc:
|
608
|
+
alias_method :original_delete_records, :delete_records
|
609
|
+
|
610
|
+
def delete_records(records, method)
|
611
|
+
method ||= :destroy
|
612
|
+
original_delete_records(records, method)
|
613
|
+
end
|
614
|
+
end
|
615
|
+
end
|
616
|
+
end
|
617
|
+
```
|
618
|
+
|
619
|
+
See [issue 113](https://github.com/airblade/paper_trail/issues/113) for a discussion about this.
|
620
|
+
|
621
|
+
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.
|
622
|
+
|
623
|
+
|
624
|
+
## Storing metadata
|
625
|
+
|
626
|
+
You can store arbitrary model-level metadata alongside each version like this:
|
627
|
+
|
628
|
+
```ruby
|
629
|
+
class Article < ActiveRecord::Base
|
630
|
+
belongs_to :author
|
631
|
+
has_paper_trail :meta => { :author_id => :author_id,
|
632
|
+
:word_count => :count_words,
|
633
|
+
:answer => 42 }
|
634
|
+
def count_words
|
635
|
+
153
|
636
|
+
end
|
637
|
+
end
|
638
|
+
```
|
639
|
+
|
640
|
+
PaperTrail will call your proc with the current article and store the result in the `author_id` column of the `versions` table.
|
641
|
+
|
642
|
+
N.B. You must also:
|
643
|
+
|
644
|
+
* Add your metadata columns to the `versions` table.
|
645
|
+
* Declare your metadata columns using `attr_accessible`.
|
646
|
+
|
647
|
+
For example:
|
648
|
+
|
649
|
+
```ruby
|
650
|
+
# config/initializers/paper_trail.rb
|
651
|
+
module PaperTrail
|
652
|
+
class Version < ActiveRecord::Base
|
653
|
+
attr_accessible :author_id, :word_count, :answer
|
654
|
+
end
|
655
|
+
end
|
656
|
+
```
|
657
|
+
|
658
|
+
Why would you do this? In this example, `author_id` is an attribute of `Article` and PaperTrail will store it anyway in serialized (YAML) form in the `object` column of the `version` record. But let's say you wanted to pull out all versions for a particular author; without the metadata you would have to deserialize (reify) each `version` object to see if belonged to the author in question. Clearly this is inefficient. Using the metadata you can find just those versions you want:
|
659
|
+
|
660
|
+
```ruby
|
661
|
+
PaperTrail::Version.all(:conditions => ['author_id = ?', author_id])
|
662
|
+
```
|
663
|
+
|
664
|
+
Note you can pass a symbol as a value in the `meta` hash to signal a method to call.
|
665
|
+
|
666
|
+
You can also store any information you like from your controller. Just override the `info_for_paper_trail` method in your controller to return a hash whose keys correspond to columns in your `versions` table. E.g.:
|
667
|
+
|
668
|
+
```ruby
|
669
|
+
class ApplicationController
|
670
|
+
def info_for_paper_trail
|
671
|
+
{ :ip => request.remote_ip, :user_agent => request.user_agent }
|
672
|
+
end
|
673
|
+
end
|
674
|
+
```
|
675
|
+
|
676
|
+
Remember to add those extra columns to your `versions` table and use `attr_accessible` ;)
|
677
|
+
|
678
|
+
|
679
|
+
## Diffing Versions
|
680
|
+
|
681
|
+
There are two scenarios: diffing adjacent versions and diffing non-adjacent versions.
|
682
|
+
|
683
|
+
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:
|
684
|
+
|
685
|
+
```ruby
|
686
|
+
>> widget = Widget.create :name => 'Bob'
|
687
|
+
>> widget.versions.last.changeset # {'name' => [nil, 'Bob']}
|
688
|
+
>> widget.update_attributes :name => 'Robert'
|
689
|
+
>> widget.versions.last.changeset # {'name' => ['Bob', 'Robert']}
|
690
|
+
>> widget.destroy
|
691
|
+
>> widget.versions.last.changeset # {}
|
692
|
+
```
|
693
|
+
|
694
|
+
Note PaperTrail only stores the changes for creation and updates; it doesn't store anything when an object is destroyed.
|
695
|
+
|
696
|
+
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.
|
697
|
+
|
698
|
+
To diff non-adjacent versions you'll have to write your own code. These libraries may help:
|
699
|
+
|
700
|
+
For diffing two strings:
|
701
|
+
|
702
|
+
* [htmldiff](http://github.com/myobie/htmldiff): expects but doesn't require HTML input and produces HTML output. Works very well but slows down significantly on large (e.g. 5,000 word) inputs.
|
703
|
+
* [differ](http://github.com/pvande/differ): expects plain text input and produces plain text/coloured/HTML/any output. Can do character-wise, word-wise, line-wise, or arbitrary-boundary-string-wise diffs. Works very well on non-HTML input.
|
704
|
+
* [diff-lcs](https://github.com/halostatue/diff-lcs): old-school, line-wise diffs.
|
705
|
+
|
706
|
+
For diffing two ActiveRecord objects:
|
707
|
+
|
708
|
+
* [Jeremy Weiskotten's PaperTrail fork](http://github.com/jeremyw/paper_trail/blob/master/lib/paper_trail/has_paper_trail.rb#L151-156): uses ActiveSupport's diff to return an array of hashes of the changes.
|
709
|
+
* [activerecord-diff](http://github.com/tim/activerecord-diff): rather like ActiveRecord::Dirty but also allows you to specify which columns to compare.
|
710
|
+
|
711
|
+
|
712
|
+
## Turning PaperTrail Off/On
|
713
|
+
|
714
|
+
Sometimes you don't want to store changes. Perhaps you are only interested in changes made by your users and don't need to store changes you make yourself in, say, a migration -- or when testing your application.
|
715
|
+
|
716
|
+
You can turn PaperTrail on or off in three ways: globally, per request, or per class.
|
717
|
+
|
718
|
+
### Globally
|
719
|
+
|
720
|
+
On a global level you can turn PaperTrail off like this:
|
721
|
+
|
722
|
+
```ruby
|
723
|
+
>> PaperTrail.enabled = false
|
724
|
+
```
|
725
|
+
|
726
|
+
For example, you might want to disable PaperTrail in your Rails application's test environment to speed up your tests. This will do it:
|
727
|
+
|
728
|
+
```ruby
|
729
|
+
# in config/environments/test.rb
|
730
|
+
config.after_initialize do
|
731
|
+
PaperTrail.enabled = false
|
732
|
+
end
|
733
|
+
```
|
734
|
+
|
735
|
+
If you disable PaperTrail in your test environment but want to enable it for specific tests, you can add a helper like this to your test helper:
|
736
|
+
|
737
|
+
```ruby
|
738
|
+
# in test/test_helper.rb
|
739
|
+
def with_versioning
|
740
|
+
was_enabled = PaperTrail.enabled?
|
741
|
+
PaperTrail.enabled = true
|
742
|
+
begin
|
743
|
+
yield
|
744
|
+
ensure
|
745
|
+
PaperTrail.enabled = was_enabled
|
746
|
+
end
|
747
|
+
end
|
748
|
+
```
|
749
|
+
|
750
|
+
And then use it in your tests like this:
|
751
|
+
|
752
|
+
```ruby
|
753
|
+
test "something that needs versioning" do
|
754
|
+
with_versioning do
|
755
|
+
# your test
|
756
|
+
end
|
757
|
+
end
|
758
|
+
```
|
759
|
+
|
760
|
+
### Per request
|
761
|
+
|
762
|
+
You can turn PaperTrail on or off per request by adding a `paper_trail_enabled_for_controller` method to your controller which returns true or false:
|
763
|
+
|
764
|
+
```ruby
|
765
|
+
class ApplicationController < ActionController::Base
|
766
|
+
def paper_trail_enabled_for_controller
|
767
|
+
request.user_agent != 'Disable User-Agent'
|
768
|
+
end
|
769
|
+
end
|
770
|
+
```
|
771
|
+
|
772
|
+
### Per class
|
773
|
+
|
774
|
+
If you are about change some widgets and you don't want a paper trail of your changes, you can turn PaperTrail off like this:
|
775
|
+
|
776
|
+
```ruby
|
777
|
+
>> Widget.paper_trail_off
|
778
|
+
```
|
779
|
+
|
780
|
+
And on again like this:
|
781
|
+
|
782
|
+
```ruby
|
783
|
+
>> Widget.paper_trail_on
|
784
|
+
```
|
785
|
+
|
786
|
+
### Per method call
|
787
|
+
|
788
|
+
You can call a method without creating a new version using `without_versioning`. It takes either a method name as a symbol:
|
789
|
+
|
790
|
+
```ruby
|
791
|
+
@widget.without_versioning :destroy
|
792
|
+
```
|
793
|
+
|
794
|
+
Or a block:
|
795
|
+
|
796
|
+
```ruby
|
797
|
+
@widget.without_versioning do
|
798
|
+
@widget.update_attributes :name => 'Ford'
|
799
|
+
end
|
800
|
+
```
|
801
|
+
|
802
|
+
## Using a custom serializer
|
803
|
+
|
804
|
+
By default, PaperTrail stores your changes as a YAML dump. You can override this with the serializer config option:
|
805
|
+
|
806
|
+
```ruby
|
807
|
+
>> PaperTrail.serializer = MyCustomSerializer
|
808
|
+
```
|
809
|
+
|
810
|
+
A valid serializer is a `module` (or `class`) that defines a `load` and `dump` method. These serializers are included in the gem for your convenience:
|
811
|
+
|
812
|
+
* [Yaml](https://github.com/airblade/paper_trail/blob/master/lib/paper_trail/serializers/yaml.rb) - Default
|
813
|
+
* [Json](https://github.com/airblade/paper_trail/blob/master/lib/paper_trail/serializers/json.rb)
|
814
|
+
|
815
|
+
## Limiting the number of versions created per object instance
|
816
|
+
|
817
|
+
If you are weary of your `versions` table growing to an unwieldy size, or just don't care to track more than a certain number of versions per object,
|
818
|
+
there is a configuration option that can be set to cap the number of versions saved per object. Note that this value must be numeric, and it only applies to
|
819
|
+
versions other than `create` events (which will always be preserved if they are stored).
|
820
|
+
|
821
|
+
```ruby
|
822
|
+
# will make it so that a maximum of 4 versions will be stored for each object (the 3 most recent ones plus a `create` event)
|
823
|
+
>> PaperTrail.config.version_limit = 3
|
824
|
+
# disables/removes the version limit
|
825
|
+
>> PaperTrail.config.version_limit = nil
|
826
|
+
```
|
827
|
+
|
828
|
+
## Deleting Old Versions
|
829
|
+
|
830
|
+
Over time your `versions` table will grow to an unwieldy size. Because each version is self-contained (see the Diffing section above for more) you can simply delete any records you don't want any more. For example:
|
831
|
+
|
832
|
+
```sql
|
833
|
+
sql> delete from versions where created_at < 2010-06-01;
|
834
|
+
```
|
835
|
+
|
836
|
+
```ruby
|
837
|
+
>> PaperTrail::Version.delete_all ["created_at < ?", 1.week.ago]
|
838
|
+
```
|
839
|
+
|
840
|
+
## Testing
|
841
|
+
|
842
|
+
You may want to turn PaperTrail off to speed up your tests. See the [Turning PaperTrail Off/On](#turning-papertrail-offon) section above.
|
843
|
+
|
844
|
+
### RSpec
|
845
|
+
|
846
|
+
PaperTrail provides a helper that works with RSpec to make it easier to control when `PaperTrail` during testing. By default, PaperTrail will be
|
847
|
+
turned off for all tests. When you wish to enable PaperTrail for a test you can either wrap the test in a `with_versioning` block, or pass
|
848
|
+
in `:versioning => true` option to a spec block, like so:
|
849
|
+
|
850
|
+
```ruby
|
851
|
+
describe "RSpec test group" do
|
852
|
+
it 'by default, PaperTrail will be turned off' do
|
853
|
+
PaperTrail.should_not be_enabled
|
854
|
+
end
|
855
|
+
|
856
|
+
with_versioning do
|
857
|
+
it 'within a `with_versioning` block it will be turned on' do
|
858
|
+
PaperTrail.should be_enabled
|
859
|
+
end
|
860
|
+
end
|
861
|
+
|
862
|
+
it 'can be turned on at the `it` or `describe` level like this', :versioning => true do
|
863
|
+
PaperTrail.should be_enabled
|
864
|
+
end
|
865
|
+
end
|
866
|
+
```
|
867
|
+
|
868
|
+
The helper will also reset the `PaperTrail.whodunnit` value to `nil` before each test to help prevent data spillover between tests.
|
869
|
+
If you are using PaperTrail with Rails, the helper will automatically set the `PaperTrail.controller_info` value to `{}` as well, again, to help prevent data spillover between tests.
|
870
|
+
|
871
|
+
There is also a `be_versioned` matcher provided by PaperTrail's RSpec helper which can be leveraged like so:
|
872
|
+
|
873
|
+
```ruby
|
874
|
+
class Widget < ActiveRecord::Base
|
875
|
+
end
|
876
|
+
|
877
|
+
describe Widget do
|
878
|
+
it { should_not be_versioned }
|
879
|
+
|
880
|
+
describe "add versioning to the `Widget` class" do
|
881
|
+
before(:all) do
|
882
|
+
class Widget < ActiveRecord::Base
|
883
|
+
has_paper_trail
|
884
|
+
end
|
885
|
+
end
|
886
|
+
|
887
|
+
it { should be_versioned }
|
888
|
+
end
|
889
|
+
end
|
890
|
+
```
|
891
|
+
|
892
|
+
### Cucumber
|
893
|
+
|
894
|
+
PaperTrail provides a helper that works similar to the RSpec helper.
|
895
|
+
By default, PaperTrail will be turned off for all scenarios by a `before` hook added by the helper.
|
896
|
+
When you wish to enable PaperTrail for a scenario, you can wrap code in a `with_versioning` block in a step, like so:
|
897
|
+
|
898
|
+
```ruby
|
899
|
+
Given /I want versioning on my model/ do
|
900
|
+
with_versioning do
|
901
|
+
# PaperTrail will be turned on for all code inside of this block
|
902
|
+
end
|
903
|
+
end
|
904
|
+
```
|
905
|
+
|
906
|
+
The helper will also reset the `PaperTrail.whodunnit` value to `nil` before each test to help prevent data spillover between tests.
|
907
|
+
If you are using PaperTrail with Rails, the helper will automatically set the `PaperTrail.controller_info` value to `{}` as well, again, to help prevent data spillover between tests.
|
908
|
+
|
909
|
+
## Articles
|
910
|
+
|
911
|
+
[Keep a Paper Trail with PaperTrail](http://www.linux-mag.com/id/7528), Linux Magazine, 16th September 2009.
|
912
|
+
|
913
|
+
|
914
|
+
## Problems
|
915
|
+
|
916
|
+
Please use GitHub's [issue tracker](http://github.com/airblade/paper_trail/issues).
|
917
|
+
|
918
|
+
|
919
|
+
## Contributors
|
920
|
+
|
921
|
+
Many thanks to:
|
922
|
+
|
923
|
+
* [Zachery Hostens](http://github.com/zacheryph)
|
924
|
+
* [Jeremy Weiskotten](http://github.com/jeremyw)
|
925
|
+
* [Phan Le](http://github.com/revo)
|
926
|
+
* [jdrucza](http://github.com/jdrucza)
|
927
|
+
* [conickal](http://github.com/conickal)
|
928
|
+
* [Thibaud Guillaume-Gentil](http://github.com/thibaudgg)
|
929
|
+
* Danny Trelogan
|
930
|
+
* [Mikl Kurkov](http://github.com/mkurkov)
|
931
|
+
* [Franco Catena](https://github.com/francocatena)
|
932
|
+
* [Emmanuel Gomez](https://github.com/emmanuel)
|
933
|
+
* [Matthew MacLeod](https://github.com/mattmacleod)
|
934
|
+
* [benzittlau](https://github.com/benzittlau)
|
935
|
+
* [Tom Derks](https://github.com/EgoH)
|
936
|
+
* [Jonas Hoglund](https://github.com/jhoglund)
|
937
|
+
* [Stefan Huber](https://github.com/MSNexploder)
|
938
|
+
* [thinkcast](https://github.com/thinkcast)
|
939
|
+
* [Dominik Sander](https://github.com/dsander)
|
940
|
+
* [Burke Libbey](https://github.com/burke)
|
941
|
+
* [6twenty](https://github.com/6twenty)
|
942
|
+
* [nir0](https://github.com/nir0)
|
943
|
+
* [Eduard Tsech](https://github.com/edtsech)
|
944
|
+
* [Mathieu Arnold](https://github.com/mat813)
|
945
|
+
* [Nicholas Thrower](https://github.com/throwern)
|
946
|
+
* [Benjamin Curtis](https://github.com/stympy)
|
947
|
+
* [Peter Harkins](https://github.com/pushcx)
|
948
|
+
* [Mohd Amree](https://github.com/amree)
|
949
|
+
* [Nikita Cernovs](https://github.com/nikitachernov)
|
950
|
+
* [Jason Noble](https://github.com/jasonnoble)
|
951
|
+
* [Jared Mehle](https://github.com/jrmehle)
|
952
|
+
* [Eric Schwartz](https://github.com/emschwar)
|
953
|
+
* [Ben Woosley](https://github.com/Empact)
|
954
|
+
* [Philip Arndt](https://github.com/parndt)
|
955
|
+
* [Daniel Vydra](https://github.com/dvydra)
|
956
|
+
* [Byron Bowerman](https://github.com/BM5k)
|
957
|
+
* [Nicolas Buduroi](https://github.com/budu)
|
958
|
+
* [Pikender Sharma](https://github.com/pikender)
|
959
|
+
* [Paul Brannan](https://github.com/cout)
|
960
|
+
* [Ben Morrall](https://github.com/bmorrall)
|
961
|
+
* [Yves Senn](https://github.com/senny)
|
962
|
+
* [Ben Atkins](https://github.com/fullbridge-batkins)
|
963
|
+
* [Yves Senn](https://github.com/senny)
|
964
|
+
* [Tyler Rick](https://github.com/TylerRick)
|
965
|
+
* [Bradley Priest](https://github.com/bradleypriest)
|
966
|
+
* [David Butler](https://github.com/dwbutler)
|
967
|
+
* [Paul Belt](https://github.com/belt)
|
968
|
+
|
969
|
+
|
970
|
+
## Inspirations
|
971
|
+
|
972
|
+
* [Simply Versioned](http://github.com/github/simply_versioned)
|
973
|
+
* [Acts As Audited](http://github.com/collectiveidea/acts_as_audited)
|
974
|
+
|
975
|
+
|
976
|
+
## Intellectual Property
|
977
|
+
|
978
|
+
Copyright (c) 2011 Andy Stewart (boss@airbladesoftware.com).
|
979
|
+
Released under the MIT licence.
|