Adding JSON serializer to the gem. Close #194

This commit is contained in:
Ben Atkins 2013-01-22 18:13:58 -05:00
parent d41d2dde51
commit 526e6ae7cc
9 changed files with 113 additions and 17 deletions

View File

@ -1,9 +1,10 @@
## 2.7.1 (Unreleased)
- [#194](https://github.com/airblade/paper_trail/issues/194) - A JSON serializer is now included in the gem.
- [#192](https://github.com/airblade/paper_trail/pull/192) - `object_changes` should store serialized representation of serialized
attributes for `create` actions (in addition to `update` actions, which had already been patched by
[#180](https://github.com/airblade/paper_trail/pull/180)).
- [#190](https://github.com/airblade/paper_trail/pull/190) - Fix compatibility with
- [#190](https://github.com/airblade/paper_trail/pull/190) - Fixed compatibility with
[SerializedAttributes](https://github.com/technoweenie/serialized_attributes) gem.
- [#189](https://github.com/airblade/paper_trail/pull/189) - Provided support for a `configure` block initializer.
- Added `setter` method for the `serializer` config option.

View File

@ -749,7 +749,10 @@ By default, PaperTrail stores your changes as a YAML dump. You can override this
>> PaperTrail.config.serializer = MyCustomSerializer
```
The serializer needs to be a class that responds to a `load` and `dump` method. [See the default PaperTrail serializer](https://github.com/airblade/paper_trail/blob/master/lib/paper_trail/serializers/yaml.rb) for an example.
The serializer needs to be a class that has a `load` and `dump` class method. These serializers are included in the gem for your convenience:
* [Yaml](https://github.com/airblade/paper_trail/blob/master/lib/paper_trail/serializers/yaml.rb) - Default
* [Json](https://github.com/airblade/paper_trail/blob/master/lib/paper_trail/serializers/json.rb)
## Deleting Old Versions

View File

@ -2,7 +2,9 @@ require 'paper_trail/config'
require 'paper_trail/controller'
require 'paper_trail/has_paper_trail'
require 'paper_trail/version'
require 'paper_trail/serializers/yaml'
require 'paper_trail/serializers/json'
# PaperTrail's module methods can be called in both models and controllers.
module PaperTrail

View File

@ -0,0 +1,15 @@
require 'active_support/json'
module PaperTrail
module Serializers
class Json
def self.load(string)
ActiveSupport::JSON.decode string
end
def self.dump(hash)
ActiveSupport::JSON.encode hash
end
end
end
end

View File

@ -19,7 +19,8 @@ Gem::Specification.new do |s|
s.add_dependency 'activerecord', '~> 3.0'
s.add_development_dependency 'rake'
s.add_development_dependency 'shoulda', '~> 3.3'
s.add_development_dependency 'sqlite3', '~> 1.2'
s.add_development_dependency 'capybara', '~> 2.0'
s.add_development_dependency 'shoulda', '~> 3.3'
s.add_development_dependency 'sqlite3', '~> 1.2'
s.add_development_dependency 'capybara', '~> 2.0'
s.add_development_dependency 'ffaker', '>= 1.15'
end

View File

@ -11,6 +11,7 @@ require "rails/test_help"
Rails.backtrace_cleaner.remove_silencers!
require 'shoulda'
require 'ffaker'
# Configure capybara for integration testing
require "capybara/rails"

View File

@ -1,16 +1,5 @@
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
@ -40,7 +29,7 @@ class SerializerTest < ActiveSupport::TestCase
context 'Custom Serializer' do
setup do
PaperTrail.configure do |config|
config.serializer = CustomSerializer
config.serializer = PaperTrail::Serializers::Json
end
Fluxor.instance_eval <<-END

View File

@ -0,0 +1,42 @@
require 'test_helper'
class JsonTest < ActiveSupport::TestCase
setup do
# Setup a hash with random values
@hash = {}
(1..4).each do |i|
@hash["key#{i}"] = Faker::Lorem.word
end
@hash_as_json = @hash.to_json
# Setup an array of random words
@array = []
(4..8).to_a.sample.times do
@array << Faker::Lorem.word
end
@array_as_json = @array.to_json
end
context '`load` class method' do
should 'exist' do
assert PaperTrail::Serializers::Json.respond_to?(:load)
end
should '`deserialize` JSON to Ruby' do
assert_equal @hash, PaperTrail::Serializers::Json.load(@hash_as_json)
assert_equal @array, PaperTrail::Serializers::Json.load(@array_as_json)
end
end
context '`dump` class method' do
should 'exist' do
assert PaperTrail::Serializers::Json.respond_to?(:dump)
end
should '`deserialize` JSON to Ruby' do
assert_equal @hash_as_json, PaperTrail::Serializers::Json.dump(@hash)
assert_equal @array_as_json, PaperTrail::Serializers::Json.dump(@array)
end
end
end

View File

@ -0,0 +1,42 @@
require 'test_helper'
class YamlTest < ActiveSupport::TestCase
setup do
# Setup a hash with random values
@hash = {}
(1..4).each do |i|
@hash["key#{i}".to_sym] = Faker::Lorem.word
end
@hash_as_yaml = @hash.to_yaml
# Setup an array of random words
@array = []
(4..8).to_a.sample.times do
@array << Faker::Lorem.word
end
@array_as_yaml = @array.to_yaml
end
context '`load` class method' do
should 'exist' do
assert PaperTrail::Serializers::Yaml.respond_to?(:load)
end
should '`deserialize` YAML to Ruby' do
assert_equal @hash, PaperTrail::Serializers::Yaml.load(@hash_as_yaml)
assert_equal @array, PaperTrail::Serializers::Yaml.load(@array_as_yaml)
end
end
context '`dump` class method' do
should 'exist' do
assert PaperTrail::Serializers::Yaml.respond_to?(:dump)
end
should '`deserialize` YAML to Ruby' do
assert_equal @hash_as_yaml, PaperTrail::Serializers::Yaml.dump(@hash)
assert_equal @array_as_yaml, PaperTrail::Serializers::Yaml.dump(@array)
end
end
end