Skip to content

Commit

Permalink
Namespacing version class under the PaperTrail module. Fix #165
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Atkins committed May 29, 2013
1 parent 9703df7 commit 7bc537a
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 250 deletions.
30 changes: 16 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Widget.paper_trail_off
Widget.paper_trail_on
```

And a `Version` instance has these methods:
And a `PaperTrail::Version` instance has these methods:

```ruby
# Returns the item restored from this version.
Expand Down Expand Up @@ -124,7 +124,7 @@ This gives you a `versions` method which returns the paper trail of changes to y

```ruby
>> widget = Widget.find 42
>> widget.versions # [<Version>, <Version>, ...]
>> widget.versions # [<PaperTrail::Version>, <PaperTrail::Version>, ...]
```

Once you have a version, you can find out what happened:
Expand Down Expand Up @@ -193,7 +193,7 @@ class Article < ActiveRecord::Base
end
```

You may also have the `Version` model save a custom string in it's `event` field instead of the typical `create`, `update`, `destroy`.
You may also have the `PaperTrail::Version` model save a custom string in it's `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.

Expand Down Expand Up @@ -233,7 +233,7 @@ class Article < ActiveRecord::Base
end
```

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 `Version` to be created. For example:
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:

```ruby
>> a = Article.create
Expand Down Expand Up @@ -267,7 +267,7 @@ This means that only changes to the `title` will save a version of the article:

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`.

You can skip fields altogether with the `:skip` option. As with `:ignore`, updates to these fields will not create a new `Version`. In addition, these fields will not be included in the serialised version of the object whenever a new `Version` is created.
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.

For example:

Expand Down Expand Up @@ -304,7 +304,7 @@ Undeleting is just as simple:
>> widget = Widget.find 42
>> widget.destroy
# Time passes....
>> widget = Version.find(153).reify # the widget as it was before it was destroyed
>> widget = PaperTrail::Version.find(153).reify # the widget as it was before it was destroyed
>> widget.save # the widget lives!
```

Expand Down Expand Up @@ -388,7 +388,7 @@ In a migration or in `rails console` you can set who is responsible like this:
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:

```ruby
class Version < ActiveRecord::Base
class PaperTrail::Version < ActiveRecord::Base
if defined?(Rails::Console)
PaperTrail.whodunnit = "#{`whoami`.strip}: console"
elsif File.basename($0) == "rake"
Expand Down Expand Up @@ -423,7 +423,7 @@ To find out who made a `version`'s object look that way, use `version.originator
You can specify custom version subclasses with the `:class_name` option:

```ruby
class PostVersion < Version
class PostVersion < PaperTrail::Version
# custom behaviour, e.g:
self.table_name = :post_versions
end
Expand All @@ -438,7 +438,7 @@ This allows you to store each model's versions in a separate table, which is use
If you are using Postgres, you should also define the sequence that your custom version class will use:

```ruby
class PostVersion < Version
class PostVersion < PaperTrail::Version
self.table_name = :post_versions
self.sequence_name = :post_version_id_seq
end
Expand All @@ -451,7 +451,7 @@ If you only use custom version classes and don't use PaperTrail's built-in one,
- either declare PaperTrail's version class abstract like this (in `config/initializers/paper_trail_patch.rb`):

```ruby
Version.module_eval do
PaperTrail::Version.module_eval do
self.abstract_class = true
end
```
Expand Down Expand Up @@ -609,15 +609,17 @@ For example:

```ruby
# config/initializers/paper_trail.rb
class Version < ActiveRecord::Base
attr_accessible :author_id, :word_count, :answer
module PaperTrail
class Version < ActiveRecord::Base
attr_accessible :author_id, :word_count, :answer
end
end
```

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:

```ruby
Version.all(:conditions => ['author_id = ?', author_id])
PaperTrail::Version.all(:conditions => ['author_id = ?', author_id])
```

Note you can pass a symbol as a value in the `meta` hash to signal a method to call.
Expand Down Expand Up @@ -793,7 +795,7 @@ sql> delete from versions where created_at < 2010-06-01;
```

```ruby
>> Version.delete_all ["created_at < ?", 1.week.ago]
>> PaperTrail::Version.delete_all ["created_at < ?", 1.week.ago]
```

## Installation
Expand Down
2 changes: 1 addition & 1 deletion lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def has_paper_trail(options = {})
attr_accessor self.version_association_name

class_attribute :version_class_name
self.version_class_name = options[:class_name] || '::Version'
self.version_class_name = options[:class_name] || 'PaperTrail::Version'

class_attribute :paper_trail_options
self.paper_trail_options = options.dup
Expand Down
Loading

0 comments on commit 7bc537a

Please sign in to comment.