-
Notifications
You must be signed in to change notification settings - Fork 901
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 015/serializing postgres arrays #1018
Merged
jaredbeck
merged 11 commits into
paper-trail-gem:master
from
hubertpompecki:issue_015/serializing_postgres_arrays
Dec 8, 2017
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
aae7d93
Serialize a postgres into a plain array
hubertpompecki fdb7f29
Add specs for deserializing arrays
hubertpompecki c784f20
Rubocop
hubertpompecki 39bda99
Add comments
hubertpompecki 8bec622
Clean up
hubertpompecki 764eae7
Add byebug log to gitignore
hubertpompecki bf8e7d8
Add support for nested postgres subtypes
hubertpompecki e9b25c6
Merge branch 'master' into issue_015/serializing_postgres_arrays
jaredbeck 99dc9af
Merge branch 'master' into issue_015/serializing_postgres_arrays
jaredbeck dd7f239
Fix specs for postgres and ar < 5.0.2
hubertpompecki eb368b4
Review comments, update specs
hubertpompecki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
lib/paper_trail/attribute_serializers/attribute_serializer_factory.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require "paper_trail/type_serializers/postgres_array_serializer" | ||
|
||
module PaperTrail | ||
module AttributeSerializers | ||
# Values returned by some Active Record serializers are | ||
# not suited for writing JSON to a text column. This factory | ||
# replaces certain default Active Record serializers | ||
# with custom PaperTrail ones. | ||
module AttributeSerializerFactory | ||
def self.for(klass, attr) | ||
active_record_serializer = klass.type_for_attribute(attr) | ||
case active_record_serializer | ||
when ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array then | ||
TypeSerializers::PostgresArraySerializer.new( | ||
active_record_serializer.subtype, | ||
active_record_serializer.delimiter | ||
) | ||
else | ||
active_record_serializer | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
lib/paper_trail/type_serializers/postgres_array_serializer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module PaperTrail | ||
module TypeSerializers | ||
# Provides an alternative method of serialization | ||
# and deserialization of PostgreSQL array columns. | ||
class PostgresArraySerializer | ||
def initialize(subtype, delimiter) | ||
@subtype = subtype | ||
@delimiter = delimiter | ||
end | ||
|
||
def serialize(array) | ||
return serialize_with_ar(array) if active_record_pre_502? | ||
array | ||
end | ||
|
||
def deserialize(array) | ||
return deserialize_with_ar(array) if active_record_pre_502? | ||
|
||
case array | ||
# Needed for legacy reasons. If serialized array is a string | ||
# then it was serialized with Rails < 5.0.2. | ||
when ::String then deserialize_with_ar(array) | ||
else array | ||
end | ||
end | ||
|
||
private | ||
|
||
def active_record_pre_502? | ||
::ActiveRecord::VERSION::MAJOR < 5 || | ||
(::ActiveRecord::VERSION::MINOR.zero? && ::ActiveRecord::VERSION::TINY < 2) | ||
end | ||
|
||
def serialize_with_ar(array) | ||
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array. | ||
new(@subtype, @delimiter). | ||
serialize(array) | ||
end | ||
|
||
def deserialize_with_ar(array) | ||
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array. | ||
new(@subtype, @delimiter). | ||
deserialize(array) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class PostgresUser < ActiveRecord::Base | ||
has_paper_trail | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
spec/paper_trail/attribute_serializers/object_attribute_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "spec_helper" | ||
|
||
module PaperTrail | ||
module AttributeSerializers | ||
::RSpec.describe ObjectAttribute do | ||
if ENV["DB"] == "postgres" && ::ActiveRecord::VERSION::MAJOR >= 5 | ||
describe "postgres-specific column types" do | ||
describe "#serialize" do | ||
it "serializes a postgres array into a plain array" do | ||
attrs = { "post_ids" => [1, 2, 3] } | ||
described_class.new(PostgresUser).serialize(attrs) | ||
expect(attrs["post_ids"]).to eq [1, 2, 3] | ||
end | ||
end | ||
|
||
describe "#deserialize" do | ||
it "deserializes a plain array correctly" do | ||
attrs = { "post_ids" => [1, 2, 3] } | ||
described_class.new(PostgresUser).deserialize(attrs) | ||
expect(attrs["post_ids"]).to eq [1, 2, 3] | ||
end | ||
|
||
it "deserializes an array serialized with Rails <= 5.0.1 correctly" do | ||
attrs = { "post_ids" => "{1,2,3}" } | ||
described_class.new(PostgresUser).deserialize(attrs) | ||
expect(attrs["post_ids"]).to eq [1, 2, 3] | ||
end | ||
|
||
it "deserializes an array of time objects correctly" do | ||
date1 = 1.day.ago | ||
date2 = 2.days.ago | ||
date3 = 3.days.ago | ||
attrs = { "post_ids" => [date1, date2, date3] } | ||
described_class.new(PostgresUser).serialize(attrs) | ||
described_class.new(PostgresUser).deserialize(attrs) | ||
expect(attrs["post_ids"]).to eq [date1, date2, date3] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like how you are testing two different types of arrays here. Also, it looks like this is the first test table to have arrays at all! Kind of surprising.