2012-07-04 03:22:52 +00:00
|
|
|
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.
|
2013-01-21 15:38:43 +00:00
|
|
|
hash = {"widget_id" => nil, "name" => "Some text.", "id" => 1}
|
2012-10-13 11:34:57 +00:00
|
|
|
assert_equal YAML.dump(hash), @fluxor.versions[1].object
|
|
|
|
assert_equal hash, YAML.load(@fluxor.versions[1].object)
|
2012-07-04 03:22:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'Custom Serializer' do
|
|
|
|
setup do
|
2013-01-07 19:44:12 +00:00
|
|
|
PaperTrail.configure do |config|
|
|
|
|
config.serializer = CustomSerializer
|
|
|
|
end
|
2012-07-04 03:22:52 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-01-10 10:48:06 +00:00
|
|
|
should 'reify with custom serializer' do
|
2012-07-04 03:22:52 +00:00
|
|
|
# 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.
|
2012-10-13 11:34:57 +00:00
|
|
|
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)
|
2013-01-10 10:48:06 +00:00
|
|
|
end
|
2012-07-04 03:22:52 +00:00
|
|
|
|
2013-01-10 10:48:06 +00:00
|
|
|
should 'store object_changes' do
|
|
|
|
initial_changeset = {"name" => [nil, "Some text."], "id" => [nil, 1]}
|
|
|
|
second_changeset = {"name"=>["Some text.", "Some more text."]}
|
|
|
|
assert_equal initial_changeset, @fluxor.versions[0].changeset
|
|
|
|
assert_equal second_changeset, @fluxor.versions[1].changeset
|
2012-07-04 03:22:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|