Skip to content

Commit

Permalink
Suggestions for PR 1189, to be squashed
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredbeck committed Mar 13, 2019
1 parent 8d3963e commit 309a8c1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/paper_trail/events/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,14 @@ def prepare_object_changes(changes)
# serialization here, using `PaperTrail.serializer`.
#
# @api private
# @param changes HashWithIndifferentAccess
def recordable_object_changes(changes)
if PaperTrail.config.object_changes_adapter&.respond_to?(:diff)
changes = PaperTrail.config.object_changes_adapter.diff(changes)
# We'd like to avoid the `to_hash` here, because it increases memory
# usage, but that would be a breaking change because
# `object_changes_adapter` expects a plain `Hash`, not a
# `HashWithIndifferentAccess`.
changes = PaperTrail.config.object_changes_adapter.diff(changes.to_hash)
end

if @record.class.paper_trail.version_class.object_changes_col_is_json?
Expand Down Expand Up @@ -285,6 +290,9 @@ def serialize_object_changes(changes)
AttributeSerializers::ObjectChangesAttribute.
new(@record.class).
serialize(changes)

# We'd like to convert this `HashWithIndifferentAccess` to a plain
# `Hash`, but we don't, to save memory.
changes
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/paper_trail/serializers/yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def load(string)
::YAML.load string
end

# @param object (Hash | HashWithIndifferentAccess) - Coming from
# `recordable_object` `object` will be a plain `Hash`. However, due to
# recent [memory optimizations](https://git.io/fjeYv), when coming from
# `recordable_object_changes`, it will be a `HashWithIndifferentAccess`.
def dump(object)
object = object.to_hash if object.is_a?(HashWithIndifferentAccess)
::YAML.dump object
Expand Down

0 comments on commit 309a8c1

Please sign in to comment.