PaperTrail 2.2.7-2.4.0 Changes

1. Changeset

From PaperTrail 2.2.7 release you can find a new method of Version instances, called changeset. PaperTrail doesn’t have diffs mechanism inside, but now if you have object_changes column in versions table (it can be generated automatically if you install PaperTrail with –with-changes option) it will store Rails’ changes of dirty objects.

1
2
$ rails g paper_trail:install --with-changes
# or manually add `object_changes` column in your `versions` table
1
2
3
4
>> widget = Widget.create :name => 'Bob'
>> widget.versions.last.changeset                # {}
>> widget.update_attributes :name => 'Robert'
>> widget.versions.last.changeset                # {'name' => ['Bob', 'Robert']}

More information about Diffing Versions you can find in PaperTrail README on GitHub.

2. Flexibility in naming of methods

There are some situations when methods version and versions are already busy by other associations or smth. In this case we can change the names of these methods in our application (but sometimes it’s time-consuming) or we can configure these methods in PaperTrail like that:

1
2
has_paper_trail :versions => :paper_trail_versions,
                :version_name => :paper_trail_version

3. Add :on option

With this option we can configure what events we need to track. For example, we don’t need to track create events:

1
has_paper_trail :on => [:update, :destroy]

4. Without Versioning

In some cases some action/actions must be executed without versioning. Now PaperTrail has simple wrapper for this case:

1
2
3
4
5
6
7
8
# Executes the given method or block without creating a new version.
def without_versioning(method = nil)
  paper_trail_was_enabled = self.paper_trail_enabled_for_model
  self.class.paper_trail_off
  method ? method.to_proc.call(self) : yield
ensure
  self.class.paper_trail_on if paper_trail_was_enabled
end

Usage:

1
2
3
4
5
6
7
# With method name
@widget.without_versioning :destroy

# or with block
@widget.without_versioning do
  @widget.update_attributes :name => 'Ford'
end

5. Attr Accessible

Now we need to use attr_accessible if we want to store some meta info in versions table. Example of meta information from PaperTrail README:

1
2
3
4
5
6
7
8
9
class Article < ActiveRecord::Base
  belongs_to :author
  has_paper_trail :meta => { :author_id  => Proc.new { |article| article.author_id },
                             :word_count => :count_words,
                             :answer     => 42 }
  def count_words
    153
  end
end

In this case author_id, word_count and answer are meta, and we need to have these columns in versions table. And also we need to add these attrs to attr_accessible

Peace!

Comments