No commit activity in last 3 years
No release in over 3 years
Allows storing only incremental changes in the object_changes column
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.77

Runtime

~> 1.0
 Project Readme

PaperTrail HashDiff Adapter

Adapter for the popular versioning/auditing tool PaperTrail This gem allows you to store only incremental changes in the object_changes column. This is especially useful when you are storing hashes in a json/b column and would like to track changes for each key. Uses HashDiff

Usage

Add the gem 'paper_trail-hashdiff' to your Gemfile.

In your paper_trail.rb initializer, add the following:

require 'paper_trail_hashdiff'
PaperTrail.config.object_changes_adapter = PaperTrailHashDiff.new

Additionally, if you want to limit storing these changes for only object columns (eg. json hashes or arrays) and skip primitives, you can use the "only_objects" option when initializing:

PaperTrail.config.object_changes_adapter = PaperTrailHashDiff.new(only_objects: true)

Note: With this option, the where_object_changes method from PT may not work properly.

Storage format

say you have a jsonb column called 'custom_values' as follows:

{
  name: 'abc'
  age: 5,
  tags: ['a', 'b']
}

and it is changed to

{
  name: 'def'
  age: 5,
  tags: ['a', 'c']
}

Previously the object_changes column would have been stored as follows:

{
  custom_values: [
    {
      name: 'abc',
      age: 5,
      tags: ['a', 'b']
    },
    {
      name: 'def',
      age: 5,
      tags: ['a', 'c']
    }
  ]
}

With this change, it will be stored as follows:

{
  custom_values: [
    ["~", [:name], "abc", "def"], ["-", [:tags, 1], "b"], ["+", [:tags, 1], "c"]
  ]
}

As you can see, the keys that were not changed (i.e. in this case, age) will not appear in the object changes at all, thus increasing storage efficiency.