Skip to content

Commit

Permalink
Fix serializing of time/date columns using yaml serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Jan 27, 2024
1 parent 47dbc22 commit ed4f74e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

### Fixed

- None
- [#1458](https://github.com/paper-trail-gem/paper_trail/pull/1416) - Fix serializing
of time/date columns using yaml serializer. Previously, these were serialized as ruby
objects which uses a lot of space. Now they are serialized into strings.

## 15.1.0 (2023-10-22)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ def deserialize(attr, val)
end

def serialize(attr, val)
AttributeSerializerFactory.for(@klass, attr).serialize(val)
serialized = AttributeSerializerFactory.for(@klass, attr).serialize(val)
@klass.connection.type_cast(serialized)
case serialized
when Date, Time
@klass.connection.type_cast(serialized)
else
serialized
end
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/paper_trail/attribute_serializers/object_attribute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ module AttributeSerializers
end
end
end

describe "#serialize" do
it "serializes a time object into a plain string" do
attrs = { "created_at" => Time.zone.local(2015, 7, 15, 20, 34, 0) }
described_class.new(Widget).serialize(attrs)
expect(attrs["created_at"]).not_to be_a(ActiveSupport::TimeWithZone)
expect(attrs["created_at"]).to be_a(String)
expect(attrs["created_at"]).to match(/2015/)
end
end

describe "#deserialize" do
it "deserializes a time object correctly" do
time = 1.day.ago
attrs = { "created_at" => time }
described_class.new(Widget).serialize(attrs)
described_class.new(Widget).deserialize(attrs)
expect(attrs["created_at"].to_i).to eq(time.to_i)
end
end
end
end
end

0 comments on commit ed4f74e

Please sign in to comment.