Skip to content

Commit

Permalink
Adjusting the methods that dealt with the encoding/decoding data for …
Browse files Browse the repository at this point in the history
…attributes in ActiveRecord's :serialized_attributes hash so that it will fall back on YAML as the default serializer. Close #197
  • Loading branch information
Ben Atkins committed Feb 4, 2013
1 parent 24339a1 commit 9f9032a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 2.7.1 (Unreleased)

- [#197](https://github.com/airblade/paper_trail/issues/197) - PaperTrail now falls back on using YAML for serialization of
serialized model attributes for storage in the `object` and `object_changes` columns in the `Version` table. This fixes
compatibility for `Rails 3.0.x` for projects that employ the `serialize` declaration on a model.
- [#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
Expand All @@ -14,7 +17,7 @@
- [#183](https://github.com/airblade/paper_trail/pull/183) - Fully qualify the `Version` class to help prevent
namespace resolution errors within other gems / plugins.
- [#180](https://github.com/airblade/paper_trail/pull/180) - Store serialized representation of serialized attributes
on the `object` and `object_changes` column in the `Version` table.
on the `object` and `object_changes` columns in the `Version` table.
- [#164](https://github.com/airblade/paper_trail/pull/164) - Allow usage of custom serializer for storage of object attributes.

## 2.6.4
Expand Down
12 changes: 10 additions & 2 deletions lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,27 @@ def paper_trail_on
# Used for Version#object attribute
def serialize_attributes_for_paper_trail(attributes)
serialized_attributes.each do |key, coder|
attributes[key] = coder.dump(attributes[key]) if attributes.key?(key)
if attributes.key?(key)
coder = PaperTrail::Serializers::Yaml unless coder.respond_to?(:dump) # Rails 3.0.x's default serializers don't have a `dump` method
attributes[key] = coder.dump(attributes[key])
end
end
end

def unserialize_attributes_for_paper_trail(attributes)
serialized_attributes.each do |key, coder|
attributes[key] = coder.load(attributes[key]) if attributes.key?(key)
if attributes.key?(key)
coder = PaperTrail::Serializers::Yaml unless coder.respond_to?(:dump)
attributes[key] = coder.load(attributes[key])
end
end
end

# Used for Version#object_changes attribute
def serialize_attribute_changes(changes)
serialized_attributes.each do |key, coder|
if changes.key?(key)
coder = PaperTrail::Serializers::Yaml unless coder.respond_to?(:dump) # Rails 3.0.x's default serializers don't have a `dump` method
old_value, new_value = changes[key]
changes[key] = [coder.dump(old_value),
coder.dump(new_value)]
Expand All @@ -108,6 +115,7 @@ def serialize_attribute_changes(changes)
def unserialize_attribute_changes(changes)
serialized_attributes.each do |key, coder|
if changes.key?(key)
coder = PaperTrail::Serializers::Yaml unless coder.respond_to?(:dump)
old_value, new_value = changes[key]
changes[key] = [coder.load(old_value),
coder.load(new_value)]
Expand Down

0 comments on commit 9f9032a

Please sign in to comment.