Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to use a custom serializer for storing the object in the database #164

Merged
merged 2 commits into from
Dec 28, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,16 @@ Or a block:
end
```

## Using a custom serializer

By default, PaperTrail stores your changes as a YAML dump. You can override this with the serializer config option:

```ruby
>> PaperTrail.serializer = MyCustomSerializer
```

The serializer needs to be a class that responds to a `load` and `dump` method.

## Deleting Old Versions

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:
Expand Down
4 changes: 4 additions & 0 deletions lib/paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'paper_trail/controller'
require 'paper_trail/has_paper_trail'
require 'paper_trail/version'
require 'paper_trail/serializers/yaml'

# PaperTrail's module methods can be called in both models and controllers.
module PaperTrail
Expand Down Expand Up @@ -69,6 +70,9 @@ def self.controller_info=(value)
paper_trail_store[:controller_info] = value
end

def self.serializer
PaperTrail.config.serializer
end

private

Expand Down
5 changes: 3 additions & 2 deletions lib/paper_trail/config.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
module PaperTrail
class Config
include Singleton
attr_accessor :enabled, :timestamp_field
attr_accessor :enabled, :timestamp_field, :serializer

def initialize
# Indicates whether PaperTrail is on or off.
@enabled = true
@timestamp_field = :created_at
@serializer = PaperTrail::Serializers::Yaml
end
end
end
7 changes: 4 additions & 3 deletions lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ def record_update
}
if version_class.column_names.include? 'object_changes'
# The double negative (reject, !include?) preserves the hash structure of self.changes.
data[:object_changes] = self.changes.reject do |key, value|
changes = self.changes.reject do |key, value|
!notably_changed.include?(key)
end.to_yaml
end
data[:object_changes] = PaperTrail.serializer.dump(changes)
end
send(self.class.versions_association_name).build merge_metadata(data)
end
Expand Down Expand Up @@ -215,7 +216,7 @@ def item_before_change
end

def object_to_string(object)
object.attributes.except(*self.class.skip).to_yaml
PaperTrail.serializer.dump object.attributes.except(*self.class.skip)
end

def changed_notably?
Expand Down
23 changes: 23 additions & 0 deletions lib/paper_trail/serializers/yaml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'yaml'

module PaperTrail
module Serializers
class Yaml
def self.load(string)
new.load(string)
end

def self.dump(hash)
new.dump(hash)
end

def load(string)
YAML.load(string)
end

def dump(hash)
YAML.dump(hash)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/paper_trail/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def reify(options = {})
options.reverse_merge! :has_one => false

unless object.nil?
attrs = YAML::load object
attrs = PaperTrail.serializer.load object

# Normally a polymorphic belongs_to relationship allows us
# to get the object we belong to by calling, in this case,
Expand Down Expand Up @@ -103,7 +103,7 @@ def reify(options = {})
def changeset
if self.class.column_names.include? 'object_changes'
if changes = object_changes
HashWithIndifferentAccess[YAML::load(changes)]
HashWithIndifferentAccess[PaperTrail.serializer.load(changes)]
else
{}
end
Expand Down
6 changes: 3 additions & 3 deletions test/unit/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
end

should 'have removed the skipped attributes when saving the previous version' do
assert_equal nil, YAML::load(@old_article.object)['file_upload']
assert_equal nil, PaperTrail.serializer.load(@old_article.object)['file_upload']
end

should 'have kept the non-skipped attributes in the previous version' do
assert_equal 'Some text here.', YAML::load(@old_article.object)['content']
assert_equal 'Some text here.', PaperTrail.serializer.load(@old_article.object)['content']
end
end
end
Expand Down Expand Up @@ -184,7 +184,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
end

should 'have stored changes' do
assert_equal ({'name' => ['Henry', 'Harry']}), YAML::load(@widget.versions.last.object_changes)
assert_equal ({'name' => ['Henry', 'Harry']}), PaperTrail.serializer.load(@widget.versions.last.object_changes)
assert_equal ({'name' => ['Henry', 'Harry']}), @widget.versions.last.changeset
end

Expand Down
71 changes: 71 additions & 0 deletions test/unit/serializer_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'test_helper'

class CustomSerializer
require 'json'
def self.dump(object_hash)
JSON.dump object_hash
end

def self.load(string)
JSON.parse string
end
end

class SerializerTest < ActiveSupport::TestCase

context 'YAML Serializer' do
setup do
Fluxor.instance_eval <<-END
has_paper_trail
END

@fluxor = Fluxor.create :name => 'Some text.'
@fluxor.update_attributes :name => 'Some more text.'
end

should 'work with the default yaml serializer' do
# Normal behaviour
assert_equal 2, @fluxor.versions.length
assert_nil @fluxor.versions[0].reify
assert_equal 'Some text.', @fluxor.versions[1].reify.name


# Check values are stored as YAML.
hash = {"widget_id" => nil,"name" =>"Some text.","id" =>1}
assert_equal YAML.dump(hash), @fluxor.versions[1].object
assert_equal hash, YAML.load(@fluxor.versions[1].object)

end
end

context 'Custom Serializer' do
setup do
PaperTrail.config.serializer = CustomSerializer

Fluxor.instance_eval <<-END
has_paper_trail
END

@fluxor = Fluxor.create :name => 'Some text.'
@fluxor.update_attributes :name => 'Some more text.'
end

teardown do
PaperTrail.config.serializer = PaperTrail::Serializers::Yaml
end

should 'work with custom serializer' do
# Normal behaviour
assert_equal 2, @fluxor.versions.length
assert_nil @fluxor.versions[0].reify
assert_equal 'Some text.', @fluxor.versions[1].reify.name

# Check values are stored as JSON.
hash = {"widget_id" => nil,"name" =>"Some text.","id" =>1}
assert_equal JSON.dump(hash), @fluxor.versions[1].object
assert_equal hash, JSON.parse(@fluxor.versions[1].object)

end
end

end