2015-08-06 10:47:23 -04:00
|
|
|
# PaperTrail
|
|
|
|
|
2015-10-14 15:51:03 -04:00
|
|
|
[![Build Status][4]][5] [![Dependency Status][6]][7]
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2015-08-06 11:45:23 -04:00
|
|
|
Track changes to your models, for auditing or versioning. See how a model looked
|
|
|
|
at any stage in its lifecycle, revert it to any version, or restore it after it
|
|
|
|
has been destroyed.
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2015-11-27 21:02:02 -05:00
|
|
|
## Documentation
|
|
|
|
|
|
|
|
| Version | Documentation |
|
|
|
|
| -------------- | ------------- |
|
2016-12-04 17:13:20 -05:00
|
|
|
| Unreleased | https://github.com/airblade/paper_trail/blob/master/README.md |
|
2017-10-04 22:12:52 -04:00
|
|
|
| 8.0.0 | https://github.com/airblade/paper_trail/blob/v8.0.0/README.md |
|
2017-09-19 01:42:20 -04:00
|
|
|
| 7.1.3 | https://github.com/airblade/paper_trail/blob/v7.1.3/README.md |
|
2016-12-13 23:09:50 -05:00
|
|
|
| 6.0.2 | https://github.com/airblade/paper_trail/blob/v6.0.2/README.md |
|
2016-11-29 16:32:56 -05:00
|
|
|
| 5.2.3 | https://github.com/airblade/paper_trail/blob/v5.2.3/README.md |
|
2016-06-22 16:21:32 -04:00
|
|
|
| 4.2.0 | https://github.com/airblade/paper_trail/blob/v4.2.0/README.md |
|
|
|
|
| 3.0.9 | https://github.com/airblade/paper_trail/blob/v3.0.9/README.md |
|
|
|
|
| 2.7.2 | https://github.com/airblade/paper_trail/blob/v2.7.2/README.md |
|
|
|
|
| 1.6.5 | https://github.com/airblade/paper_trail/blob/v1.6.5/README.md |
|
2015-11-27 21:02:02 -05:00
|
|
|
|
|
|
|
## Table of Contents
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
- [1. Introduction](#1-introduction)
|
|
|
|
- [1.a. Compatibility](#1a-compatibility)
|
|
|
|
- [1.b. Installation](#1b-installation)
|
|
|
|
- [1.c. Basic Usage](#1c-basic-usage)
|
|
|
|
- [1.d. API Summary](#1d-api-summary)
|
2016-08-25 14:31:03 -04:00
|
|
|
- [1.e. Configuration](#1e-configuration)
|
2016-05-10 23:43:33 -04:00
|
|
|
- [2. Limiting What is Versioned, and When](#2-limiting-what-is-versioned-and-when)
|
|
|
|
- [2.a. Choosing Lifecycle Events To Monitor](#2a-choosing-lifecycle-events-to-monitor)
|
|
|
|
- [2.b. Choosing When To Save New Versions](#2b-choosing-when-to-save-new-versions)
|
|
|
|
- [2.c. Choosing Attributes To Monitor](#2c-choosing-attributes-to-monitor)
|
|
|
|
- [2.d. Turning PaperTrail Off](#2d-turning-papertrail-off)
|
|
|
|
- [2.e. Limiting the Number of Versions Created](#2e-limiting-the-number-of-versions-created)
|
|
|
|
- [3. Working With Versions](#3-working-with-versions)
|
|
|
|
- [3.a. Reverting And Undeleting A Model](#3a-reverting-and-undeleting-a-model)
|
|
|
|
- [3.b. Navigating Versions](#3b-navigating-versions)
|
|
|
|
- [3.c. Diffing Versions](#3c-diffing-versions)
|
|
|
|
- [3.d. Deleting Old Versions](#3d-deleting-old-versions)
|
|
|
|
- [4. Saving More Information About Versions](#4-saving-more-information-about-versions)
|
|
|
|
- [4.a. Finding Out Who Was Responsible For A Change](#4a-finding-out-who-was-responsible-for-a-change)
|
|
|
|
- [4.b. Associations](#4b-associations)
|
2016-08-08 13:43:14 -04:00
|
|
|
- [4.b.1. Known Issues](#4b1-known-issues)
|
2016-05-10 23:43:33 -04:00
|
|
|
- [4.c. Storing metadata](#4c-storing-metadata)
|
|
|
|
- [5. ActiveRecord](#5-activerecord)
|
|
|
|
- [5.a. Single Table Inheritance](#5a-single-table-inheritance-sti)
|
|
|
|
- [5.b. Configuring the `versions` Association](#5b-configuring-the-versions-association)
|
2016-05-11 00:06:31 -04:00
|
|
|
- [5.c. Generators](#5c-generators)
|
2016-12-04 16:03:23 -05:00
|
|
|
- [5.d. Protected Attributes](#5d-protected-attributes)
|
2016-05-10 23:43:33 -04:00
|
|
|
- [6. Extensibility](#6-extensibility)
|
|
|
|
- [6.a. Custom Version Classes](#6a-custom-version-classes)
|
|
|
|
- [6.b. Custom Serializer](#6b-custom-serializer)
|
|
|
|
- [7. Testing](#7-testing)
|
2016-10-18 11:22:15 -04:00
|
|
|
- [7.a Minitest](#7a-minitest)
|
|
|
|
- [7.b RSpec](#7b-rspec)
|
|
|
|
- [7.c Cucumber](#7c-cucumber)
|
|
|
|
- [7.d Spork](#7d-spork)
|
|
|
|
- [7.e Zeus or Spring](#7e-zeus-or-spring)
|
2017-04-27 13:39:14 -04:00
|
|
|
- [8. Integration with Other Libraries](#8-integration-with-other-libraries)
|
2016-05-10 23:43:33 -04:00
|
|
|
|
|
|
|
## 1. Introduction
|
|
|
|
|
|
|
|
### 1.a. Compatibility
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2016-11-30 12:03:22 -05:00
|
|
|
| paper_trail | branch | tags | ruby | activerecord |
|
|
|
|
| -------------- | ---------- | ------ | -------- | ------------- |
|
2017-09-20 04:31:50 -04:00
|
|
|
| unreleased | master | | >= 2.2.0 | >= 4.2, < 6 |
|
2017-10-04 22:12:52 -04:00
|
|
|
| 8 | master | v8.x | >= 2.2.0 | >= 4.2, < 6 |
|
2017-04-01 03:05:50 -04:00
|
|
|
| 7 | 7-stable | v7.x | >= 2.1.0 | >= 4.0, < 6 |
|
2016-12-30 11:47:50 -05:00
|
|
|
| 6 | 6-stable | v6.x | >= 1.9.3 | >= 4.0, < 6 |
|
2016-11-30 12:03:22 -05:00
|
|
|
| 5 | 5-stable | v5.x | >= 1.9.3 | >= 3.0, < 5.1 |
|
|
|
|
| 4 | 4-stable | v4.x | >= 1.8.7 | >= 3.0, < 5.1 |
|
|
|
|
| 3 | 3.0-stable | v3.x | >= 1.8.7 | >= 3.0, < 5 |
|
|
|
|
| 2 | 2.7-stable | v2.x | >= 1.8.7 | >= 3.0, < 4 |
|
|
|
|
| 1 | rails2 | v1.x | >= 1.8.7 | >= 2.3, < 3 |
|
2013-08-07 10:51:41 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 1.b. Installation
|
2013-08-07 10:51:41 -04:00
|
|
|
|
2014-05-19 22:25:02 -04:00
|
|
|
1. Add PaperTrail to your `Gemfile`.
|
2013-08-07 10:51:41 -04:00
|
|
|
|
2016-01-20 12:04:52 -05:00
|
|
|
`gem 'paper_trail'`
|
2013-08-07 10:51:41 -04:00
|
|
|
|
2016-08-25 14:31:03 -04:00
|
|
|
1. Add a `versions` table to your database and an initializer file for configuration:
|
2013-08-07 10:51:41 -04:00
|
|
|
|
2015-10-18 00:59:45 -04:00
|
|
|
```
|
|
|
|
bundle exec rails generate paper_trail:install
|
|
|
|
bundle exec rake db:migrate
|
|
|
|
```
|
2016-02-21 21:31:59 -05:00
|
|
|
|
|
|
|
If using [rails_admin][38], you must enable the experimental
|
2017-05-15 02:13:25 -04:00
|
|
|
[Associations](#4b-associations) feature. For more information on this
|
2016-05-11 00:06:31 -04:00
|
|
|
generator, see [section 5.c. Generators](#5c-generators).
|
2013-08-07 10:51:41 -04:00
|
|
|
|
2015-10-18 00:59:45 -04:00
|
|
|
1. Add `has_paper_trail` to the models you want to track.
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2016-01-20 12:04:52 -05:00
|
|
|
```ruby
|
|
|
|
class Widget < ActiveRecord::Base
|
|
|
|
has_paper_trail
|
|
|
|
end
|
|
|
|
```
|
2015-10-18 01:10:31 -04:00
|
|
|
|
2016-01-20 12:04:52 -05:00
|
|
|
1. If your controllers have a `current_user` method, you can easily [track who
|
2016-06-30 07:42:30 -04:00
|
|
|
is responsible for changes](#4a-finding-out-who-was-responsible-for-a-change)
|
2016-01-20 12:04:52 -05:00
|
|
|
by adding a controller callback.
|
2015-10-18 01:10:31 -04:00
|
|
|
|
2016-01-20 12:04:52 -05:00
|
|
|
```ruby
|
|
|
|
class ApplicationController
|
2016-09-02 21:49:17 -04:00
|
|
|
before_action :set_paper_trail_whodunnit
|
2016-01-20 12:04:52 -05:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 1.c. Basic Usage
|
2015-10-18 01:10:31 -04:00
|
|
|
|
2016-01-20 12:04:52 -05:00
|
|
|
Your models now have a `versions` method which returns the "paper trail" of
|
|
|
|
changes to your model.
|
2015-10-18 01:10:31 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
widget = Widget.find 42
|
|
|
|
widget.versions
|
|
|
|
# [<PaperTrail::Version>, <PaperTrail::Version>, ...]
|
|
|
|
```
|
|
|
|
|
|
|
|
Once you have a version, you can find out what happened:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
v = widget.versions.last
|
|
|
|
v.event # 'update', 'create', or 'destroy'
|
|
|
|
v.created_at # When the `event` occurred
|
|
|
|
v.whodunnit # If the update was via a controller and the
|
|
|
|
# controller has a current_user method, returns the
|
|
|
|
# id of the current user as a string.
|
|
|
|
widget = v.reify # The widget as it was before the update
|
|
|
|
# (nil for a create event)
|
|
|
|
```
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
widget = Widget.find 153
|
|
|
|
widget.name # 'Doobly'
|
|
|
|
|
|
|
|
# Add has_paper_trail to Widget model.
|
|
|
|
|
|
|
|
widget.versions # []
|
2017-05-29 12:23:05 -04:00
|
|
|
widget.update_attributes name: 'Wotsit'
|
2015-10-18 01:10:31 -04:00
|
|
|
widget.versions.last.reify.name # 'Doobly'
|
|
|
|
widget.versions.last.event # 'update'
|
|
|
|
```
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
Here's a helpful table showing what PaperTrail stores:
|
|
|
|
|
|
|
|
| *Event* | *create* | *update* | *destroy* |
|
|
|
|
| -------------- | -------- | -------- | --------- |
|
|
|
|
| *Model Before* | nil | widget | widget |
|
|
|
|
| *Model After* | widget | widget | nil |
|
|
|
|
|
|
|
|
PaperTrail stores the values in the Model Before column. Most other
|
|
|
|
auditing/versioning plugins store the After column.
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 1.d. API Summary
|
2010-10-28 06:14:35 -04:00
|
|
|
|
|
|
|
When you declare `has_paper_trail` in your model, you get these methods:
|
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
|
|
|
class Widget < ActiveRecord::Base
|
2015-09-11 11:09:36 -04:00
|
|
|
has_paper_trail
|
2011-12-10 07:09:18 -05:00
|
|
|
end
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2015-09-11 11:09:36 -04:00
|
|
|
# Returns this widget's versions. You can customise the name of the
|
2016-05-09 18:08:28 -04:00
|
|
|
# association, but overriding this method is not supported.
|
2011-12-10 07:09:18 -05:00
|
|
|
widget.versions
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Return the version this widget was reified from, or nil if it is live.
|
|
|
|
# You can customise the name of the method.
|
|
|
|
widget.version
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2015-09-11 11:09:36 -04:00
|
|
|
# Returns true if this widget is the current, live one; or false if it is from
|
|
|
|
# a previous version.
|
2016-02-23 17:59:14 -05:00
|
|
|
widget.paper_trail.live?
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Returns who put the widget into its current state.
|
2016-02-23 17:59:14 -05:00
|
|
|
widget.paper_trail.originator
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Returns the widget (not a version) as it looked at the given timestamp.
|
2016-02-23 17:59:14 -05:00
|
|
|
widget.paper_trail.version_at(timestamp)
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Returns the widget (not a version) as it was most recently.
|
2016-02-23 17:59:14 -05:00
|
|
|
widget.paper_trail.previous_version
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Returns the widget (not a version) as it became next.
|
2016-02-23 17:59:14 -05:00
|
|
|
widget.paper_trail.next_version
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2015-09-11 11:09:36 -04:00
|
|
|
# Generates a version for a `touch` event (`widget.touch` does NOT generate a
|
|
|
|
# version)
|
2016-02-23 17:59:14 -05:00
|
|
|
widget.paper_trail.touch_with_version
|
2014-02-21 18:20:40 -05:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Turn PaperTrail off for all widgets.
|
2016-02-23 17:59:14 -05:00
|
|
|
Widget.paper_trail.disable
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Turn PaperTrail on for all widgets.
|
2016-02-23 17:59:14 -05:00
|
|
|
Widget.paper_trail.enable
|
2014-02-21 18:20:40 -05:00
|
|
|
|
2015-09-11 11:09:36 -04:00
|
|
|
# Is PaperTrail enabled for Widget, the class?
|
2016-02-23 17:59:14 -05:00
|
|
|
Widget.paper_trail.enabled?
|
2015-09-11 11:09:36 -04:00
|
|
|
|
|
|
|
# Is PaperTrail enabled for widget, the instance?
|
2016-02-23 17:59:14 -05:00
|
|
|
widget.paper_trail.enabled_for_model?
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2016-05-02 12:19:18 -04:00
|
|
|
And a `PaperTrail::Version` instance (which is just an ordinary ActiveRecord
|
2016-04-26 08:26:42 -04:00
|
|
|
instance, with all the usual methods) adds these methods:
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
|
|
|
# Returns the item restored from this version.
|
|
|
|
version.reify(options = {})
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2014-07-18 04:47:21 -04:00
|
|
|
# Return a new item from this version
|
|
|
|
version.reify(dup: true)
|
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Returns who put the item into the state stored in this version.
|
2015-05-08 13:28:47 -04:00
|
|
|
version.paper_trail_originator
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Returns who changed the item from the state it had in this version.
|
|
|
|
version.terminator
|
|
|
|
version.whodunnit
|
2013-10-16 11:41:13 -04:00
|
|
|
version.version_author
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Returns the next version.
|
|
|
|
version.next
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Returns the previous version.
|
|
|
|
version.previous
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Returns the index of this version in all the versions.
|
|
|
|
version.index
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Returns the event that caused this version (create|update|destroy).
|
|
|
|
version.event
|
2014-09-21 10:29:14 -04:00
|
|
|
|
2016-05-10 02:11:41 -04:00
|
|
|
# Query the `versions.object` column (or `object_changes` column), by
|
|
|
|
# attributes, using the SQL LIKE operator. Known issue: inconsistent results for
|
|
|
|
# numeric values due to limitations of SQL wildcard matchers against the
|
|
|
|
# serialized objects.
|
2014-09-21 10:29:14 -04:00
|
|
|
PaperTrail::Version.where_object(attr1: val1, attr2: val2)
|
2015-01-07 16:57:45 -05:00
|
|
|
PaperTrail::Version.where_object_changes(attr1: val1)
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-10-28 06:14:35 -04:00
|
|
|
|
|
|
|
In your controllers you can override these methods:
|
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
|
|
|
# Returns the user who is responsible for any changes that occur.
|
|
|
|
# Defaults to current_user.
|
|
|
|
user_for_paper_trail
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
# Returns any information about the controller or request that you want
|
|
|
|
# PaperTrail to store alongside any changes that occur.
|
|
|
|
info_for_paper_trail
|
|
|
|
```
|
2010-10-28 06:14:35 -04:00
|
|
|
|
2016-08-25 14:31:03 -04:00
|
|
|
### 1.e. Configuration
|
|
|
|
|
|
|
|
Many aspects of PaperTrail are configurable for individual models; typically
|
|
|
|
this is achieved by passing options to the `has_paper_trail` method within
|
|
|
|
a given model.
|
|
|
|
|
|
|
|
Some aspects of PaperTrail are configured globally for all models. These
|
|
|
|
settings are assigned directly on the `PaperTrail.config` object.
|
|
|
|
A common place to put these settings is in a Rails initializer file
|
|
|
|
such as `config/initializers/paper_trail.rb` or in an environment-specific
|
|
|
|
configuration file such as `config/environments/test.rb`.
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
## 2. Limiting What is Versioned, and When
|
|
|
|
|
|
|
|
### 2.a. Choosing Lifecycle Events To Monitor
|
2011-08-22 04:24:04 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
You can choose which events to track with the `on` option. For example, to
|
|
|
|
ignore `create` events:
|
2011-08-22 04:24:04 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
|
|
|
class Article < ActiveRecord::Base
|
2017-05-29 12:23:05 -04:00
|
|
|
has_paper_trail on: [:update, :destroy]
|
2011-12-10 07:09:18 -05:00
|
|
|
end
|
|
|
|
```
|
2011-08-22 04:24:04 -04:00
|
|
|
|
2015-09-11 11:09:36 -04:00
|
|
|
`has_paper_trail` installs callbacks for these lifecycle events. If there are
|
|
|
|
other callbacks in your model, their order relative to those installed by
|
|
|
|
PaperTrail may matter, so be aware of any potential interactions.
|
|
|
|
|
2016-07-13 15:12:55 -04:00
|
|
|
You may also have the `PaperTrail::Version` model save a custom string in its
|
2015-06-27 00:13:36 -04:00
|
|
|
`event` field instead of the typical `create`, `update`, `destroy`. PaperTrail
|
|
|
|
supplies a custom accessor method called `paper_trail_event`, which it will
|
|
|
|
attempt to use to fill the `event` field before falling back on one of the
|
|
|
|
default events.
|
2013-05-13 11:38:53 -04:00
|
|
|
|
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
a = Article.create
|
|
|
|
a.versions.size # 1
|
|
|
|
a.versions.last.event # 'create'
|
|
|
|
a.paper_trail_event = 'update title'
|
2017-05-29 12:23:05 -04:00
|
|
|
a.update_attributes title: 'My Title'
|
2015-06-27 00:23:14 -04:00
|
|
|
a.versions.size # 2
|
|
|
|
a.versions.last.event # 'update title'
|
|
|
|
a.paper_trail_event = nil
|
2017-05-29 12:23:05 -04:00
|
|
|
a.update_attributes title: 'Alternate'
|
2015-06-27 00:23:14 -04:00
|
|
|
a.versions.size # 3
|
|
|
|
a.versions.last.event # 'update'
|
2013-05-13 11:38:53 -04:00
|
|
|
```
|
2011-08-22 04:24:04 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
#### Controlling the Order of AR Callbacks
|
2015-10-09 04:54:42 -04:00
|
|
|
|
2015-10-10 00:13:45 -04:00
|
|
|
The `has_paper_trail` method installs AR callbacks. If you need to control
|
|
|
|
their order, use the `paper_trail_on_*` methods.
|
2015-09-10 11:40:59 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Article < ActiveRecord::Base
|
2016-01-18 13:08:30 -05:00
|
|
|
|
|
|
|
# Include PaperTrail, but do not add any callbacks yet. Passing the
|
|
|
|
# empty array to `:on` omits callbacks.
|
2017-05-29 12:23:05 -04:00
|
|
|
has_paper_trail on: []
|
2016-01-18 13:08:30 -05:00
|
|
|
|
|
|
|
# Add callbacks in the order you need.
|
2016-02-23 17:59:14 -05:00
|
|
|
paper_trail.on_destroy # add destroy callback
|
|
|
|
paper_trail.on_update # etc.
|
|
|
|
paper_trail.on_create
|
2015-09-10 11:40:59 -04:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2016-02-23 17:59:14 -05:00
|
|
|
The `paper_trail.on_destroy` method can be further configured to happen
|
2016-01-18 13:08:30 -05:00
|
|
|
`:before` or `:after` the destroy event. In PaperTrail 4, the default is
|
|
|
|
`:after`. In PaperTrail 5, the default will be `:before`, to support
|
|
|
|
ActiveRecord 5. (see https://github.com/airblade/paper_trail/pull/683)
|
2015-09-10 11:40:59 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 2.b. Choosing When To Save New Versions
|
2012-01-11 09:26:10 -05:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
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:
|
2012-01-11 09:26:10 -05:00
|
|
|
|
2012-10-12 19:05:27 -04:00
|
|
|
```ruby
|
|
|
|
class Translation < ActiveRecord::Base
|
2017-05-29 12:23:05 -04:00
|
|
|
has_paper_trail if: Proc.new { |t| t.language_code == 'US' },
|
|
|
|
unless: Proc.new { |t| t.type == 'DRAFT' }
|
2012-10-12 19:05:27 -04:00
|
|
|
end
|
|
|
|
```
|
2012-01-11 09:26:10 -05:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
#### Choosing Based on Changed Attributes
|
2016-04-08 18:54:37 -04:00
|
|
|
|
|
|
|
Starting with PaperTrail 4.0, versions are saved during an after-callback. If
|
|
|
|
you decide whether to save a new version based on changed attributes, please
|
|
|
|
use attribute_name_was instead of attribute_name.
|
2011-08-22 04:24:04 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 2.c. Choosing Attributes To Monitor
|
2009-10-28 09:12:36 -04:00
|
|
|
|
2017-03-09 16:48:36 -05:00
|
|
|
#### Ignore
|
|
|
|
|
|
|
|
You can `ignore` changes to certain attributes:
|
2009-10-28 09:12:36 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
|
|
|
class Article < ActiveRecord::Base
|
2017-03-09 16:48:36 -05:00
|
|
|
has_paper_trail ignore: [:title, :rating]
|
2011-12-10 07:09:18 -05:00
|
|
|
end
|
|
|
|
```
|
2009-10-28 09:12:36 -04:00
|
|
|
|
2017-03-09 16:48:36 -05:00
|
|
|
Changes to just the `title` or `rating` will not create a version record.
|
|
|
|
Changes to other attributes will create a version record.
|
2009-10-28 09:12:36 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
a = Article.create
|
|
|
|
a.versions.length # 1
|
2017-03-09 16:48:36 -05:00
|
|
|
a.update_attributes title: 'My Title', rating: 3
|
2015-06-27 00:23:14 -04:00
|
|
|
a.versions.length # 1
|
2017-03-09 16:48:36 -05:00
|
|
|
a.update_attributes title: 'Greeting', content: 'Hello'
|
2015-06-27 00:23:14 -04:00
|
|
|
a.versions.length # 2
|
2016-02-23 17:59:14 -05:00
|
|
|
a.paper_trail.previous_version.title # 'My Title'
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2009-10-28 09:12:36 -04:00
|
|
|
|
2017-03-09 16:48:36 -05:00
|
|
|
#### Only
|
|
|
|
|
|
|
|
Or, you can specify a list of the `only` attributes you care about:
|
2011-02-25 09:36:32 -05:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
|
|
|
class Article < ActiveRecord::Base
|
2017-03-09 16:48:36 -05:00
|
|
|
has_paper_trail only: [:title]
|
2011-12-10 07:09:18 -05:00
|
|
|
end
|
|
|
|
```
|
2011-02-25 09:36:32 -05:00
|
|
|
|
2017-03-09 16:48:36 -05:00
|
|
|
Only changes to the `title` will create a version record.
|
2011-02-25 09:36:32 -05:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
a = Article.create
|
|
|
|
a.versions.length # 1
|
2017-03-09 16:48:36 -05:00
|
|
|
a.update_attributes title: 'My Title'
|
2015-06-27 00:23:14 -04:00
|
|
|
a.versions.length # 2
|
2017-03-09 16:48:36 -05:00
|
|
|
a.update_attributes content: 'Hello'
|
2015-06-27 00:23:14 -04:00
|
|
|
a.versions.length # 2
|
2016-02-23 17:59:14 -05:00
|
|
|
a.paper_trail.previous_version.content # nil
|
2013-09-19 16:18:02 -04:00
|
|
|
```
|
|
|
|
|
2017-03-09 16:48:36 -05:00
|
|
|
The `:ignore` and `:only` options can also accept `Hash` arguments.
|
2013-09-19 16:18:02 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Article < ActiveRecord::Base
|
2017-03-09 16:48:36 -05:00
|
|
|
has_paper_trail only: { title: Proc.new { |obj| !obj.title.blank? } }
|
2013-09-19 16:18:02 -04:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2017-03-09 16:48:36 -05:00
|
|
|
If the `title` is not blank, then only changes to the `title`
|
|
|
|
will create a version record.
|
2013-09-19 16:18:02 -04:00
|
|
|
|
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
a = Article.create
|
|
|
|
a.versions.length # 1
|
2017-03-09 16:48:36 -05:00
|
|
|
a.update_attributes content: 'Hello'
|
2015-06-27 00:23:14 -04:00
|
|
|
a.versions.length # 2
|
2017-03-09 16:48:36 -05:00
|
|
|
a.update_attributes title: 'Title One'
|
2015-06-27 00:23:14 -04:00
|
|
|
a.versions.length # 3
|
2017-03-09 16:48:36 -05:00
|
|
|
a.update_attributes content: 'Hai'
|
2015-06-27 00:23:14 -04:00
|
|
|
a.versions.length # 3
|
2016-02-23 17:59:14 -05:00
|
|
|
a.paper_trail.previous_version.content # "Hello"
|
2017-03-09 16:48:36 -05:00
|
|
|
a.update_attributes title: 'Title Two'
|
2015-06-27 00:23:14 -04:00
|
|
|
a.versions.length # 4
|
2016-02-23 17:59:14 -05:00
|
|
|
a.paper_trail.previous_version.content # "Hai"
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2011-02-25 10:26:41 -05:00
|
|
|
|
2017-03-09 16:48:36 -05:00
|
|
|
Configuring both `:ignore` and `:only` is not recommended, but it should work as
|
|
|
|
expected. 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`.
|
2009-10-28 09:12:36 -04:00
|
|
|
|
2017-03-09 16:48:36 -05:00
|
|
|
#### Skip
|
2011-11-09 05:06:17 -05:00
|
|
|
|
2017-03-09 16:48:36 -05:00
|
|
|
You can skip attributes completely with the `:skip` option. As with `:ignore`,
|
|
|
|
updates to these attributes will not create a version record. In addition, if a
|
|
|
|
version record is created for some other reason, these attributes will not be
|
|
|
|
persisted.
|
2011-11-09 05:06:17 -05:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
|
|
|
class Article < ActiveRecord::Base
|
2017-05-29 12:23:05 -04:00
|
|
|
has_paper_trail skip: [:file_upload]
|
2011-12-10 07:09:18 -05:00
|
|
|
end
|
|
|
|
```
|
2011-02-25 10:26:41 -05:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 2.d. Turning PaperTrail Off
|
2015-08-06 10:59:54 -04:00
|
|
|
|
2016-03-05 16:48:58 -05:00
|
|
|
PaperTrail is on by default, but sometimes you don't want to record versions.
|
2015-08-06 10:59:54 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
#### Per Process
|
2015-08-06 10:59:54 -04:00
|
|
|
|
2016-03-05 16:48:58 -05:00
|
|
|
Turn PaperTrail off for all threads in a `ruby` process.
|
2015-08-06 10:59:54 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
PaperTrail.enabled = false
|
|
|
|
```
|
|
|
|
|
2016-06-30 07:42:30 -04:00
|
|
|
This is commonly used to speed up tests. See [Testing](#7-testing) below.
|
2015-08-06 10:59:54 -04:00
|
|
|
|
2016-01-16 16:48:27 -05:00
|
|
|
There is also a rails config option that does the same thing.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# in config/environments/test.rb
|
|
|
|
config.paper_trail.enabled = false
|
|
|
|
```
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
#### Per Request
|
2015-08-06 10:59:54 -04:00
|
|
|
|
2016-03-05 16:48:58 -05:00
|
|
|
Add a `paper_trail_enabled_for_controller` method to your controller.
|
2015-08-06 10:59:54 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
class ApplicationController < ActionController::Base
|
|
|
|
def paper_trail_enabled_for_controller
|
|
|
|
request.user_agent != 'Disable User-Agent'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
#### Per Class
|
2015-08-06 10:59:54 -04:00
|
|
|
|
|
|
|
```ruby
|
2016-02-23 17:59:14 -05:00
|
|
|
Widget.paper_trail.disable
|
|
|
|
Widget.paper_trail.enable
|
2015-08-06 10:59:54 -04:00
|
|
|
```
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
#### Per Method
|
2015-08-06 10:59:54 -04:00
|
|
|
|
|
|
|
You can call a method without creating a new version using `without_versioning`.
|
|
|
|
It takes either a method name as a symbol:
|
|
|
|
|
|
|
|
```ruby
|
2016-02-23 17:59:14 -05:00
|
|
|
@widget.paper_trail.without_versioning :destroy
|
2015-08-06 10:59:54 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
Or a block:
|
|
|
|
|
|
|
|
```ruby
|
2016-02-23 17:59:14 -05:00
|
|
|
@widget.paper_trail.without_versioning do
|
2017-05-29 12:23:05 -04:00
|
|
|
@widget.update_attributes name: 'Ford'
|
2015-08-06 10:59:54 -04:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2016-12-30 11:51:21 -05:00
|
|
|
PaperTrail is disabled for the whole model
|
2016-12-28 09:30:12 -05:00
|
|
|
(e.g. `Widget`), not just for the instance (e.g. `@widget`).
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 2.e. Limiting the Number of Versions Created
|
2015-08-06 10:59:54 -04:00
|
|
|
|
|
|
|
Configure `version_limit` to cap the number of versions saved per record. This
|
|
|
|
does not apply to `create` events.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# Limit: 4 versions per record (3 most recent, plus a `create` event)
|
|
|
|
PaperTrail.config.version_limit = 3
|
|
|
|
# Remove the limit
|
|
|
|
PaperTrail.config.version_limit = nil
|
|
|
|
```
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
## 3. Working With Versions
|
|
|
|
|
|
|
|
### 3.a. Reverting And Undeleting A Model
|
2009-06-23 05:39:16 -04:00
|
|
|
|
2009-06-23 05:59:32 -04:00
|
|
|
PaperTrail makes reverting to a previous version easy:
|
2009-06-23 05:39:16 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
widget = Widget.find 42
|
2017-05-29 12:23:05 -04:00
|
|
|
widget.update_attributes name: 'Blah blah'
|
2011-12-10 07:09:18 -05:00
|
|
|
# Time passes....
|
2016-02-23 17:59:14 -05:00
|
|
|
widget = widget.paper_trail.previous_version # the widget as it was before the update
|
|
|
|
widget.save # reverted
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2009-06-23 05:59:32 -04:00
|
|
|
|
2010-02-18 12:34:10 -05:00
|
|
|
Alternatively you can find the version at a given time:
|
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2016-02-23 17:59:14 -05:00
|
|
|
widget = widget.paper_trail.version_at(1.day.ago) # the widget as it was one day ago
|
|
|
|
widget.save # reverted
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-02-18 12:34:10 -05:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
Note `version_at` gives you the object, not a version, so you don't need to call
|
|
|
|
`reify`.
|
2010-02-18 12:34:10 -05:00
|
|
|
|
2009-06-23 05:59:32 -04:00
|
|
|
Undeleting is just as simple:
|
2009-06-23 05:39:16 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2016-08-23 00:26:03 -04:00
|
|
|
widget = Widget.find(42)
|
2015-06-27 00:23:14 -04:00
|
|
|
widget.destroy
|
2011-12-10 07:09:18 -05:00
|
|
|
# Time passes....
|
2016-08-23 00:26:03 -04:00
|
|
|
versions = widget.versions # versions ordered by versions.created_at, ascending
|
|
|
|
widget = versions.last.reify # the widget as it was before destruction
|
|
|
|
widget.save # the widget lives!
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2009-06-23 05:39:16 -04:00
|
|
|
|
2017-01-12 23:38:08 -05:00
|
|
|
You could even use PaperTrail to implement an undo system; [Ryan Bates has!][3]
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2015-06-22 12:04:39 -04:00
|
|
|
If your model uses [optimistic locking][1] don't forget to [increment your
|
|
|
|
`lock_version`][2] before saving or you'll get a `StaleObjectError`.
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 3.b. Navigating Versions
|
2010-06-22 07:48:36 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
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.
|
2010-06-22 09:41:26 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
live_widget = Widget.find 42
|
2016-02-23 17:59:14 -05:00
|
|
|
live_widget.versions.length # 4, for example
|
|
|
|
widget = live_widget.paper_trail.previous_version # => widget == live_widget.versions.last.reify
|
|
|
|
widget = widget.paper_trail.previous_version # => widget == live_widget.versions[-2].reify
|
|
|
|
widget = widget.paper_trail.next_version # => widget == live_widget.versions.last.reify
|
|
|
|
widget.paper_trail.next_version # live_widget
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-06-22 09:41:26 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
If instead you have a particular `version` of an item you can navigate to the
|
|
|
|
previous and next versions.
|
2010-06-22 07:48:36 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
widget = Widget.find 42
|
|
|
|
version = widget.versions[-2] # assuming widget has several versions
|
|
|
|
previous = version.previous
|
|
|
|
next = version.next
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-06-22 07:48:36 -04:00
|
|
|
|
|
|
|
You can find out which of an item's versions yours is:
|
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
current_version_number = version.index # 0-based
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-06-22 07:48:36 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
If you got an item by reifying one of its versions, you can navigate back to the
|
|
|
|
version it came from:
|
2010-06-22 07:48:36 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
latest_version = Widget.find(42).versions.last
|
|
|
|
widget = latest_version.reify
|
|
|
|
widget.version == latest_version # true
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-06-22 07:48:36 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
You can find out whether a model instance is the current, live one -- or whether
|
|
|
|
it came instead from a previous version -- with `live?`:
|
2010-10-21 07:30:50 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
widget = Widget.find 42
|
|
|
|
widget.live? # true
|
2016-02-23 17:59:14 -05:00
|
|
|
widget = widget.paper_trail.previous_version
|
2015-06-27 00:23:14 -04:00
|
|
|
widget.live? # false
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-06-22 07:48:36 -04:00
|
|
|
|
2014-09-21 10:29:14 -04:00
|
|
|
And you can perform `WHERE` queries for object versions based on attributes:
|
|
|
|
|
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
# All versions that meet these criteria.
|
2017-05-29 12:23:05 -04:00
|
|
|
PaperTrail::Version.where_object(content: 'Hello', title: 'Article')
|
2014-09-21 10:29:14 -04:00
|
|
|
```
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 3.c. Diffing Versions
|
2015-08-06 11:41:39 -04:00
|
|
|
|
|
|
|
There are two scenarios: diffing adjacent versions and diffing non-adjacent
|
|
|
|
versions.
|
|
|
|
|
|
|
|
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 `rails generate paper_trail:install --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:
|
|
|
|
|
|
|
|
```ruby
|
2017-05-29 12:23:05 -04:00
|
|
|
widget = Widget.create name: 'Bob'
|
2015-08-10 00:24:22 -04:00
|
|
|
widget.versions.last.changeset
|
|
|
|
# {
|
|
|
|
# "name"=>[nil, "Bob"],
|
|
|
|
# "created_at"=>[nil, 2015-08-10 04:10:40 UTC],
|
|
|
|
# "updated_at"=>[nil, 2015-08-10 04:10:40 UTC],
|
|
|
|
# "id"=>[nil, 1]
|
|
|
|
# }
|
2017-05-29 12:23:05 -04:00
|
|
|
widget.update_attributes name: 'Robert'
|
2015-08-10 00:24:22 -04:00
|
|
|
widget.versions.last.changeset
|
|
|
|
# {
|
|
|
|
# "name"=>["Bob", "Robert"],
|
|
|
|
# "updated_at"=>[2015-08-10 04:13:19 UTC, 2015-08-10 04:13:19 UTC]
|
|
|
|
# }
|
2015-08-06 11:41:39 -04:00
|
|
|
widget.destroy
|
2015-08-10 00:24:22 -04:00
|
|
|
widget.versions.last.changeset
|
|
|
|
# {}
|
2015-08-06 11:41:39 -04:00
|
|
|
```
|
|
|
|
|
2015-08-10 00:24:22 -04:00
|
|
|
The `object_changes` are only stored for creation and updates, not when an
|
|
|
|
object is destroyed.
|
2015-08-06 11:41:39 -04:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
To diff non-adjacent versions you'll have to write your own code. These
|
|
|
|
libraries may help:
|
|
|
|
|
|
|
|
For diffing two strings:
|
|
|
|
|
|
|
|
* [htmldiff][19]: 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.
|
|
|
|
* [differ][20]: 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.
|
|
|
|
* [diff-lcs][21]: old-school, line-wise diffs.
|
|
|
|
|
|
|
|
For diffing two ActiveRecord objects:
|
|
|
|
|
|
|
|
* [Jeremy Weiskotten's PaperTrail fork][22]: uses ActiveSupport's diff to return
|
|
|
|
an array of hashes of the changes.
|
|
|
|
* [activerecord-diff][23]: rather like ActiveRecord::Dirty but also allows you
|
|
|
|
to specify which columns to compare.
|
|
|
|
|
2017-04-06 11:17:08 -04:00
|
|
|
If you want to selectively record changes for some models but not others you
|
2017-05-29 12:23:05 -04:00
|
|
|
can opt out of recording changes by passing `save_changes: false` to your
|
2015-08-06 11:41:39 -04:00
|
|
|
`has_paper_trail` method declaration.
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 3.d. Deleting Old Versions
|
2015-08-06 11:41:39 -04:00
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
```sql
|
|
|
|
sql> delete from versions where created_at < 2010-06-01;
|
|
|
|
```
|
|
|
|
|
|
|
|
```ruby
|
2017-05-29 12:23:05 -04:00
|
|
|
PaperTrail::Version.delete_all ['created_at < ?', 1.week.ago]
|
2015-08-06 11:41:39 -04:00
|
|
|
```
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
## 4. Saving More Information About Versions
|
|
|
|
|
|
|
|
### 4.a. Finding Out Who Was Responsible For A Change
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2015-06-28 00:00:33 -04:00
|
|
|
Set `PaperTrail.whodunnit=`, and that value will be stored in the version's
|
2015-06-26 23:16:21 -04:00
|
|
|
`whodunnit` column.
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2015-06-28 00:00:33 -04:00
|
|
|
```ruby
|
|
|
|
PaperTrail.whodunnit = 'Andy Stewart'
|
2017-05-29 12:23:05 -04:00
|
|
|
widget.update_attributes name: 'Wibble'
|
2015-06-28 00:00:33 -04:00
|
|
|
widget.versions.last.whodunnit # Andy Stewart
|
|
|
|
```
|
|
|
|
|
2017-02-28 21:12:20 -05:00
|
|
|
`whodunnit` also accepts a block, a convenient way to temporarily set the value.
|
|
|
|
|
|
|
|
```ruby
|
2017-05-29 12:23:05 -04:00
|
|
|
PaperTrail.whodunnit('Dorian Marié') do
|
|
|
|
widget.update_attributes name: 'Wibble'
|
2017-02-28 21:12:20 -05:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2017-07-08 16:39:04 -04:00
|
|
|
`whodunnit` also accepts a `Proc`.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
PaperTrail.whodunnit = proc do
|
|
|
|
caller.first{ |c| c.starts_with? Rails.root.to_s }
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2015-06-28 00:00:33 -04:00
|
|
|
If your controller has a `current_user` method, PaperTrail provides a
|
2016-09-02 21:49:17 -04:00
|
|
|
`before_action` that will assign `current_user.id` to `PaperTrail.whodunnit`.
|
|
|
|
You can add this `before_action` to your `ApplicationController`.
|
2010-03-19 14:53:49 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
|
|
|
class ApplicationController
|
2016-09-02 21:49:17 -04:00
|
|
|
before_action :set_paper_trail_whodunnit
|
2011-12-10 07:09:18 -05:00
|
|
|
end
|
|
|
|
```
|
2010-03-19 14:53:49 -04:00
|
|
|
|
2015-06-28 00:00:33 -04:00
|
|
|
You may want `set_paper_trail_whodunnit` 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:
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-28 00:00:33 -04:00
|
|
|
class ApplicationController
|
|
|
|
def user_for_paper_trail
|
|
|
|
logged_in? ? current_member.id : 'Public user' # or whatever
|
|
|
|
end
|
|
|
|
end
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2015-07-12 22:27:58 -04:00
|
|
|
See also: [Setting whodunnit in the rails console][33]
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
Sometimes you want to define who is responsible for a change in a small scope
|
|
|
|
without overwriting value of `PaperTrail.whodunnit`. It is possible to define
|
|
|
|
the `whodunnit` value for an operation inside a block like this:
|
2014-03-11 17:55:44 -04:00
|
|
|
|
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
PaperTrail.whodunnit = 'Andy Stewart'
|
2016-11-27 20:11:03 -05:00
|
|
|
widget.paper_trail.whodunnit('Lucas Souza') do
|
2017-05-29 12:23:05 -04:00
|
|
|
widget.update_attributes name: 'Wibble'
|
2015-06-27 00:23:14 -04:00
|
|
|
end
|
|
|
|
widget.versions.last.whodunnit # Lucas Souza
|
2017-05-29 12:23:05 -04:00
|
|
|
widget.update_attributes name: 'Clair'
|
2015-06-27 00:23:14 -04:00
|
|
|
widget.versions.last.whodunnit # Andy Stewart
|
2014-03-11 17:55:44 -04:00
|
|
|
```
|
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
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`.
|
2010-06-28 09:03:35 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
To find out who made a version's object look that way, use
|
|
|
|
`version.paper_trail_originator`. And to find out who made a "live" object look
|
|
|
|
like it does, call `paper_trail_originator` on the object.
|
2010-06-28 09:03:35 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
widget = Widget.find 153 # assume widget has 0 versions
|
|
|
|
PaperTrail.whodunnit = 'Alice'
|
2017-05-29 12:23:05 -04:00
|
|
|
widget.update_attributes name: 'Yankee'
|
2016-02-23 17:59:14 -05:00
|
|
|
widget.paper_trail.originator # 'Alice'
|
2015-06-27 00:23:14 -04:00
|
|
|
PaperTrail.whodunnit = 'Bob'
|
2017-05-29 12:23:05 -04:00
|
|
|
widget.update_attributes name: 'Zulu'
|
2016-02-23 17:59:14 -05:00
|
|
|
widget.paper_trail.originator # 'Bob'
|
2015-06-27 00:23:14 -04:00
|
|
|
first_version, last_version = widget.versions.first, widget.versions.last
|
|
|
|
first_version.whodunnit # 'Alice'
|
|
|
|
first_version.paper_trail_originator # nil
|
|
|
|
first_version.terminator # 'Alice'
|
|
|
|
last_version.whodunnit # 'Bob'
|
|
|
|
last_version.paper_trail_originator # 'Alice'
|
|
|
|
last_version.terminator # 'Bob'
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2009-05-27 11:21:20 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
#### Storing an ActiveRecord globalid in whodunnit
|
2016-02-07 06:39:46 -05:00
|
|
|
|
2016-02-21 21:41:54 -05:00
|
|
|
If you would like `whodunnit` to return an `ActiveRecord` object instead of a
|
|
|
|
string, please try the [paper_trail-globalid][37] gem.
|
2016-02-07 06:39:46 -05:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 4.b. Associations
|
2011-03-01 05:58:22 -05:00
|
|
|
|
2016-08-08 13:43:14 -04:00
|
|
|
**Experimental feature**, not recommended for production. See known issues
|
|
|
|
below.
|
2015-08-21 02:01:16 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
PaperTrail can restore three types of associations: Has-One, Has-Many, and
|
2016-08-25 14:31:03 -04:00
|
|
|
Has-Many-Through. In order to do this, you will need to do two things:
|
|
|
|
|
|
|
|
1. Create a `version_associations` table
|
|
|
|
2. Set `PaperTrail.config.track_associations = true` (e.g. in an initializer)
|
|
|
|
|
|
|
|
Both will be done for you automatically if you install PaperTrail with the
|
|
|
|
`--with_associations` option
|
|
|
|
(e.g. `rails generate paper_trail:install --with-associations`)
|
|
|
|
|
|
|
|
If you want to add this functionality after the initial installation, you will
|
|
|
|
need to create the `version_associations` table manually, and you will need to
|
|
|
|
ensure that `PaperTrail.config.track_associations = true` is set.
|
|
|
|
|
|
|
|
PaperTrail will store in the `version_associations` table additional information
|
|
|
|
to correlate versions of the association and versions of the model when the
|
|
|
|
associated record is changed. When reifying the model, PaperTrail can use this
|
|
|
|
table, together with the `transaction_id` to find the correct version of the
|
|
|
|
association and reify it. The `transaction_id` is a unique id for version records
|
|
|
|
created in the same transaction. It is used to associate the version of the model
|
|
|
|
and the version of the association that are created in the same transaction.
|
2011-03-01 05:58:22 -05:00
|
|
|
|
2017-05-29 12:23:05 -04:00
|
|
|
To restore Has-One associations as they were at the time, pass option `has_one:
|
|
|
|
true` to `reify`. To restore Has-Many and Has-Many-Through associations, use
|
|
|
|
option `has_many: true`. To restore Belongs-To association, use
|
|
|
|
option `belongs_to: true`. For example:
|
2010-10-20 08:48:52 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2014-03-12 05:03:32 -04:00
|
|
|
class Location < ActiveRecord::Base
|
|
|
|
belongs_to :treasure
|
|
|
|
has_paper_trail
|
|
|
|
end
|
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
class Treasure < ActiveRecord::Base
|
|
|
|
has_one :location
|
2014-03-12 05:03:32 -04:00
|
|
|
has_paper_trail
|
2011-12-10 07:09:18 -05:00
|
|
|
end
|
2010-10-20 08:48:52 -04:00
|
|
|
|
2015-06-27 00:23:14 -04:00
|
|
|
treasure.amount # 100
|
|
|
|
treasure.location.latitude # 12.345
|
2010-10-20 08:48:52 -04:00
|
|
|
|
2017-05-29 12:23:05 -04:00
|
|
|
treasure.update_attributes amount: 153
|
|
|
|
treasure.location.update_attributes latitude: 54.321
|
2011-07-14 05:25:10 -04:00
|
|
|
|
2017-05-29 12:23:05 -04:00
|
|
|
t = treasure.versions.last.reify(has_one: true)
|
2015-06-27 00:23:14 -04:00
|
|
|
t.amount # 100
|
|
|
|
t.location.latitude # 12.345
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-10-20 09:26:44 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
If the parent and child are updated in one go, PaperTrail can use the
|
|
|
|
aforementioned `transaction_id` to reify the models as they were before the
|
|
|
|
transaction (instead of before the update to the model).
|
2010-10-21 09:04:28 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
treasure.amount # 100
|
|
|
|
treasure.location.latitude # 12.345
|
2014-12-16 23:47:18 -05:00
|
|
|
|
2015-06-27 00:23:14 -04:00
|
|
|
Treasure.transaction do
|
2017-05-29 12:23:05 -04:00
|
|
|
treasure.location.update_attributes latitude: 54.321
|
|
|
|
treasure.update_attributes amount: 153
|
2015-06-27 00:23:14 -04:00
|
|
|
end
|
2014-12-16 23:47:18 -05:00
|
|
|
|
2017-05-29 12:23:05 -04:00
|
|
|
t = treasure.versions.last.reify(has_one: true)
|
2015-06-27 00:23:14 -04:00
|
|
|
t.amount # 100
|
|
|
|
t.location.latitude # 12.345, instead of 54.321
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-10-21 09:04:28 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
By default, PaperTrail excludes an associated record from the reified parent
|
|
|
|
model if the associated record exists in the live model but did not exist as at
|
|
|
|
the time the version was created. This is usually what you want if you just want
|
|
|
|
to look at the reified version. But if you want to persist it, it would be
|
2017-05-29 12:23:05 -04:00
|
|
|
better to pass in option `mark_for_destruction: true` so that the associated
|
2015-06-27 00:13:36 -04:00
|
|
|
record is included and marked for destruction. Note that `mark_for_destruction`
|
|
|
|
only has [an effect on associations marked with `autosave: true`][32].
|
2010-10-21 09:04:28 -04:00
|
|
|
|
2014-12-16 23:47:18 -05:00
|
|
|
```ruby
|
|
|
|
class Widget < ActiveRecord::Base
|
|
|
|
has_paper_trail
|
|
|
|
has_one :wotsit, autosave: true
|
|
|
|
end
|
2010-10-20 08:48:52 -04:00
|
|
|
|
2014-12-16 23:47:18 -05:00
|
|
|
class Wotsit < ActiveRecord::Base
|
|
|
|
has_paper_trail
|
|
|
|
belongs_to :widget
|
|
|
|
end
|
|
|
|
|
2017-05-29 12:23:05 -04:00
|
|
|
widget = Widget.create(name: 'widget_0')
|
|
|
|
widget.update_attributes(name: 'widget_1')
|
|
|
|
widget.create_wotsit(name: 'wotsit')
|
2010-10-20 08:48:52 -04:00
|
|
|
|
2017-05-29 12:23:05 -04:00
|
|
|
widget_0 = widget.versions.last.reify(has_one: true)
|
2015-06-27 00:23:14 -04:00
|
|
|
widget_0.wotsit # nil
|
2014-12-16 23:47:18 -05:00
|
|
|
|
2017-05-29 12:23:05 -04:00
|
|
|
widget_0 = widget.versions.last.reify(has_one: true, mark_for_destruction: true)
|
2015-06-27 00:23:14 -04:00
|
|
|
widget_0.wotsit.marked_for_destruction? # true
|
|
|
|
widget_0.save!
|
|
|
|
widget.reload.wotsit # nil
|
2014-12-16 23:47:18 -05:00
|
|
|
```
|
|
|
|
|
2016-08-08 13:43:14 -04:00
|
|
|
#### 4.b.1. Known Issues
|
2014-12-16 23:47:18 -05:00
|
|
|
|
2016-08-08 13:43:14 -04:00
|
|
|
Associations are an **experimental feature** and have the following known
|
|
|
|
issues, in order of descending importance.
|
|
|
|
|
|
|
|
1. PaperTrail only reifies the first level of associations.
|
|
|
|
1. [#542](https://github.com/airblade/paper_trail/issues/542) -
|
|
|
|
Not compatible with [transactional tests][34], aka. transactional fixtures.
|
2015-10-31 21:17:05 -04:00
|
|
|
1. Requires database timestamp columns with fractional second precision.
|
|
|
|
- Sqlite and postgres timestamps have fractional second precision by default.
|
|
|
|
[MySQL timestamps do not][35]. Furthermore, MySQL 5.5 and earlier do not
|
|
|
|
support fractional second precision at all.
|
|
|
|
- Also, support for fractional seconds in MySQL was not added to
|
|
|
|
rails until ActiveRecord 4.2 (https://github.com/rails/rails/pull/14359).
|
2015-06-27 00:13:36 -04:00
|
|
|
1. PaperTrail can't restore an association properly if the association record
|
|
|
|
can be updated to replace its parent model (by replacing the foreign key)
|
2016-08-08 13:43:14 -04:00
|
|
|
1. Currently PaperTrail only supports a single `version_associations` table.
|
|
|
|
Therefore, you can only use a single table to store the versions for
|
2015-06-27 00:13:36 -04:00
|
|
|
all related models. Sorry for those who use multiple version tables.
|
2015-09-30 18:28:57 -04:00
|
|
|
1. PaperTrail relies on the callbacks on the association model (and the :through
|
2015-06-27 00:13:36 -04:00
|
|
|
association model for Has-Many-Through associations) to record the versions
|
|
|
|
and the relationship between the versions. If the association is changed
|
|
|
|
without invoking the callbacks, Reification won't work. Below are some
|
|
|
|
examples:
|
2010-07-05 08:01:02 -04:00
|
|
|
|
|
|
|
Given these models:
|
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
|
|
|
class Book < ActiveRecord::Base
|
2017-05-29 12:23:05 -04:00
|
|
|
has_many :authorships, dependent: :destroy
|
|
|
|
has_many :authors, through: :authorships, source: :person
|
2011-12-10 07:09:18 -05:00
|
|
|
has_paper_trail
|
|
|
|
end
|
|
|
|
|
|
|
|
class Authorship < ActiveRecord::Base
|
|
|
|
belongs_to :book
|
|
|
|
belongs_to :person
|
|
|
|
has_paper_trail # NOTE
|
|
|
|
end
|
|
|
|
|
|
|
|
class Person < ActiveRecord::Base
|
2017-05-29 12:23:05 -04:00
|
|
|
has_many :authorships, dependent: :destroy
|
|
|
|
has_many :books, through: :authorships
|
2011-12-10 07:09:18 -05:00
|
|
|
has_paper_trail
|
|
|
|
end
|
|
|
|
```
|
2010-07-05 08:01:02 -04:00
|
|
|
|
|
|
|
Then each of the following will store authorship versions:
|
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
@book.authors << @dostoyevsky
|
2017-05-29 12:23:05 -04:00
|
|
|
@book.authors.create name: 'Tolstoy'
|
2015-06-27 00:23:14 -04:00
|
|
|
@book.authorships.last.destroy
|
|
|
|
@book.authorships.clear
|
|
|
|
@book.author_ids = [@solzhenistyn.id, @dostoyevsky.id]
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-07-05 08:01:02 -04:00
|
|
|
|
|
|
|
But none of these will:
|
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
@book.authors.delete @tolstoy
|
|
|
|
@book.author_ids = []
|
|
|
|
@book.authors = []
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-07-05 08:01:02 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
Having said that, you can apparently get all these working (I haven't tested it
|
|
|
|
myself) with this patch:
|
2011-11-25 02:54:22 -05:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
|
|
|
# In config/initializers/active_record_patch.rb
|
|
|
|
module ActiveRecord
|
|
|
|
# = Active Record Has Many Through Association
|
|
|
|
module Associations
|
|
|
|
class HasManyThroughAssociation < HasManyAssociation #:nodoc:
|
|
|
|
alias_method :original_delete_records, :delete_records
|
|
|
|
|
|
|
|
def delete_records(records, method)
|
|
|
|
method ||= :destroy
|
|
|
|
original_delete_records(records, method)
|
2010-10-28 05:41:25 -04:00
|
|
|
end
|
|
|
|
end
|
2011-12-10 07:09:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
2010-10-28 05:41:25 -04:00
|
|
|
|
2015-06-26 23:57:57 -04:00
|
|
|
See [issue 113][16] for a discussion about this.
|
2010-09-03 05:06:41 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 4.c. Storing Metadata
|
2010-01-06 07:57:54 -05:00
|
|
|
|
2010-03-19 14:53:49 -04:00
|
|
|
You can store arbitrary model-level metadata alongside each version like this:
|
2010-01-06 07:57:54 -05:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
|
|
|
class Article < ActiveRecord::Base
|
|
|
|
belongs_to :author
|
2017-05-29 12:23:05 -04:00
|
|
|
has_paper_trail meta: { author_id: :author_id,
|
|
|
|
word_count: :count_words,
|
|
|
|
answer: 42 }
|
2011-12-10 07:09:18 -05:00
|
|
|
def count_words
|
|
|
|
153
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
2010-01-06 07:57:54 -05:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
PaperTrail will call your proc with the current article and store the result in
|
|
|
|
the `author_id` column of the `versions` table.
|
2015-10-01 14:30:23 -04:00
|
|
|
Don't forget to add any such columns to your `versions` table.
|
2011-09-29 05:00:13 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
#### Advantages of Metadata
|
2010-01-06 07:57:54 -05:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
Why would you do this? In this example, `author_id` is an attribute of
|
|
|
|
`Article` and PaperTrail will store it anyway in a serialized 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:
|
2010-01-06 07:57:54 -05:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2017-05-29 12:23:05 -04:00
|
|
|
PaperTrail::Version.where(author_id: author_id)
|
2011-12-10 07:09:18 -05:00
|
|
|
```
|
2010-01-06 07:57:54 -05:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
#### Metadata from Controllers
|
2011-02-08 08:07:31 -05:00
|
|
|
|
2015-06-28 21:34:34 -04:00
|
|
|
You can also store any information you like from your controller. Override
|
2015-06-27 00:13:36 -04:00
|
|
|
the `info_for_paper_trail` method in your controller to return a hash whose keys
|
2015-06-28 21:34:34 -04:00
|
|
|
correspond to columns in your `versions` table.
|
2010-03-19 14:53:49 -04:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
|
|
|
class ApplicationController
|
|
|
|
def info_for_paper_trail
|
2017-05-29 12:23:05 -04:00
|
|
|
{ ip: request.remote_ip, user_agent: request.user_agent }
|
2011-12-10 07:09:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
2010-03-19 14:53:49 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
## 5. ActiveRecord
|
|
|
|
|
|
|
|
### 5.a. Single Table Inheritance (STI)
|
2016-05-04 14:45:45 -04:00
|
|
|
|
2016-05-04 14:56:10 -04:00
|
|
|
PaperTrail supports [Single Table Inheritance][39], and even supports an
|
2016-12-04 16:03:23 -05:00
|
|
|
un-versioned base model, as of `23ffbdc7e1`.
|
2016-05-04 14:45:45 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Fruit < ActiveRecord::Base
|
2016-05-04 14:56:10 -04:00
|
|
|
# un-versioned base model
|
2016-05-04 14:45:45 -04:00
|
|
|
end
|
|
|
|
class Banana < Fruit
|
|
|
|
has_paper_trail
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2016-05-04 14:56:10 -04:00
|
|
|
However, there is a known issue when reifying [associations](#associations),
|
|
|
|
see https://github.com/airblade/paper_trail/issues/594
|
2016-05-04 14:45:45 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 5.b. Configuring the `versions` Association
|
2016-05-09 18:08:28 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
You may configure the name of the `versions` association by passing
|
|
|
|
a different name to `has_paper_trail`.
|
2016-05-09 18:08:28 -04:00
|
|
|
|
|
|
|
```ruby
|
2016-05-10 23:43:33 -04:00
|
|
|
class Post < ActiveRecord::Base
|
|
|
|
has_paper_trail class_name: 'Version', versions: :drafts
|
2016-05-09 18:08:28 -04:00
|
|
|
end
|
2016-05-10 23:43:33 -04:00
|
|
|
|
|
|
|
Post.new.versions # => NoMethodError
|
2016-05-09 18:08:28 -04:00
|
|
|
```
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
Overriding (instead of configuring) the `versions` method is not supported.
|
|
|
|
Overriding associations is not recommended in general.
|
|
|
|
|
2016-05-11 00:06:31 -04:00
|
|
|
### 5.c. Generators
|
|
|
|
|
|
|
|
PaperTrail has one generator, `paper_trail:install`. It writes, but does not
|
2016-10-08 14:37:52 -04:00
|
|
|
run, a migration file. It also creates a PaperTrail configuration initializer.
|
2016-08-25 14:31:03 -04:00
|
|
|
The migration adds (at least) the `versions` table. The
|
2016-05-11 00:06:31 -04:00
|
|
|
most up-to-date documentation for this generator can be found by running `rails
|
|
|
|
generate paper_trail:install --help`, but a copy is included here for
|
|
|
|
convenience.
|
|
|
|
|
|
|
|
```
|
|
|
|
Usage:
|
|
|
|
rails generate paper_trail:install [options]
|
|
|
|
|
|
|
|
Options:
|
|
|
|
[--with-changes], [--no-with-changes] # Store changeset (diff) with each version
|
|
|
|
[--with-associations], [--no-with-associations] # Store transactional IDs to support association restoration
|
|
|
|
|
|
|
|
Runtime options:
|
|
|
|
-f, [--force] # Overwrite files that already exist
|
|
|
|
-p, [--pretend], [--no-pretend] # Run but do not make any changes
|
|
|
|
-q, [--quiet], [--no-quiet] # Suppress status output
|
|
|
|
-s, [--skip], [--no-skip] # Skip files that already exist
|
|
|
|
|
2016-08-25 14:31:03 -04:00
|
|
|
Generates (but does not run) a migration to add a versions table. Also generates an initializer file for configuring PaperTrail
|
2016-05-11 00:06:31 -04:00
|
|
|
```
|
|
|
|
|
2016-12-04 16:03:23 -05:00
|
|
|
### 5.d. Protected Attributes
|
|
|
|
|
|
|
|
As of version 6, PT no longer supports rails 3 or the [protected_attributes][17]
|
|
|
|
gem. If you are still using them, you may use PT 5 or lower. We recommend
|
2017-10-17 17:59:46 -04:00
|
|
|
upgrading to [strong_parameters][18] as soon as possible.
|
2017-07-06 17:50:49 -04:00
|
|
|
|
2017-10-17 17:59:46 -04:00
|
|
|
If you must use [protected_attributes][17] for now, and want to use PT > 5, you
|
2017-07-06 17:50:49 -04:00
|
|
|
can reopen `PaperTrail::Version` and add the following `attr_accessible` fields:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# app/models/paper_trail/version.rb
|
|
|
|
module PaperTrail
|
|
|
|
class Version < ActiveRecord::Base
|
|
|
|
include PaperTrail::VersionConcern
|
|
|
|
attr_accessible :item_type, :item_id, :event, :whodunnit, :object, :object_changes, :created_at
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
This unsupported workaround has been tested with protected_attributes 1.0.9 /
|
|
|
|
rails 4.2.8 / paper_trail 7.0.3.
|
2016-12-04 16:03:23 -05:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
## 6. Extensibility
|
|
|
|
|
|
|
|
### 6.a. Custom Version Classes
|
2015-10-18 00:50:34 -04:00
|
|
|
|
|
|
|
You can specify custom version subclasses with the `:class_name` option:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
class PostVersion < PaperTrail::Version
|
|
|
|
# custom behaviour, e.g:
|
|
|
|
self.table_name = :post_versions
|
|
|
|
end
|
|
|
|
|
|
|
|
class Post < ActiveRecord::Base
|
2017-05-29 12:23:05 -04:00
|
|
|
has_paper_trail class_name: 'PostVersion'
|
2015-10-18 00:50:34 -04:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2016-02-23 17:59:14 -05:00
|
|
|
Unlike ActiveRecord's `class_name`, you'll have to supply the complete module
|
|
|
|
path to the class (e.g. `Foo::BarVersion` if your class is inside the module
|
|
|
|
`Foo`).
|
2015-10-18 00:50:34 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
#### Advantages
|
2015-10-18 00:50:34 -04:00
|
|
|
|
|
|
|
1. For models which have a lot of versions, storing each model's versions in a
|
|
|
|
separate table can improve the performance of certain database queries.
|
|
|
|
1. Store different version [metadata](#storing-metadata) for different models.
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
#### Configuration
|
2015-10-18 00:50:34 -04:00
|
|
|
|
|
|
|
If you are using Postgres, you should also define the sequence that your custom
|
|
|
|
version class will use:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
class PostVersion < PaperTrail::Version
|
|
|
|
self.table_name = :post_versions
|
|
|
|
self.sequence_name = :post_versions_id_seq
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
If you only use custom version classes and don't have a `versions` table, you
|
|
|
|
must let ActiveRecord know that the `PaperTrail::Version` class is an
|
|
|
|
`abstract_class`.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# app/models/paper_trail/version.rb
|
|
|
|
module PaperTrail
|
|
|
|
class Version < ActiveRecord::Base
|
|
|
|
include PaperTrail::VersionConcern
|
|
|
|
self.abstract_class = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Post < ActiveRecord::Base
|
2017-05-29 12:23:05 -04:00
|
|
|
has_paper_trail versions: :paper_trail_versions,
|
|
|
|
version: :paper_trail_version
|
2015-10-18 00:50:34 -04:00
|
|
|
|
|
|
|
# Existing versions method. We don't want to clash.
|
|
|
|
def versions
|
2017-04-06 11:17:08 -04:00
|
|
|
# ...
|
2015-10-18 00:50:34 -04:00
|
|
|
end
|
2017-04-06 11:17:08 -04:00
|
|
|
|
2015-10-18 00:50:34 -04:00
|
|
|
# Existing version method. We don't want to clash.
|
|
|
|
def version
|
2017-04-06 11:17:08 -04:00
|
|
|
# ...
|
2015-10-18 00:50:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 6.b. Custom Serializer
|
2012-07-03 23:22:52 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
By default, PaperTrail stores your changes as a `YAML` dump. You can override
|
|
|
|
this with the serializer config option:
|
2012-07-03 23:22:52 -04:00
|
|
|
|
|
|
|
```ruby
|
2015-06-27 00:23:14 -04:00
|
|
|
PaperTrail.serializer = MyCustomSerializer
|
2012-07-03 23:22:52 -04:00
|
|
|
```
|
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
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:
|
2013-01-22 18:13:58 -05:00
|
|
|
|
2015-06-26 23:57:57 -04:00
|
|
|
* [PaperTrail::Serializers::YAML][24] - Default
|
|
|
|
* [PaperTrail::Serializers::JSON][25]
|
2012-07-03 23:22:52 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
#### PostgreSQL JSON column type support
|
2015-01-29 17:52:56 -05:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
If you use PostgreSQL, and would like to store your `object` (and/or
|
2015-12-16 10:47:04 -05:00
|
|
|
`object_changes`) data in a column of [type `json` or type `jsonb`][26], specify
|
2015-06-27 00:13:36 -04:00
|
|
|
`json` instead of `text` for these columns in your migration:
|
2015-01-21 18:24:51 -05:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
create_table :versions do |t|
|
2017-04-06 11:17:08 -04:00
|
|
|
# ...
|
2015-01-21 18:24:51 -05:00
|
|
|
t.json :object # Full object changes
|
|
|
|
t.json :object_changes # Optional column-level changes
|
2017-04-06 11:17:08 -04:00
|
|
|
# ...
|
2015-01-21 18:24:51 -05:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2015-12-16 10:47:04 -05:00
|
|
|
If you use the PostgreSQL `json` or `jsonb` column type, you do not need
|
|
|
|
to specify a `PaperTrail.serializer`.
|
2015-08-31 02:44:29 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
##### Convert existing YAML data to JSON
|
2015-12-06 14:25:45 -05:00
|
|
|
|
|
|
|
If you've been using PaperTrail for a while with the default YAML serializer
|
|
|
|
and you want to switch to JSON or JSONB, you're in a bit of a bind because
|
|
|
|
there's no automatic way to migrate your data. The first (slow) option is to
|
|
|
|
loop over every record and parse it in Ruby, then write to a temporary column:
|
|
|
|
|
|
|
|
```ruby
|
2017-01-16 14:15:40 -05:00
|
|
|
add_column :versions, :new_object, :jsonb # or :json
|
2015-12-06 14:25:45 -05:00
|
|
|
|
|
|
|
PaperTrail::Version.reset_column_information
|
|
|
|
PaperTrail::Version.find_each do |version|
|
|
|
|
version.update_column :new_object, YAML.load(version.object)
|
|
|
|
end
|
|
|
|
|
|
|
|
remove_column :versions, :object
|
|
|
|
rename_column :versions, :new_object, :object
|
|
|
|
```
|
|
|
|
|
2015-12-11 15:23:38 -05:00
|
|
|
This technique can be very slow if you have a lot of data. Though slow, it is
|
|
|
|
safe in databases where transactions are protected against DDL, such as
|
|
|
|
Postgres. In databases without such protection, such as MySQL, a table lock may
|
|
|
|
be necessary.
|
2015-12-06 14:25:45 -05:00
|
|
|
|
2015-12-11 15:23:38 -05:00
|
|
|
If the above technique is too slow for your needs, and you're okay doing without
|
2016-01-07 18:22:09 -05:00
|
|
|
PaperTrail data temporarily, you can create the new column without converting
|
2015-12-11 15:23:38 -05:00
|
|
|
the data.
|
2015-12-06 14:25:45 -05:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
rename_column :versions, :object, :old_object
|
|
|
|
add_column :versions, :object, :jsonb # or :json
|
|
|
|
```
|
|
|
|
|
2015-12-11 15:23:38 -05:00
|
|
|
After that migration, your historical data still exists as YAML, and new data
|
|
|
|
will be stored as JSON. Next, convert records from YAML to JSON using a
|
|
|
|
background script.
|
2015-12-06 14:25:45 -05:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
PaperTrail::Version.where.not(old_object: nil).find_each do |version|
|
|
|
|
version.update_columns old_object: nil, object: YAML.load(version.old_object)
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2015-12-11 15:23:38 -05:00
|
|
|
Finally, in another migration, remove the old column.
|
2015-12-06 14:25:45 -05:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
remove_column :versions, :old_object
|
|
|
|
```
|
|
|
|
|
2015-12-11 15:23:38 -05:00
|
|
|
If you use the optional `object_changes` column, don't forget to convert it
|
|
|
|
also, using the same technique.
|
2015-12-06 14:25:45 -05:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
##### Convert a Column from Text to JSON
|
2015-12-16 10:47:04 -05:00
|
|
|
|
|
|
|
If your `object` column already contains JSON data, and you want to change its
|
|
|
|
data type to `json` or `jsonb`, you can use the following [DDL][36]. Of course,
|
|
|
|
if your `object` column contains YAML, you must first convert the data to JSON
|
|
|
|
(see above) before you can change the column type.
|
|
|
|
|
|
|
|
Using SQL:
|
|
|
|
|
|
|
|
```sql
|
|
|
|
alter table versions
|
|
|
|
alter column object type jsonb
|
|
|
|
using object::jsonb;
|
|
|
|
```
|
|
|
|
|
|
|
|
Using ActiveRecord:
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
class ConvertVersionsObjectToJson < ActiveRecord::Migration
|
|
|
|
def up
|
|
|
|
change_column :versions, :object, 'jsonb USING object::jsonb'
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
change_column :versions, :object, 'text USING object::text'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
## 7. Testing
|
2009-05-27 13:29:58 -04:00
|
|
|
|
2016-03-05 16:48:58 -05:00
|
|
|
You may want to turn PaperTrail off to speed up your tests. See [Turning
|
2017-09-11 12:57:34 -04:00
|
|
|
PaperTrail Off](#2d-turning-papertrail-off) above.
|
2013-08-27 15:12:33 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 7.a. Minitest
|
2016-03-05 16:48:58 -05:00
|
|
|
|
|
|
|
First, disable PT for the entire `ruby` process.
|
2009-05-27 13:29:58 -04:00
|
|
|
|
2016-03-05 16:48:58 -05:00
|
|
|
```ruby
|
|
|
|
# in config/environments/test.rb
|
|
|
|
config.after_initialize do
|
|
|
|
PaperTrail.enabled = false
|
|
|
|
end
|
|
|
|
```
|
2014-12-29 15:42:49 -05:00
|
|
|
|
2016-03-05 16:48:58 -05:00
|
|
|
Then, to enable PT for specific tests, you can add a `with_versioning` test
|
|
|
|
helper method.
|
2014-12-29 15:42:49 -05:00
|
|
|
|
|
|
|
```ruby
|
2016-03-05 16:48:58 -05:00
|
|
|
# in test/test_helper.rb
|
|
|
|
def with_versioning
|
|
|
|
was_enabled = PaperTrail.enabled?
|
|
|
|
was_enabled_for_controller = PaperTrail.enabled_for_controller?
|
|
|
|
PaperTrail.enabled = true
|
|
|
|
PaperTrail.enabled_for_controller = true
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
PaperTrail.enabled = was_enabled
|
|
|
|
PaperTrail.enabled_for_controller = was_enabled_for_controller
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
2014-12-29 15:42:49 -05:00
|
|
|
|
2016-03-05 16:48:58 -05:00
|
|
|
Then, use the helper in your tests.
|
|
|
|
|
|
|
|
```ruby
|
2017-05-29 12:23:05 -04:00
|
|
|
test 'something that needs versioning' do
|
2016-03-05 16:48:58 -05:00
|
|
|
with_versioning do
|
|
|
|
# your test
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 7.b. RSpec
|
2016-03-05 16:48:58 -05:00
|
|
|
|
|
|
|
PaperTrail provides a helper, `paper_trail/frameworks/rspec.rb`, that works with
|
|
|
|
[RSpec][27] to make it easier to control when `PaperTrail` is enabled during
|
|
|
|
testing.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# spec/rails_helper.rb
|
2014-12-29 15:42:49 -05:00
|
|
|
ENV["RAILS_ENV"] ||= 'test'
|
|
|
|
require 'spec_helper'
|
|
|
|
require File.expand_path("../../config/environment", __FILE__)
|
|
|
|
require 'rspec/rails'
|
2017-04-06 11:17:08 -04:00
|
|
|
# ...
|
2014-12-29 15:42:49 -05:00
|
|
|
require 'paper_trail/frameworks/rspec'
|
|
|
|
```
|
|
|
|
|
2016-03-05 16:48:58 -05:00
|
|
|
With the helper loaded, PaperTrail will be turned off for all tests by
|
|
|
|
default. To enable PaperTrail for a test you can either wrap the
|
2017-04-06 11:17:08 -04:00
|
|
|
test in a `with_versioning` block, or pass in `versioning: true` option to a
|
2016-03-05 16:48:58 -05:00
|
|
|
spec block.
|
2011-11-09 06:04:41 -05:00
|
|
|
|
2011-12-10 07:09:18 -05:00
|
|
|
```ruby
|
2017-05-29 12:23:05 -04:00
|
|
|
describe 'RSpec test group' do
|
2013-08-27 15:12:33 -04:00
|
|
|
it 'by default, PaperTrail will be turned off' do
|
2014-10-07 23:58:51 -04:00
|
|
|
expect(PaperTrail).to_not be_enabled
|
2013-08-27 15:12:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
with_versioning do
|
|
|
|
it 'within a `with_versioning` block it will be turned on' do
|
2014-10-07 23:58:51 -04:00
|
|
|
expect(PaperTrail).to be_enabled
|
2013-08-27 15:12:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-06 11:17:08 -04:00
|
|
|
it 'can be turned on at the `it` or `describe` level', versioning: true do
|
2014-10-07 23:58:51 -04:00
|
|
|
expect(PaperTrail).to be_enabled
|
2011-12-10 07:09:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
2011-11-09 06:04:41 -05:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
The helper will also reset the `PaperTrail.whodunnit` value to `nil` before each
|
|
|
|
test to help prevent data spillover between tests. 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.
|
2013-08-27 15:12:33 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
There is also a `be_versioned` matcher provided by PaperTrail's RSpec helper
|
|
|
|
which can be leveraged like so:
|
2013-08-27 15:12:33 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
class Widget < ActiveRecord::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
describe Widget do
|
2017-05-29 12:23:05 -04:00
|
|
|
it 'is not versioned by default' do
|
2014-10-09 18:06:17 -04:00
|
|
|
is_expected.to_not be_versioned
|
2014-10-07 23:58:51 -04:00
|
|
|
end
|
2013-08-27 15:12:33 -04:00
|
|
|
|
2017-05-29 12:23:05 -04:00
|
|
|
describe 'add versioning to the `Widget` class' do
|
2013-08-27 15:12:33 -04:00
|
|
|
before(:all) do
|
|
|
|
class Widget < ActiveRecord::Base
|
|
|
|
has_paper_trail
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-29 12:23:05 -04:00
|
|
|
it 'enables paper trail' do
|
2014-10-09 18:06:17 -04:00
|
|
|
is_expected.to be_versioned
|
2014-10-07 23:58:51 -04:00
|
|
|
end
|
2013-08-27 15:12:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
2009-05-27 13:29:58 -04:00
|
|
|
|
2017-04-06 11:17:08 -04:00
|
|
|
#### Matchers
|
|
|
|
|
|
|
|
The `have_a_version_with` matcher makes assertions about versions using
|
|
|
|
`where_object`, based on the `object` column.
|
2014-07-09 08:47:15 -04:00
|
|
|
|
2016-10-18 11:22:15 -04:00
|
|
|
```ruby
|
|
|
|
describe '`have_a_version_with` matcher' do
|
2017-05-29 12:23:05 -04:00
|
|
|
it 'is possible to do assertions on version attributes' do
|
2016-10-18 11:34:27 -04:00
|
|
|
widget.update_attributes!(name: 'Leonard', an_integer: 1)
|
|
|
|
widget.update_attributes!(name: 'Tom')
|
|
|
|
widget.update_attributes!(name: 'Bob')
|
|
|
|
expect(widget).to have_a_version_with name: 'Leonard', an_integer: 1
|
|
|
|
expect(widget).to have_a_version_with an_integer: 1
|
|
|
|
expect(widget).to have_a_version_with name: 'Tom'
|
2016-10-18 11:22:15 -04:00
|
|
|
end
|
|
|
|
end
|
2014-07-09 08:47:15 -04:00
|
|
|
```
|
2017-04-06 11:17:08 -04:00
|
|
|
|
|
|
|
The `have_a_version_with_changes` matcher makes assertions about versions using
|
|
|
|
`where_object_changes`, based on the optional
|
|
|
|
[`object_changes` column](#3c-diffing-versions).
|
2016-11-01 01:14:52 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
describe '`have_a_version_with_changes` matcher' do
|
2017-05-29 12:23:05 -04:00
|
|
|
it 'is possible to do assertions on version changes' do
|
2016-11-01 01:14:52 -04:00
|
|
|
widget.update_attributes!(name: 'Leonard', an_integer: 1)
|
|
|
|
widget.update_attributes!(name: 'Tom')
|
|
|
|
widget.update_attributes!(name: 'Bob')
|
2017-04-06 11:03:25 -04:00
|
|
|
expect(widget).to have_a_version_with_changes name: 'Leonard', an_integer: 2
|
|
|
|
expect(widget).to have_a_version_with_changes an_integer: 2
|
|
|
|
expect(widget).to have_a_version_with_changes name: 'Bob'
|
2016-11-01 01:14:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
|
|
|
For more examples of the RSpec matchers, see the
|
|
|
|
[Widget spec](https://github.com/airblade/paper_trail/blob/master/spec/models/widget_spec.rb)
|
2014-07-09 08:47:15 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 7.c. Cucumber
|
2013-08-27 16:16:06 -04:00
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
PaperTrail provides a helper for [Cucumber][28] that works similar to the RSpec
|
2017-04-06 11:17:08 -04:00
|
|
|
helper. If you want to use the helper, you will need to require in your cucumber
|
2015-06-27 00:13:36 -04:00
|
|
|
helper like so:
|
2014-12-29 15:42:49 -05:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
# features/support/env.rb
|
|
|
|
|
2017-05-29 12:23:05 -04:00
|
|
|
ENV["RAILS_ENV"] ||= 'cucumber'
|
2014-12-29 15:42:49 -05:00
|
|
|
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
|
2017-04-06 11:17:08 -04:00
|
|
|
# ...
|
2014-12-29 15:42:49 -05:00
|
|
|
require 'paper_trail/frameworks/cucumber'
|
|
|
|
```
|
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
When the helper is loaded, PaperTrail will be turned off for all scenarios by a
|
2017-04-06 11:17:08 -04:00
|
|
|
`before` hook added by the helper by default. When you want to enable PaperTrail
|
2015-06-27 00:13:36 -04:00
|
|
|
for a scenario, you can wrap code in a `with_versioning` block in a step, like
|
|
|
|
so:
|
2013-08-27 16:16:06 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
Given /I want versioning on my model/ do
|
|
|
|
with_versioning do
|
|
|
|
# PaperTrail will be turned on for all code inside of this block
|
|
|
|
end
|
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
The helper will also reset the `PaperTrail.whodunnit` value to `nil` before each
|
|
|
|
test to help prevent data spillover between tests. 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.
|
2013-08-27 16:16:06 -04:00
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 7.d. Spork
|
2013-10-02 13:48:20 -04:00
|
|
|
|
2017-04-06 11:17:08 -04:00
|
|
|
If you want to use the `RSpec` or `Cucumber` helpers with [Spork][29], you will
|
2015-06-27 00:13:36 -04:00
|
|
|
need to manually require the helper(s) in your `prefork` block on your test
|
|
|
|
helper, like so:
|
2013-10-02 13:48:20 -04:00
|
|
|
|
|
|
|
```ruby
|
2014-12-29 15:42:49 -05:00
|
|
|
# spec/rails_helper.rb
|
2013-10-02 13:48:20 -04:00
|
|
|
|
|
|
|
require 'spork'
|
|
|
|
|
|
|
|
Spork.prefork do
|
|
|
|
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
|
|
|
ENV["RAILS_ENV"] ||= 'test'
|
2014-12-29 15:42:49 -05:00
|
|
|
require 'spec_helper'
|
2013-10-02 13:48:20 -04:00
|
|
|
require File.expand_path("../../config/environment", __FILE__)
|
|
|
|
require 'rspec/rails'
|
|
|
|
require 'paper_trail/frameworks/rspec'
|
|
|
|
require 'paper_trail/frameworks/cucumber'
|
2017-04-06 11:17:08 -04:00
|
|
|
# ...
|
2013-10-02 13:48:20 -04:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2016-05-10 23:43:33 -04:00
|
|
|
### 7.e. Zeus or Spring
|
2013-10-02 13:48:20 -04:00
|
|
|
|
2017-04-06 11:17:08 -04:00
|
|
|
If you want to use the `RSpec` or `Cucumber` helpers with [Zeus][30] or
|
2015-06-27 00:13:36 -04:00
|
|
|
[Spring][31], you will need to manually require the helper(s) in your test
|
|
|
|
helper, like so:
|
2013-10-02 13:48:20 -04:00
|
|
|
|
|
|
|
```ruby
|
2014-12-29 15:42:49 -05:00
|
|
|
# spec/rails_helper.rb
|
2013-10-02 13:48:20 -04:00
|
|
|
|
|
|
|
ENV["RAILS_ENV"] ||= 'test'
|
2014-12-29 15:42:49 -05:00
|
|
|
require 'spec_helper'
|
2013-10-02 13:48:20 -04:00
|
|
|
require File.expand_path("../../config/environment", __FILE__)
|
|
|
|
require 'rspec/rails'
|
|
|
|
require 'paper_trail/frameworks/rspec'
|
|
|
|
```
|
|
|
|
|
2017-04-27 13:39:14 -04:00
|
|
|
## 8. Integration with Other Libraries
|
2015-10-18 00:59:45 -04:00
|
|
|
|
2017-04-27 13:39:14 -04:00
|
|
|
- [ActiveAdmin][42]
|
|
|
|
- Sinatra - [paper_trail-sinatra][41]
|
2017-10-17 17:59:46 -04:00
|
|
|
- [globalize][45] - [globalize-versioning][44]
|
2015-10-18 00:59:45 -04:00
|
|
|
|
2010-02-23 12:07:00 -05:00
|
|
|
## Articles
|
|
|
|
|
2015-06-27 00:13:36 -04:00
|
|
|
* [Jutsu #8 - Version your RoR models with PaperTrail](http://samurails.com/gems/papertrail/),
|
|
|
|
[Thibault](http://samurails.com/about-me/), 29th September 2014
|
|
|
|
* [Versioning with PaperTrail](http://www.sitepoint.com/versioning-papertrail),
|
|
|
|
[Ilya Bodrov](http://www.sitepoint.com/author/ibodrov), 10th April 2014
|
2016-06-30 07:42:30 -04:00
|
|
|
* [Using PaperTrail to track stack traces](http://web.archive.org/web/20141120233916/http://rubyrailsexpert.com/?p=36),
|
2015-06-27 00:13:36 -04:00
|
|
|
T James Corcoran's blog, 1st October 2013.
|
|
|
|
* [RailsCast #255 - Undo with PaperTrail](http://railscasts.com/episodes/255-undo-with-paper-trail),
|
|
|
|
28th February 2011.
|
|
|
|
* [Keep a Paper Trail with PaperTrail](http://www.linux-mag.com/id/7528),
|
|
|
|
Linux Magazine, 16th September 2009.
|
2010-02-23 12:07:00 -05:00
|
|
|
|
2009-06-23 05:39:16 -04:00
|
|
|
## Problems
|
|
|
|
|
|
|
|
Please use GitHub's [issue tracker](http://github.com/airblade/paper_trail/issues).
|
|
|
|
|
2009-11-23 11:52:42 -05:00
|
|
|
## Contributors
|
|
|
|
|
2017-04-27 13:46:11 -04:00
|
|
|
Created by Andy Stewart in 2010, maintained since 2012 by Ben Atkins, since 2015
|
|
|
|
by Jared Beck, with contributions by over 150 people.
|
|
|
|
|
|
|
|
https://github.com/airblade/paper_trail/graphs/contributors
|
2009-11-23 11:52:42 -05:00
|
|
|
|
2017-07-08 16:39:04 -04:00
|
|
|
## Contributing
|
|
|
|
|
|
|
|
See our [contribution guidelines][43]
|
|
|
|
|
2009-05-27 11:21:20 -04:00
|
|
|
## Inspirations
|
|
|
|
|
|
|
|
* [Simply Versioned](http://github.com/github/simply_versioned)
|
|
|
|
* [Acts As Audited](http://github.com/collectiveidea/acts_as_audited)
|
|
|
|
|
|
|
|
## Intellectual Property
|
|
|
|
|
2011-02-09 05:54:06 -05:00
|
|
|
Copyright (c) 2011 Andy Stewart (boss@airbladesoftware.com).
|
2009-05-27 11:21:20 -04:00
|
|
|
Released under the MIT licence.
|
2015-06-22 12:04:39 -04:00
|
|
|
|
|
|
|
[1]: http://api.rubyonrails.org/classes/ActiveRecord/Locking/Optimistic.html
|
|
|
|
[2]: https://github.com/airblade/paper_trail/issues/163
|
2016-04-06 01:46:54 -04:00
|
|
|
[3]: http://railscasts.com/episodes/255-undo-with-paper-trail
|
2015-10-14 15:51:03 -04:00
|
|
|
[4]: https://api.travis-ci.org/airblade/paper_trail.svg?branch=master
|
|
|
|
[5]: https://travis-ci.org/airblade/paper_trail
|
2015-06-26 23:57:57 -04:00
|
|
|
[6]: https://img.shields.io/gemnasium/airblade/paper_trail.svg
|
|
|
|
[7]: https://gemnasium.com/airblade/paper_trail
|
|
|
|
[9]: https://github.com/airblade/paper_trail/tree/3.0-stable
|
|
|
|
[10]: https://github.com/airblade/paper_trail/tree/2.7-stable
|
|
|
|
[11]: https://github.com/airblade/paper_trail/tree/rails2
|
|
|
|
[14]: https://raw.github.com/airblade/paper_trail/master/lib/generators/paper_trail/templates/create_versions.rb
|
|
|
|
[16]: https://github.com/airblade/paper_trail/issues/113
|
|
|
|
[17]: https://github.com/rails/protected_attributes
|
|
|
|
[18]: https://github.com/rails/strong_parameters
|
|
|
|
[19]: http://github.com/myobie/htmldiff
|
|
|
|
[20]: http://github.com/pvande/differ
|
|
|
|
[21]: https://github.com/halostatue/diff-lcs
|
|
|
|
[22]: http://github.com/jeremyw/paper_trail/blob/master/lib/paper_trail/has_paper_trail.rb#L151-156
|
|
|
|
[23]: http://github.com/tim/activerecord-diff
|
|
|
|
[24]: https://github.com/airblade/paper_trail/blob/master/lib/paper_trail/serializers/yaml.rb
|
|
|
|
[25]: https://github.com/airblade/paper_trail/blob/master/lib/paper_trail/serializers/json.rb
|
|
|
|
[26]: http://www.postgresql.org/docs/9.4/static/datatype-json.html
|
|
|
|
[27]: https://github.com/rspec/rspec
|
|
|
|
[28]: http://cukes.info
|
|
|
|
[29]: https://github.com/sporkrb/spork
|
|
|
|
[30]: https://github.com/burke/zeus
|
|
|
|
[31]: https://github.com/rails/spring
|
2015-06-27 00:13:36 -04:00
|
|
|
[32]: http://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html#method-i-mark_for_destruction
|
2015-07-12 22:27:58 -04:00
|
|
|
[33]: https://github.com/airblade/paper_trail/wiki/Setting-whodunnit-in-the-rails-console
|
2015-09-30 18:28:57 -04:00
|
|
|
[34]: https://github.com/rails/rails/blob/591a0bb87fff7583e01156696fbbf929d48d3e54/activerecord/lib/active_record/fixtures.rb#L142
|
2015-10-31 21:17:05 -04:00
|
|
|
[35]: https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html
|
2015-12-16 10:47:04 -05:00
|
|
|
[36]: http://www.postgresql.org/docs/9.4/interactive/ddl.html
|
2016-02-07 06:39:46 -05:00
|
|
|
[37]: https://github.com/ankit1910/paper_trail-globalid
|
2016-02-21 21:31:59 -05:00
|
|
|
[38]: https://github.com/sferik/rails_admin
|
2016-05-04 14:45:45 -04:00
|
|
|
[39]: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#class-ActiveRecord::Base-label-Single+table+inheritance
|
|
|
|
[40]: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#module-ActiveRecord::Associations::ClassMethods-label-Polymorphic+Associations
|
2017-03-28 12:45:18 -04:00
|
|
|
[41]: https://github.com/jaredbeck/paper_trail-sinatra
|
2017-04-27 13:39:14 -04:00
|
|
|
[42]: https://github.com/activeadmin/activeadmin/wiki/Auditing-via-paper_trail-%28change-history%29
|
2017-07-08 16:39:04 -04:00
|
|
|
[43]: https://github.com/airblade/paper_trail/blob/master/.github/CONTRIBUTING.md
|
2017-10-17 17:59:46 -04:00
|
|
|
[44]: https://github.com/globalize/globalize-versioning
|
|
|
|
[45]: https://github.com/globalize/globalize
|